数据结构—顺序表

Posted fengkun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构—顺序表相关的知识,希望对你有一定的参考价值。

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<memory.h>


typedef struct seqlist 

    int arr[10];   //首元素地址
    int capacity;  //数组一共能容纳多少数据。sizeof(arr)/sizeof(arr[0])
    int size;      //统计数组现在已经放进去多少数据,size值的范围  [0,capacity]
seqlist;          //数组元素下标的最大值  capacity-1

顺序表:表现一般是数组,地址连续的存储单元依次存储数据元素。arr[10]
长度是固定的,在分配内存之前要确定数组的长度   n=sizeof(arr) / sizeof(arr[0])
存储空间连续,允许随机访问,即访问任何下标的地方都可以   [0,n)
使用索引(下标)访问元素  arr[index]

插入元素,要判断是否超出数组下标。超出则申请二倍长度的新空间,再数据复制到新空间
每超出,如果是将k插入到链表末尾:
int main()

    int arr[10] =  1, 2, 3, 4 ;
    seqlist* Seqlist;

    Seqlist->arr[9] = k;
    Seqlist->capacity = sizeof(arr) / sizeof(arr[0]);
    Seqlist->size = 0;
    Seqlist->size++;


void print(int arr[], int size)

    assert(arr != NULL);

    for (int i = 0; i < size; i++)
    
        printf("%d ", arr[i]);
    
    printf("\n");

typedef struct seqlist

    int arr[10];  //首元素地址
    int capacity;  //数组一共能容纳多少数据。sizeof(arr)/sizeof(arr[0])
    int size;      //统计数组现在已经放进去多少数据,size值的范围  [0,capacity]
seqlist;               //数组元素下标的最大值  capacity-1


void InitSeqlist(seqlist* Seqlist)

    assert(Seqlist != NULL);

    
    int L = sizeof(Seqlist->arr) / sizeof(Seqlist->arr[0]);
    Seqlist->capacity = L;
    Seqlist->size = 0;
    for (int i = 0; i < L-3; ++i)
    
        Seqlist->arr[i] = i;
        Seqlist->size++;
    
    
    



void pushbackSeqlist(seqlist* Seqlist, int k)

    assert (Seqlist != NULL);
    Seqlist->arr[Seqlist->size] = k;
    Seqlist->size++;


void pushfrontSeqlist(seqlist* Seqlist,int y)

    assert(Seqlist != NULL);
    for (int i = Seqlist->size; i > 0; i--)
    
        Seqlist->arr[i] = Seqlist->arr[i - 1];
    
    Seqlist->arr[0] = y;
    Seqlist->size++;



void pushMidSeqlist(seqlist* Seqlist, int z, int index)

    int i = 0;
    for (i = Seqlist->size; i >= index; --i)
    
        Seqlist->arr[i] = Seqlist->arr[i-1];
    
    Seqlist->arr[index] = z;
    Seqlist->size++;


void searchSeqlist(seqlist* Seqlist, int y)

    int i = 0;
    while (i<Seqlist->size)
    
        if (Seqlist->arr[i] != y)
        i++;
        if (Seqlist->arr[i] == y)
        
            printf("%d的下标是%d\n", y, i);
            break;
        
        else
            i++;

    

    if(i >= Seqlist->size)
    printf("找不到\n");


void SeqListPopFront(seqlist *Seqlist) 

    assert(Seqlist->size > 0);
    for (int i = 0; i <= Seqlist->size - 2; i++) 
        Seqlist->arr[i] = Seqlist->arr[i + 1];
    
    Seqlist->size--;


void SeqListPopBack(seqlist *Seqlist)

    assert(Seqlist->size > 0);
    Seqlist->size--;


void SeqListErase(seqlist *Seqlist, int index) 

    assert(Seqlist->size > 0);

    for (int i = index; i <= Seqlist->size - 2; i++) 
        Seqlist->arr[i] = Seqlist->arr[i + 1];
    

    Seqlist->size--;


