单链表

来源:互联网 发布:返享网络 编辑:程序博客网 时间:2024/06/03 12:50

@  今天写了整整6个小时,在不断的出错找错,修改中,终于将单链表初步成型了,真的很累,代码中没有多余的注释,现在的版本还是有很多地方可以改进,可是今天真的很累了! 后期的完善和优化好之后,会加上详细注释! 大家可以先看看思路


@  单链表初成型


#  自定义头文件"LinkList.h",包含一些函数功能的声明;


#define  _CRT_SECURE_NO_WARNINGS 1#ifndef __LINKLIST_H__//防止头文件的重定义#define __LINKLIST_H__#include<stdio.h>#include<stdlib.h>#include<assert.h>typedef int DataType;//将int重命名,方便修改,如果元素是char只需要将int改为char;enum Select{EXIT,INSERT,PRINT,REMOVE,REMOVEALL,SEARCH,SORT,PUSHF,PUSHB,POPF,POPB,DOSTORY,EASER,};typedef struct LinkNode{DataType data;struct LinkNode* next;}LinkNode,*pLinkNode;//结点结构体typedef struct LinkList{LinkNode* pHead;//头结点指针}LinkList ,*pLinkList;//链表void InitLinkList(pLinkList pList);//初始化列表void PrintList(pLinkList pList);//打印void DostoryList(pLinkList pList);//free链表void PopBack(pLinkList pList);//尾删void PopFront(pLinkList pList);//头删void PushBack(pLinkList pList,DataType x);//尾插void PushFront(pLinkList pList,DataType x);//头插void Insert(pLinkList pList,pLinkNode pos,DataType x);//指定位置前或者后插入元素pLinkNode Find(pLinkList pList,DataType x);//查找void Search(pLinkList pList,DataType x);//查找指定元素;void Remove(pLinkList pList,DataType x);//删除指定元素void RemoveAll(pLinkList pList,DataType x);//删除所有出现的指定元素void Erase(pLinkList pList,pLinkNode pos);//删除指定位置的元素void BubbleSort(pLinkList pList);//冒泡排序链表元素void Exit();#endif //__LINKLIST_H__

# 函数的测试部分 main();

#include"LinkList.h"void Print(){printf("\n————————————————————\n");printf("***************************************\n");printf("**** 0.退出                      ******\n");printf("**** 1. 插入元素                 ******\n");printf("**** 2. 打印链表                 ******\n");printf("**** 3. 删除指定元素             ******\n");printf("**** 4. 删除所有出现的指定元素   ******\n");printf("**** 5. 查找                     ******\n");printf("**** 6. 排序                     ******\n");printf("**** 7. 头插                     ******\n");printf("**** 8. 尾插                     ******\n");printf("**** 9. 头删                     ******\n");printf("**** 10. 尾删                    ******\n");printf("**** 11. 释放链表                   ******\n");printf("**** 12. 删除指定位置的元素      ******\n");printf("***************************************\n");printf("\n————————————————————\n");}void test(){LinkList List = {0};int select = 0;DataType x = 0;DataType pos = 0;InitLinkList(&List);while(1){Print();printf("请输入选项:> ");scanf("%d",&select);switch(select){case EXIT:Exit();break;case INSERT:printf("请输入要在哪一个位置前插入:> ");scanf("%d",&pos);printf("请输入要插入的元素:> ");scanf("%d",&x);Insert (&List,Find(&List,pos),x);break;case PRINT:PrintList(&List);break;case REMOVE:Remove(&List,5);break;case REMOVEALL:RemoveAll(&List,1);break;case SEARCH:Search(&List,3);break;case SORT:BubbleSort(&List);break;case PUSHF:printf("请输入要插入的元素:> ");scanf("%d",&x);PushFront (&List,x);break;case PUSHB:printf("请输入要插入的元素:> ");scanf("%d",&x);PushBack (&List,x);break;case POPF:PopFront (&List);break;case POPB:PopBack (&List);break;case EASER:printf("请输入指定位置元素:>\n");scanf("%d",&pos);Erase(&List,Find(&List,pos));break;case DOSTORY:DostoryList (&List);break;default:break;}}}int main(){test();system("pause");return 0;}

#  函数的实现部分   LinkList.c

