顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
顺序表分为静态存储的顺序表和动态存储的顺序表。
这里先说明静态顺序表的基本操作的实现:
//SeqList.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include<string.h>
#include<assert.h>
#define MAX_SIZE 100
typedef int DataType;
typedef struct SeqList
{DataType array[MAX_SIZE];size_t size;
}SeqList,*pSeqList;void InitSeqList(pSeqList pSeq);
void PrintSeqList(pSeqList pSeq);void PushBack(pSeqList pSeq, DataType x);
void PopBack(pSeqList pSeq);
void PushFront(pSeqList pSeq, DataType x);
void PopFront(pSeqList pSeq);void Insert(pSeqList pSeq, int pos, DataType x);
void Remove(pSeqList pSeq, DataType x);
void RemoveAll(pSeqList pSeq, DataType x);int Find(pSeqList pSeq,DataType x);
void Erase(pSeqList pSeq,DataType pos);
void ReverseList(pSeqList pSeq);
void SortList(pSeqList pSeq);
int BinarySearch(pSeqList Seq, DataType x);void InitSeqList(pSeqList pSeq)
{memset(pSeq->array,0,sizeof(DataType)*MAX_SIZE);pSeq->size = 0;
}void PushBack(pSeqList pSeq, DataType x)
{assert(pSeq);if(pSeq->size >= MAX_SIZE){printf("SeqList is Full");return;}pSeq->array[pSeq->size] = x;pSeq->size++;
}void PopBack(pSeqList pSeq)
{assert(pSeq);if(pSeq->size <= 0){printf("SeqList is empty");return;}pSeq->array[pSeq->size-1] = NULL;pSeq->size--;
}void PushFront(pSeqList pSeq, DataType x)
{int i = 0;assert(pSeq);if(pSeq->size >= MAX_SIZE){printf("SeqList is Full");return;}for(i = pSeq->size-1; i >= 0; i--){pSeq->array[i+1] = pSeq->array[i];}pSeq->array[0] = x;pSeq->size++;
}void PopFront(pSeqList pSeq)
{int i = 0;assert(pSeq);if(pSeq->size <= 0){printf("SeqList is empty");return;}for(i = 1; i < pSeq->size; i++){pSeq->array[i-1] = pSeq->array[i];}pSeq->size--;
}void PrintSeqList(pSeqList pSeq)
{int i = 0;assert(pSeq);for(i = 0; i < pSeq->size;i++){printf("%d ",pSeq->array[i]);}
}void Insert(pSeqList pSeq, int pos, DataType x)
{int i = 0;assert(pSeq);if(pSeq->size >= MAX_SIZE){printf("SeqList is Full");return;}for(i = pSeq->size-1; i >= pos; i--){pSeq->array[i+1] = pSeq->array[i];}pSeq->array[pos] = x;pSeq->size++;
}int Find(pSeqList pSeq,DataType x)
{int i = 0;assert(pSeq);if(pSeq->size <= 0){printf("SeqList is empty");return;}for(i = 0; i < pSeq->size; i++){if(pSeq->array[i] == x){return i;}}return -1;
}void Erase(pSeqList pSeq,DataType pos)
{int i = 0;assert(pSeq);if(pSeq->size <= 0){printf("SeqList is empty");return;}for(i = pos+1; i < pSeq->size; i++){pSeq->array[i-1] = pSeq->array[i];}pSeq->size--;
}void Remove(pSeqList pSeq, DataType x)
{int pos = Find(pSeq,x);assert(pSeq);if(pos != -1){Erase(pSeq,pos);}else{printf("not exist");}
}void RemoveAll(pSeqList pSeq, DataType x)
{int count = 0;int i = 0;assert(pSeq);if(pSeq->size <= 0){printf("SeqList is empty");return;}for(i = 0; i < pSeq->size; i++){if(pSeq->array[i] == x){count++;}else{pSeq->array[i-count] = pSeq->array[i];}}pSeq->size-=count;
}void ReverseList(pSeqList pSeq)
{int left = 0;int right = pSeq->size-1;assert(pSeq);while(left < right){int tmp = pSeq->array[left];pSeq->array[left] = pSeq->array[right];pSeq->array[right] = tmp;left++;right--;}
}void SortList(pSeqList pSeq)
{int i = 0, j = 0;assert(pSeq);for(i = 0; i < pSeq->size-1; i++){for(j = 0; j < pSeq->size-i-1; j++){if(pSeq->array[j] > pSeq->array[j+1]){int tmp = pSeq->array[j];pSeq->array[j] = pSeq->array[j+1];pSeq->array[j+1] = tmp;}}}
}int BinarySearch(pSeqList pSeq, DataType x)
{int left = 0;int right = pSeq->size-1;assert(pSeq);while(left <= right){int mid = left - (left - right)/2;if(pSeq->array[mid] > x){right = mid-1;}else if(pSeq->array[mid] < x){left = mid+1;}else{return mid;}}return -1;
}#endif //__SEQLIST_H__ //SeqList.c#include<stdio.h>
#include"SeqList.h"void Test1()
{SeqList seq;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PopBack(&seq);PrintSeqList(&seq);
}
void Test2()
{SeqList seq;int ret;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PushFront(&seq,0);PopFront(&seq);Insert(&seq,2,6);ret = Find(&seq,6);printf("%d\n",ret);PrintSeqList(&seq);
}
void Test3()
{SeqList seq;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PushBack(&seq,4);PushBack(&seq,2);PushBack(&seq,5);PushBack(&seq,6);PopBack(&seq);Erase(&seq,2);Remove(&seq,2);RemoveAll(&seq,4);PrintSeqList(&seq);
}
void Test4()
{SeqList seq;int pos;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PushBack(&seq,4);PushBack(&seq,5);ReverseList(&seq);SortList(&seq);pos = BinarySearch(&seq,2);printf("%d\n",pos);PrintSeqList(&seq);
}int main()
{//Test1(); //Test2();//Test3();Test4();return 0;
}