单链表的基本操作
实现单链表的基本操作(创建,查询,插入,删除,销毁,归并,排序)
创建链表 La 链表初始化成功 请输入La中元素的个数:9 请输入第 9 个元素:9 请输入第 8 个元素:8 请输入第 7 个元素:7 请输入第 6 个元素:6 请输入第 5 个元素:5 请输入第 4 个元素:4 请输入第 3 个元素:3 请输入第 2 个元素:2 请输入第 1 个元素:1 1 2 3 4 5 6 7 8 9 创建链表 Lb 链表初始化成功 请输入Lb中元素的个数:5 请输入第 5 个元素:9 请输入第 4 个元素:7 请输入第 3 个元素:5 请输入第 2 个元素:3 请输入第 1 个元素:1 1 3 5 7 9 合并链表 La Lb 链表初始化成功 1 1 2 3 3 4 5 5 6 7 7 8 9 9 请按任意键继续. . .
//----------------------------单链表的存储表示------------------------------- #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OVERFLOW -1 typedef int Status; typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *Next; }LNode, *LinkList; Status InitList_L(LinkList *L) ; Status CreateList_L(LinkList *L, int n); Status ListInsert_L(LinkList *L, int i, ElemType e); Status ListDelete_L(LinkList *L, int i, ElemType *e); Status DisplayList_L(LinkList *L); Status GetElem_L(LinkList L, int pos, ElemType *e); Status LocateElem_L(LinkList L,int *pos, ElemType e); Status MergeList_L(LinkList *La, LinkList *Lb, LinkList *Lc); int main() { LinkList La, Lb, Lc; int countA,countB; printf_s("创建链表 La \n"); InitList_L(&La); printf_s("请输入La中元素的个数:"); scanf_s("%d", &countA); CreateList_L(&La, countA); DisplayList_L(&La); printf_s("创建链表 Lb \n"); InitList_L(&Lb); printf_s("请输入Lb中元素的个数:"); scanf_s("%d", &countA); CreateList_L(&Lb, countA); DisplayList_L(&Lb); printf_s("合并链表 La Lb \n"); InitList_L(&Lc); MergeList_L(&La, &Lb, &Lc); DisplayList_L(&Lc); system("pause"); } Status InitList_L(LinkList *L) { *L = (LinkList)malloc(sizeof(LNode)); if(!(*L)) { exit(0); } (*L)->Next = NULL; printf_s("链表初始化成功\n"); return OK; }// InitList_L Status CreateList_L(LinkList *L, int n) { int i; LinkList p; for (i = n; i > 0; --i) { p = (LinkList) malloc(sizeof(LNode)); printf_s("请输入第 %d 个元素:", i); scanf_s("%d", &(p->data)); p->Next = (*L)->Next; (*L)->Next = p; } return OK; }// CreateList_L Status ListInsert_L(LinkList *L, int i, ElemType e) { LinkList s, p; int j; p = *L; j = 0; //寻找第 i-1 个节点 while (p && j < i - 1) { p = p->Next; ++j; } if (!p || j > i - 1) { return ERROR; } s = (LinkList) malloc(sizeof(LNode)); s->data = e; s->Next = p->Next; p->Next = s; return OK; }// ListInsert_L Status ListDelete_L(LinkList *L, int i, ElemType *e) { LinkList p, q; int j; p = *L; j = 0; while ( p->Next && j < i - 1) { p = p->Next; ++j; } if (!(p->Next) || j > i - 1) { return ERROR; } q = p->Next; p->Next = q->Next; e = &(q->data); free(q); return OK; }// ListDelete_L Status DisplayList_L(LinkList *L) { LinkList p = (*L)->Next; while (p != NULL) { printf_s("%d ", p->data); p = p->Next; } printf_s("\n"); return OK; }// DisplayList_L Status GetElem_L(LinkList L, int pos, ElemType *e) { LinkList p; int j; p = L->Next; j = 1; while (p && j < pos) { p = p->Next; ++j; } if (!p || j < pos) { return ERROR; } e = &(p->data); return OK; }// GetElem_L Status LocateElem_L(LinkList L,int *pos, ElemType e) { LinkList p; int i = 1; p = L->Next; while(p && (p->data != e)) { p = p->Next; ++i; } if(!p) { return ERROR; } *pos = i; return OK; }// LocateElem_L Status DestroyList_L(LinkList L) //销毁链表 { LinkList p ; while(L) { p = L; L = L->Next; free(p); } L = NULL; return OK; }// DestroyList_L Status MergeList_L(LinkList *La, LinkList *Lb, LinkList *Lc) { LinkList pa, pb, pc; pa = (*La)->Next; pb = (*Lb)->Next; *Lc = pc = *La; while (pa && pb) { //printf_s("pa->data:%d pb->data:%d\n",pa->data,pb->data); 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); return OK; }// MergeList_L