一.顺序表
1.顺序表的静态存储和动态存储
#define N 100
struct SeqList
{
int a[N];
int size;
};
struct SeqList
{
int* a;
int size; //有效数据的个数
int capacity; //容量
};
typedef int SeqDateType //升级版
struct SeqList
{
SeqDateType* a;
int size; //有效数据的个数
int capacity; //容量
};
实现增删查改
增 头查,尾插,中间插,最重要的四个顺序表
顺序表常见接口
void SeqListPushBack(struct SeqList* pseq, SeqDateType x);//尾插 { if(pq->size == pq->capacity) { int newcapacity = pseq->capacity == 0 ? 4 : pseq->capacity * 2; SeqDateType* newA = realloc(pq->a, sizeof(SeqDateType)*newcapacity); if(newA == NULL) { printf("realloc fail\n"); exit(-1); } pseq->a = newA; pseq->capacity = newcapacity; } assert(pseq); pseq->a[pseq->size] = x; pseq->size++; } void SeqListPushFront(struct SeqList* pseq, SeqDateType x);//头查 { assert(pseq) int end = pq->size -1; while(end >= 0) { pq->a[end + 1] = pq->a[end]; --end; } pq->a[0] = x; pq->size++; } void SeqListPopBack(struct SeqList* pseq);//尾删 { assert(pseq); assert(pseq->size > 0); --pseq->size; } void SeqListPopFront(struct SeqList* pseq);//头删 { assert(pq); assert(pq->size>0); int begin = 0; while(begin<pq->size - 1) { pq->a[begin] = pq->a[begin+1]; } pq->size--; } void SeqListPopPrint(struct SeqList* pseq);//打印链表 { assert(pseq); for(int i = 0; i < pq->size; ++i) { printf("%d ",pq->a[i]); } printf("\n"); } void SeqCheckCapacity(SeqList* pseq)//扩容 { if(pq->size == pq->capacity) { int newcapacity = pseq->capacity == 0 ? 4 : pseq->capacity * 2; SeqDateType* newA = realloc(pq->a, sizeof(SeqDateType)*newcapacity); if(newA == NULL) { printf("realloc fail\n"); exit(-1); } pseq->a = newA; pseq->capacity = newcapacity; } } int SeqListFind(SeqList* pq,SeqDateType x)//查找 { assert(pq); for(int i = 1;i < pq->size;++i) { if(pq->a[i] == x) { return i; } } return -1; } void SeqListInsert(SeqList* pq, int pose, SeqDateType x)//中间插 { assert(pq); assert(pose >= 0 && pq <= pq->size); SeqCheckCapacity(pq); int end = size - 1; while(end >= pose) { pq->a[end + 1] = pq->a[end]; --end; } pq->a[pose] = x; pq->size++; } void SeqListErase(SeqList* pq, int pose)//删除 { assert(pq); assert(pos >= 0 && pos < pq->size); int begin = pose; while(begin <= pq->size - 1) { pq->a[begin] = pa->a[begin+1]; ++begin; } pq->size--; } void SeqListModify(SeqList* pq,int pos, SeqDateType x)//修改 { assert(pq); assert(pos >= 0&& pos < pq->size); pq->a[pos] = x; }
typedef struct SeqList
{
SeqDateType* a;
int size; //有效数据的个数
int capacity; //容量
}SeqList;
SeqList seq;
void SeqListPopInit(struct SeqList* pseq);//初始化
{
assert(pseq);
pseq.a = NULL;
pseq.size = pseq.capacity = 0;
}
void SeqListPopDestory(struct SeqList* pseq);//销毁
{
assert(pseq);
free(pseq->a);
pseq->a = NULL;
pseq->size = pseq->capacity = 0;
}
二.链表
二.一.单向链表
typedef int SLTDataType
typedef struct SlistNode //单向
{
int data;
struct SlistNode* next;
}SLTNode;
/*struct ListNode //双向
{
int data;
struct ListNode* next;
struct ListNode* before;
};*/
void SListPushBack(SLTNode** plist, SLTDataType x) //尾插
{
STLNode* newnode* newnode = BuySLTNode(x);
if(plist == NULL)
{
plist = newnode;
}
else
{
SLTNode* tail plist;
while(tail->next != NULL)
{
tail = tail->next;
}
SListnode* newnode = BuySLTNode(x);
tail->next = newnode;
}
}
void SListPushFront(SLTNode** plist, SLTDataType x)//头插
{
SLTNode* newnode = BuySLTNode(x);
newnode->next = *pplist;
*pplist =newnode;
}
void SListPopBack(SLTNode** pplist)//尾删
{
if(*pplist = NULL)
return;
else if ((*pplist)->next = NULL)
{
free(*pplist);
*pplist = NULL;
}
else
{
SLTNode* prev = NULL;
SLTNode* tail = plist;
while(tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail)
tail = tail->next;
prev->next = NULL;
}
}
void SListPopFront(SLTNode** pplist)//头删
{
if(pplist = NULL);
return;
else
{
SLTNode* next = (*pplist)->next;
free(*pplist);
*pplist = next;
}
}
void SListPrint(SLTNode* plist) //打印
STList* cur = plist;
while(cur != NULL)
{
printf("%d ",cur->data);
cur = cur->next;
}
printf("\n");
}
void SListFind(SLTNode* plist, SLTDataType x)//查找,可以修改
{
SLTNode*cur = plist;
while(cur)
{
if(cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void SListInsert(SLTNode* pos, SLTDataType x)
{
assert(pos);
SLTNode* newwnode = BuySLTNode(x);
newnode->next = pos->next =NULL;
pos->next = newnode;
}
void SListInsertAfter(SLTNode* pos, SLTDataType x)//很麻烦
phead 指针变量 哨兵位的头节点
二.二双向链表
typedef int LTDataType;
typedef struct ListNode //双向链表
{ 结构复杂,操作简单
struct ListNode* next;
struct ListNode* prev;
LTDataType data;
}ListNode;
ListNode* BuyListNode(LTDataType x) //头节点
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->next = NULL;
node->prev = NULL;
node->data = x;
return node;
}
ListNode Listinit() //
{
ListNode* phead = BuyListNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListNodePushBack(ListNode* phead, LTDataType x) //尾插
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* newnode = BuyListNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
void ListPrint(ListNode* phead)
{
ListNode* cur = phead->next;
while(cur != phead)
{
printf("%d ",cur->data);
cur = cur->next;
}
printf("\n");
}
void ListNodePushFront(ListNode* phead, LTDataType x)
{
assert(phead);
ListNode* newnode = BuyListNode(x);
ListNode* first = phead->next;
phead->next = newnode;
newnode->prev = phead;
newnode->next = first;
first->prev = newnode;
}
void ListNodePopBack(ListNode* phead)
{
assert(phead);
assert(phead->next != NULL);
ListNode* tail = phead->prev;
ListNode* tailPrev = tail->prev;
free(tail);
tailPrev->next = phead;
phead->prev = tailPrev;
}
void ListNodePopFront(ListNode* phead) //delete
{
assert(phead);
assert(phead->next != phead);
ListNode* first = phead->next;
ListNode* second = first->next;
free(first);
phead->next = second;
second->prev = phead;
}
ListNode* ListFind(ListNode* phead, LTDataType x) //查找
{
assert(phead);
while(cur != phead)
{
if(cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void ListInsert(ListNode* pos, LTYDataType x) //插入
{
assert(pos);
ListNode* prev = pos-> prev;
ListNode* newnode = BuyListNode(x);
prev->next = newnode;
newnode->prev = prev;
newnode->next = pos;
pos->prev = newnode;
}
void ListErase(ListNode* pos) //delete
{
ListNode* prev = pos->prev;
ListNode* next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}