数据结构书中代码,未检验
头文件:
#ifndef List_H struct Node; typedef struct Node *PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; List MakeEmpty(List L); int IsEmpty(List L); int IsLast(Position p,List L); Position Find(int x,List L); void Delete(int x,List L); Position FindPrevious(int x,List L); void Insert(int x,List L,Position P); void DeleteList(List L); void show(List L); Position Header(List L); Position First(List L); Position Advance(Position P); #endif
list.cpp(函数接口问题,之前写过)
#include"list.h" #include<bits/stdc++.h> struct Node { int element; Position Next; }; int IsEmpty(List L) { return L->Next == NULL; } int IsLast(Position P,List L) { return P->Next==NULL; } /*返回x在l中的位置,找不到返回NULL */ Position Find(int x,List L) { Position P; P=L->Next; while(P!=NULL&&P->element!=x) P=P->Next; return P; } /*删除x在表中第一次出现*/ /*假如用一次头指针*/ void Delete(int x,List L) { Position P,TmpCell; P=FindPrevious(x,L); if(!IsLast(P,L))//x被找到,删除x; { TmpCell=P->Next; P->Next=TmpCell->Next; free(TmpCell); } } Position FindPrevious(int x,List L) { Position P; P=L; while(P->Next!=NULL && P->Next->element!=x) P=P->Next; return P; } void Insert(int x,List L,Position P) { Position TmpCell; TmpCell=(struct Node *)malloc( sizeof( struct Node) ); TmpCell->element =x; TmpCell->Next=P->Next; P->Next=TmpCell; }
完整版:
#ifndef List_H typedef int Elemtype; typedef struct LNode { Elemtype date; struct LNode *next; }LNode,*Linklist; void Initlist(Linklist L );//初始化单链表 bool Insert_head(Linklist L,Elemtype val);//头插法 bool Insert_tail(Linklist L,Elemtype val);//尾插法 bool Insert_pos(Linklist L,int pos, Elemtype val);//pos位置插入 LNode*Search(Linklist L,int key);//查找key的前驱 bool Delete(Linklist L,int key);//删除key结点 bool Is_empty(Linklist L);//是否为空 void Destroy(Linklist L);//摧毁函数 int Getlength(Linklist L);//得到单链表的长度 void Show(Linklist L);//打印单链表 #endif /* 函数的实现 list.cpp */ #include<stdio.h> #include"list.h" #include<assert.h> void Initlist(Linklist L)//初始化单链表 { assert(L!=NULL); L->next=NULL; } static LNode*GetNode(Elemtype val)//创建一个新结点 { LNode *pGet=(LNode*)malloc(10*sizeof(LNode)); assert(pGet!=NULL); pGet->date=val; pGet->next=NULL; return pGet; } bool Insert_head(Linklist L,Elemtype val)//头插法 { assert(L!=NULL); LNode *pGet=GetNode(val); pGet->next=L->next; L->next=pGet; return true; } bool Insert_tail(Linklist L,Elemtype val)//尾插法 { assert(L!=NULL); LNode *pGet=GetNode(val); LNode *p=L->next; while(p->next!=NULL) { p=p->next; } p->next=pGet; return true; } int Getlength(Linklist L)//得到单链表的长度 { int i=0; LNode *p=L->next; while(p!=NULL) { p=p->next; i++; } return i; } bool Insert_pos(Linklist L,int pos, Elemtype val)//pos位置插入 { assert(L!=NULL); if(pos<0||pos>Getlength(L)) { return false; } LNode *pGet=GetNode(val); LNode *p=L; int i=0; while(i!=pos) { p=p->next; } pGet->next=p->next; p->next=pGet; return true; } LNode*Search(Linklist L,int key)//查找key的前驱 { assert(L!=NULL); if(Is_empty(L)) { return NULL; } LNode *p=L; for(;p->next!=NULL;p=p->next) { if(p->next->date==key) return p; } return NULL; } bool Delete(Linklist L,int key)//删除key结点 { assert(L!=NULL); if(Is_empty(L)) { return false; } LNode *p=L; while(p->next!=NULL) { if(Search( L, key)!=NULL) { LNode *q=Search( L, key); p=q->next; q->next=p->next; p=q; } else { p=p->next; } } return true; } bool Is_empty(Linklist L)//是否为空 { if(L->next==NULL) return true; return false; } void Destroy(Linklist L)//摧毁函数 { assert(L!=NULL); LNode *p=NULL; while(L->next!=NULL) { p=L->next; L->next=p->next; free(p); } p=NULL; printf("okk\n"); } void Show(Linklist L)//打印单链表 { assert(L!=NULL); LNode *p=L->next; while(p!=NULL) { printf("%d ",p->date); p=p->next; } printf("\n"); } /* 主函数 */ #include<stdio.h> #include<stdlib.h> #include"list.cpp" int main() { LNode head; Initlist(&head); //头插 for(int i=0;i<5;i++) { Insert_head(&head,i); //Insert_tail(&head,i); } Show(&head); //尾插 for(int j=0;j<5;j++) { Insert_tail(&head,j); } Show(&head); //打印链表长度 printf("%d\n",Getlength(&head)); //位置插入 Insert_pos(&head,0, 11); Show(&head); //key值前驱的数据 printf("%d\n",Search(&head,0)->date); //删除某一元素 Delete(&head,4); Show(&head); //摧毁函数 Destroy(&head); system("pause"); return 0; }
原文:https://www.cnblogs.com/sweetlittlebaby/p/12919182.html