#include"LinkList.h"void InitLinkList(pLinkList pList){assert(pList);pList->pHead = NULL;}//初始化列表void PrintList(pLinkList pList){pLinkNode cur = NULL;assert(pList);cur = pList->pHead ;while( cur!=NULL ){printf("%d->",cur->data );cur = cur->next ;}printf("over\n");}//打印void DostoryList(pLinkList pList){pLinkNode cur = NULL;pLinkNode tmp = NULL; assert(pList);cur = pList->pHead ;if(pList ->pHead == NULL)return;while(cur){tmp = cur->next ;free(cur);cur = NULL;cur = tmp;}pList ->pHead = NULL;}//free链表void PopBack(pLinkList pList){pLinkNode cur = NULL;pLinkNode pvr = NULL;assert(pList);    cur = pList->pHead ;if(pList ->pHead ==NULL)return;else if(cur ->next == NULL){pList->pHead = cur->next;         free(cur);         cur = NULL;}else{while(cur->next){pvr = cur;cur = cur->next ;}free(cur);pvr->next = NULL;}}//尾删void PopFront(pLinkList pList){pLinkNode cur = NULL;assert(pList);cur = pList ->pHead ;if(cur == NULL)return;pList->pHead = cur->next;free(cur);cur = NULL;}//头删void PushBack(pLinkList pList,DataType x){pLinkNode cur = NULL;pLinkNode pvr = NULL;pLinkNode newNode = (pLinkNode)malloc(sizeof(LinkNode ));if(newNode == NULL){printf("out of memory\n");exit(0);}assert(pList);cur = pList ->pHead ;newNode ->data = x;newNode ->next = NULL;if(cur == NULL){pList ->pHead  = newNode ;return;}while(cur){pvr = cur;cur = cur->next ;}pvr->next  = newNode ;}//尾插void PushFront(pLinkList pList,DataType x){pLinkNode cur = NULL;pLinkNode newNode = (pLinkNode)malloc(sizeof(LinkNode ));if(newNode == NULL){printf("out of memory\n");exit(0);}assert(pList);cur = pList ->pHead ;newNode ->data = x;newNode ->next = cur;pList ->pHead  = newNode ;}//头插void Insert(pLinkList pList,pLinkNode pos,DataType x){//将指定元素插入到指定位置前, 插入指定位置后类似;pLinkNode cur = NULL;pLinkNode pvr = NULL;DataType tmp = x;pLinkNode newNode = (pLinkNode )malloc(sizeof (LinkNode ));if(newNode == NULL){printf("out of memory!\n");return ;}newNode ->data = tmp;assert(pList);cur = pList ->pHead ;if(cur == NULL){PushFront (pList,x);return ;}if(pos == NULL){printf("default locate!\n");return;}newNode ->next = pos->next ;pos->next = newNode ;tmp = pos->data ;pos->data = newNode ->data ;newNode ->data = tmp;}//指定位置前或者后插入元素pLinkNode Find(pLinkList pList,DataType x)//只找出第一次出现的x{pLinkNode cur = NULL;assert(pList);cur = pList ->pHead ;while(cur){if(cur->data == x)break;cur = cur->next ;}return cur;}//公用的查找函数void Search(pLinkList pList,DataType x){pLinkNode cur = NULL;assert(pList);cur = Find(pList, x);if(cur == NULL)printf("the x wasn't searched!\n");elseprintf("the search of result:> %d\n",cur->data );}//查找指定元素;void Remove(pLinkList pList,DataType x)//只删除第一个匹配到的元素;{pLinkNode cur = NULL;assert(pList);cur = pList ->pHead ;while(cur){if(cur->data == x){if(cur == pList ->pHead )PopFront ( pList);else if(cur->next == NULL )PopBack ( pList);else{pLinkNode tmp = NULL;cur->data = cur->next ->data ;tmp = cur->next ;cur->next = cur->next ->next ;free(tmp);tmp = NULL;}printf("\n Delete success!\n");return;}cur = cur->next ;}printf("\nThe list haven't exist x!\n");}//删除指定元素void RemoveAll(pLinkList pList,DataType x){pLinkNode cur = NULL;pLinkNode pon = NULL;int flag = 0;assert(pList);cur = pList ->pHead ;while(cur){if(cur->data == x){flag = 1;pon = cur->next ;if(cur == pList ->pHead ){PopFront ( pList);cur = pon;}else if(cur->next == NULL ){PopBack ( pList);cur = NULL;break;}else{pLinkNode tmp = NULL;cur->data = cur->next ->data ;tmp = cur->next ;cur->next = cur->next ->next ;free(tmp);tmp = NULL;}}else cur = cur->next ;           }if(flag == 1)printf("\n Delete success!\n");elseprintf("\nThe list haven't exist x!\n");}//删除所有出现的指定元素void Erase(pLinkList pList,pLinkNode pos){pLinkNode cur = NULL;pLinkNode pvr = NULL;assert(pList);cur = pList ->pHead ;if(pos == NULL){printf("\nThe list haven't exist x!\n");return ;}else if(pos == cur){PopFront (pList);return ;}while(cur != pos){pvr = cur;cur = cur->next ;}pvr->next = pos->next ;free(pos);pos = NULL;}//删除指定位置的元素void BubbleSort(pLinkList pList)//冒泡排序单链表{pLinkNode p = NULL;pLinkNode q = NULL;assert(pList);for(p = pList ->pHead ;p!=NULL; p = p->next ){for(q = p->next;q!=NULL; q = q->next ){if(p->data > q->data ){DataType tmp = q->data ;q->data  = p->data ;p->data = tmp;}}}}//冒泡排序链表元素void Exit(){exit(0);}

累了还是要好好休息一下!

1 0