void SeqListRemove(seqlist *Seqlist, int data) 

    int i = 0;
    for (i = 0; i < Seqlist->size; i++)
    
        if (Seqlist->arr[i] == data)
        
            break;
        
    
    for (int index = i; index <= Seqlist->size - 2; i++)
        
            Seqlist->arr[i] = Seqlist->arr[i + 1];
        

        Seqlist->size--;



int SeqListSize(seqlist* Seqlist)

    assert(Seqlist->size != 0);
    return Seqlist->size;


int SeqListCapacity(seqlist* Seqlist)

    assert(Seqlist->capacity != 0);
    return Seqlist->capacity;


void SeqListEmptyCheck(seqlist* Seqlist)

    if (Seqlist->size == 0)
    
        printf("链表为空\n");
    

    else
        printf("链表不为空\n");


int SeqListFront(seqlist* Seqlist)

    return Seqlist->arr[0];


int SeqListBack(seqlist* Seqlist)

    return Seqlist->arr[Seqlist->size - 1];


void SeqListDestroy(seqlist * Seqlist) 

    free(Seqlist->arr);
    
    Seqlist->capacity = 0;
    Seqlist->size = 0;


void addCapacitySeqlist(seqlist* Seqlist)

    seqlist*Seqlist2 = malloc(2 * sizeof(int)*(Seqlist->capacity));
    for (int i = 0; i < Seqlist->size; i++)
    
        Seqlist2->arr[i] = Seqlist->arr[i];
    
    memcpy(Seqlist2,Seqlist,Seqlist->capacity);
    Seqlist2->capacity = 2 * (Seqlist->capacity);
    print(Seqlist2->arr, Seqlist2->capacity);

    seqlist *a=Seqlist;
    Seqlist = Seqlist2;
    Seqlist->capacity = Seqlist2->capacity;
    print(Seqlist->arr, Seqlist->capacity);
    free(a);




int main()

    seqlist a;
    seqlist* Seqlist;
    Seqlist = &a;

    int arr[10] ;
    int k = 8;
    int y = 7;
    int z = 9;
    int index = 5;
    seqlist* Seqlist = malloc(sizeof(int)* 10);
    
    InitSeqlist(Seqlist);
    print(Seqlist->arr, Seqlist->capacity);

    pushbackSeqlist(Seqlist,k);
    print(Seqlist->arr, Seqlist->capacity);

    pushfrontSeqlist(Seqlist, y);
    print(Seqlist->arr, Seqlist->capacity);

    pushMidSeqlist(Seqlist, z, index);
    print(Seqlist->arr, Seqlist->capacity);

    searchSeqlist(Seqlist,5);

    SeqListPopFront(Seqlist);

    SeqListPopBack(Seqlist);

    SeqListErase(Seqlist,index);

    int ret=SeqListSize(Seqlist);

    int Scapacity=SeqListCapacity(Seqlist);

    SeqListEmptyCheck(Seqlist);

    int FirstData = SeqListFront(Seqlist);

    int LastData = SeqListBack(Seqlist);

    SeqListDestroy(Seqlist);



    Seqlist->size = 11;
    if (Seqlist->size > Seqlist->capacity)
    addCapacitySeqlist(Seqlist);
    print(Seqlist->arr, Seqlist->capacity);


    system("pause");
    Seqlist->arr[9] = k;
    Seqlist->capacity = sizeof(arr) / sizeof(arr[0]);
    Seqlist->size = 0;
    Seqlist->size++;
    return 0;

 

以上是关于数据结构—顺序表的主要内容,如果未能解决你的问题,请参考以下文章

数据结构之顺序表

数据结构,用JAVA编写顺序表

数据结构之顺序表

数据结构顺序表

数据结构C语言版 —— 顺序表增删改查实现

数据结构之顺序表