数据结构链表(C语言) | 您所在的位置:网站首页 › 数据结构单链表编程题c语言 › 数据结构链表(C语言) |
链表
链表的概念及结构
概念:链表是一种物理存储结构上非连续,非顺序的储存结构,数据元素的逻辑顺序是通过链表中的指针连接次序实现的。
1.单向或者双向 头文件void SListPushback(SLTNode* phead,SLTDataType x); 源文件 void SListPushBack(SLTNode** pphead,SLTDataType x) { SLTNode* newnode =(SLTNode*)malloc(sizeof(SLTNode)); assert(newnode); newnode->data =x; newnode->next = NULL; if (*pphead==NULL) { *pphead=newnode; } else { //找尾节点 SListNode*tail = *pphead; while(tail->next !=NULL) { tail = tail->next; } tail->next = newnode; } void TestSList2()//创建一个四个数据的链表 { SLTNode* plist=NULL; SListPushBack(&plist,5); SListPushBack(&plist,6); SListPushBack(&plist,7); SListPushBack(&plist,8); SListPushBack(&plist,9); SlistPrint(plist); } 头插 void SlistPushFront(SLTNode** pphead,SLTDataType x) { SLTNode* newnode =(SLTNode*)malloc(sizeof(SLTNode)); assert(newnode); newnode->data =x; newnode->next = NULL; newnode->next=*pphead; *pphead=newnode; } void TestSlist2() { SLTNode* list=NULL; SLIstPushFront(&plist,0); SListPrint(plist); } 头删 void SListPopFront(SLTNode** pphead) { //assert(*pphead!=NULL); if(*pphead==NULL) return; SLTNode* next=(*pphad)->next; free(**phead); *pphead=next; } 尾删 void SListPopBack(SLTNode** pphead) { assert(*pphead); //只有一个节点 多个节点 if((*pphead)->next==NULL) { free(*pphead); *pphead=NULL; } else{ /* SLTNode* tailPrev=NULL; SLTNode* tail=*pphead; while(tail->next!=NULL) { tailPrev=tail; tail=tail->next; } free(tail); tailPrev->next=NULL;*/ SLTNode* tail=*pphead; while(tail->next->next!=NULL) { tail=tail->next; } free(tail->next); tail->next=NULL; } ] 查找 SLTNode* SListFind(SLTNode* phead,SLTDataType x) [ SLTNode* cur=phead; while(cur) { if(cur->data=x) return cur; cur } void TestSList() { SLTNode* plist=NULL; SListPushBack(&plist,1); SListPushBack(&plist,2); SListPushBack(&plist,3); SListPushBack(&plist,4); SListPushBack(&plist,5); SLTNode* ret=SListFind(plist,3); if(ret) { printf("找到了"); ret->data=30; } SListPrint(plist); } 在pos位置之前插入 void SListInsert(SLTNode** pphead,SLTNode* pos,SLTDataTyps x) { assert(pos); assert(pphead); //头插 if(pos==*pphead) { SListPushFront(pphead,x); } else { SLTNode* prev=*pphead; while(prev->next !=pos) { prev=prev->next; } SLTNode* newnode=BuySListNode(X); //BuySListNode(X); prev->next=newnode; newnode->next=pos; } } 删除pos位置的值 void SListErase(SLTNode** pphead,SLTNode* pos) { assert(pphead); assert(pos); if(*pphead==pos) { SListPopFront(pphead); } else { SLTnode* prev=*pphead; while(prev->next !=pos) { prev=prev->next; } prev->next=pos->next; free(pos); } } 单链表删除pos位置之后插入x void SListInsertAfter(SLTNode* pos,SLTDataTyps x) { assert(pos); /*SLTNode* newnode=BuySListNode(x); newnode->next=pos->next; pos->next=newnode;*/ //不在乎链接顺序 SLTNode*newnoed=BuySListNode(x); SLTNide next=pos->nexy; //pos newnode next } |
CopyRight 2018-2019 实验室设备网 版权所有 |