数据结构(严蔚敏)之二——链表的c语言实现
生活随笔
收集整理的這篇文章主要介紹了
数据结构(严蔚敏)之二——链表的c语言实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
介紹:
1、構造一個長為n的線性表,插入元素為逆序插入
2、構造一個長為n的線性表,插入元素為順序插入
3、銷毀鏈表L
4、查找L的第i個元素,并用e返回
5、查找L中第一個與e滿足compare關系的元素,若存在返回其對應位置,否則返回error
6、若cur_e是L中的元素,且不是第一個,則返回其前驅元素
7、若cur_e是L中的元素,且不是最后一個,返回其對應后繼元素
8、在L的第i個位置插入元素e
9、刪除L的第i個元素
鏈表結構的實現:
#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <assert.h>typedef int Elemtype; typedef int Status; #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OVERFLOW -1typedef struct Node {Elemtype data;struct Node * next; }LinkNode,*LinkList;/* 構造一個空的線性表 */ Status InitList(LinkList &L) {L=(LinkList)malloc(sizeof(LinkNode));if(!L){printf("L failed");return ERROR;}L->next=NULL;return OK; }/* 構造一個長為n的線性表,插入元素為逆序插入 */ Status ListCreate_Invert(LinkList &L,const int n) {int i;InitList(L);LinkList p;for(i=0;i<n;i++){p=(LinkList)malloc(sizeof(LinkNode));if(!p){printf("p failed ");return ERROR;}scanf("%d", &p->data);p->next=L->next;L->next=p;}return OK; }/* 構造一個長為n的線性表,插入元素為順序插入 */ Status ListCreate_Sequence(LinkList &L,const int n) {int i;InitList(L);LinkList p,q=L;for(i=0;i<n;i++){p=(LinkList)malloc(sizeof(LinkNode));if(!p){printf("p failed ");return ERROR;}scanf("%d", &p->data);q->next=p;q=p;}q->next=NULL;return OK; }/* 銷毀線性表L */ Status DestroyList(LinkList &L) {LinkList p=L->next,q;free(L);while(p){q=p->next;free(p);p=q;}return OK; }/* 將L重置為空表 */ Status ClearList(LinkList &L) {LinkList p=L->next,q;while(p){q=p->next;free(p);p=q;}L->next=NULL;return OK; }/* 判斷L是否為空,空返回True,否則返回False */ Status EmptyList(const LinkList L) {if(L->next==NULL)return TRUE;elsereturn FALSE; }/* 返回L的長度 */ Status ListLength(const LinkList L) {LinkList p=L;int length=0;while(p->next){++length;p=p->next;}return length; }/* 查找L的第i個元素,并用e返回 */ Status GetElem(const LinkList L,int i,Elemtype &e) {assert(i>0);int j=0;LinkList p=L;while (j<i&&p){++j;p=p->next;}if(p==NULL||j>i){printf("there aren't %d elements is the linklist\n", i);return FALSE;}e=p->data;return OK; }/* 查找L中第一個與e滿足compare關系的元素,若存在返回其對應位置,否則返回error */ Status LocateElem(const LinkList L,Elemtype e,Status (*compare)(Elemtype,Elemtype)) {LinkList p=L->next;int pos=1;while(p){if(compare(e,p->data)==1)return pos;else{pos++;p=p->next;}}if(p==NULL){printf("no element has the compare relation with %d \n", e);return FALSE;} }//若cur_e是L中的元素,且不是第一個,則返回其前驅元素 Status PriorElem(const LinkList L,Elemtype cur_e,Elemtype &pre_e) {int pos=1;LinkList p=L;while (p->next&&p->next->data!=cur_e){p=p->next;pos++;}if(pos!=1) //不是第一個結點{pre_e=p->data;return OK;}else{printf("%d is the first element of linklist\n", cur_e);return ERROR;} }//若cur_e是L中的元素,且不是最后一個,返回其對應后繼元素 Status NextElem(const LinkList L,Elemtype cur_e,Elemtype &next_e) {int pos=1;LinkList p=L->next;while(p&&p->data!=cur_e){ p=p->next;pos++;}if(p->next!=NULL){next_e=p->next->data;return OK;}else{printf("%d is the last element of linklist\n", cur_e);return ERROR;} }//在L的第i個位置插入元素e Status InsertList(LinkList &L,int i,Elemtype e) {assert(i>0);LinkList p=L->next;int j=1;while(p&&j<i-1){p=p->next;++j;}LinkList s=(LinkList)malloc(sizeof(LinkNode));s->data=e;s->next=p->next;p->next=s;return OK; }/* 刪除L的第i個元素 */ Status DeleteList(LinkList &L,int i,Elemtype &e) {assert(i>0);LinkList p=L->next;int j=1;while(p->next&&j<i-1){p=p->next;j++;}LinkList q=p->next;p->next=q->next;e=q->data;free(q);return OK; }/* 對L中的每一個元素調用visit函數 */ Status TraverseList(const LinkList L,void(*visit)(Elemtype)) {LinkList p;p=L->next;while(p){visit(p->data);p=p->next;}return OK; }void visit(Elemtype e) {printf("%-5d", e); }
測試代碼:
//測試代碼 int main() {LinkList L;int n;LinkNode *p,*emin,*emax;printf("請輸入要要輸入的元素個數:");scanf("%d", &n);ListCreate_Sequence(L, n);printf("原序鏈表值:\n");p = L->next;while(p){printf("%d ", p->data);p = p->next;}printf("\n");p = L;int ce, pe;printf("請輸入要查找前驅和后繼的元素:");scanf("%d", &ce);PriorElem(L, ce, pe);printf("%d的前驅是%d\n", ce, pe);NextElem(L, ce, pe);printf("%d的后繼是%d\n", ce, pe); printf("請輸入要插入的元素和插入位置:");scanf("%d%d", &ce,&pe);InsertList(L, pe,ce);printf("插入元素%d后的線性表為\n", ce);TraverseList(L, visit);puts("");printf("請輸入要刪除元素位置:");scanf("%d", &ce);DeleteList(L, ce, pe);printf("刪除%d位置的元素后的線性表為\n", ce);TraverseList(L, visit);puts("");DestroyList(L);return 0; }
運行結果:
總結
以上是生活随笔為你收集整理的数据结构(严蔚敏)之二——链表的c语言实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构(严蔚敏)之一——顺序表之c语言
- 下一篇: 保险极客CTO叶晖谈企业团体险的星辰大海