300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 《数据结构》C语言版 链表的基本操作实现

《数据结构》C语言版 链表的基本操作实现

时间:2019-05-15 23:18:02

相关推荐

《数据结构》C语言版 链表的基本操作实现

#include <stdio.h>#include <stdlib.h>#define ok 1#define error -1typedef int ElemType;typedef int Status;typedef struct Node{ElemType data;struct Node* next;}LNode, *LinkList;//构造空表Status InitList(LinkList &L){L = (LinkList)malloc(sizeof(LNode));L->next = NULL;//头结点指针域为空,则表为空表return ok;}// 头插法void CreatList(LinkList &L,int n){LNode *p;int i;L->next=NULL;//先建立一个带有头结点的单链表for(i=n;i>0;--i){p=(LinkList)malloc(sizeof(LNode));//生成新节点printf("No.%d:",i);scanf_s("%d",&p->data);p->next=L->next;L->next=p;//插到表头}}//void CreatList(LinkList &L, int n)//{//LNode* p, * q;//int i = 1;//L->next = NULL;//建立一个带头结点的单链表//q = L;//for (i = 1; i <= n; i++)//{//p = (LinkList)malloc(sizeof(LNode));//printf("No.%d:", i);//scanf_s("%d", &p->data);//p->next = q->next;//q->next = p;//新节点插在表尾//q = p;//}//}void PrintfList(LinkList L){LNode* p;p = L->next;//初始化,p指向第一个结点if (p == NULL)printf("the list is NUll!");while (p != NULL){printf("%d ", p->data);p = p->next;}printf("\n");}int LengthList(LinkList L){LNode* p;int count = 0;p = L->next;while (p != NULL){count++;p = p->next;}return count;}//清空:是先保留了链表的头,然后把头后面的所有turn count;向下一个的指针设为空,//这样就相当与清空了,但这个链表还在,还可以继续使用;即保留了头,后面的全部释放。void CLearList(LinkList &L)//清空链表{LNode* p, * q;p = L->next;while (p) {q = p->next;free(p);p = q;}L->next = NULL;}//销毁:是先销毁了链表的头,//然后接着一个一个的把后面的销毁了,//这样这个链表就不能再使用了,即把包括头的所有节点全部释放,不能进行任何操作了。void DestoryList(LinkList &L)//毁坏链表{LNode* p, * q;/*(*L)->data = 0;*/p = L->next;L->next = NULL;while (p){q = p->next;free(p);p = q;}}Status ListInsert(LinkList L, int i, ElemType e){//单链表的第i个位置插入元素eLNode* p, * s;int j = 1;p = L->next;//初始化,p指向第一个结点if (i < 1)return error;//插入位置错while (p && j < i - 1){p = p->next;j++;}s = (LinkList)malloc(sizeof(LNode));s->next = p->next;//插入到L中p->next = s;s->data = e;return ok;}Status DeleteList(LinkList L, int i, ElemType& e) {// 删除第i个元素,其值返回eint j = 0;LNode* p, * q;p = L;if (i<1 || i>LengthList(L)) {printf("删除的位置异常!!!");return error;}while (p && j < i - 1)//寻找第i个结点,并令p指向其前驱{j++;p = p->next;}if (!p || j > i - 1)return error;//删除位置不合理q = p->next;// q指向待删除节点p->next = q->next;//待删除节点的前驱指向待删除节点的后继e = q->data;// e指向q的数据域free(q);//释放待删除节点return ok;}Status GetElem(LinkList L, int n, ElemType &e){LNode* p;int j = 1;if (n < 1)return error;p = L->next;while (p && j < n){p = p->next;j++;}e = p->data;//printf("the get number is %d\n",*e);return ok;}void MergeList(LinkList &La,LinkList&Lb, LinkList &Lc) {//已知单链表La和单链表Lb的元素按照非递减排列//并归La和Lb得到新单线性链表Lc,Lc的元素也按非递减排列LNode *pa = La->next;LNode *pb = Lb->next;LNode* pc;Lc = pc = La;//用La的头结点作为Lc的头结点while (pa&&pb){if (pa->data<=pb->data){pc->next = pa;pc = pa;pa = pa->next;}else{pc->next = pb;pc = pb;pb = pb->next;}}pc->next = pa ? pa : pb;//插入剩余片段free(Lb);}void main(){LinkList L;int num=1;if (InitList(L) == ok)printf("the LinkList InitList is successful!\n");while (num != 0){system("cls");printf("1.CreatList\t2.PrintfList\t3.LengthList\n");printf("4.ListInsert\t5.DeleteList\t6.GetElem\t\n");printf("7.ClearList\t8.DestoryList\t9.MergeList\t0.Exit\n");printf("enter your choose:");scanf_s("%d", &num);switch (num){case 1:{int i;printf("enter the List Length:");scanf_s("%d", &i);CreatList(L, i);}break;case 2:PrintfList(L);break;case 3:printf("the List length is %d\n", LengthList(L));break;case 4:{ElemType e;int i;printf("enter the position:");scanf_s("%d", &i);printf("enter the number:");scanf_s("%d", &e);if (ListInsert(L, i, e) == ok)PrintfList(L);}break;case 5:{int i;ElemType e;printf("enter the Delete Position:");scanf_s("%d", &i);if (DeleteList(L, i, e) == ok)PrintfList(L);}break;case 6:{int i;ElemType e;printf("enter the want to get number position:");scanf_s("%d", &i);if (GetElem(L, i, e) == ok)//get the list number{printf("the number is found!\n");printf("%d \n", e);}elseprintf("the number is not found!\n");}break;case 7:CLearList(L);printf("the clear is success!\n");break;case 8:DestoryList(L);printf("the destory is success!\n");break;case 9:{LinkList Lb;LinkList Lc;if (InitList(Lb) == ok) {//初始化Lbprintf("Lb Init success!\n");int i;printf("enter the List Length:");scanf_s("%d", &i);CreatList(Lb, i);PrintfList(Lb);MergeList(L, Lb, Lc);printf("合并后的链表为:\n");PrintfList(Lc);}elseprintf("Lb Init fail!\n");break;}case 0:printf("byebye");break;default:break;}printf("\n");system("pause");}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。