数据结构与算法分析-双向链表的实现

双向链表的实现,头文件:

 

#ifndef _List_H #define _List_H struct Node; typedef struct Node *PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; List InitList(); Position Find(int X, List L); void Delete(int X, List L); void InsertAfter(int X, List L, Position p); void DeleteList(List L); #endif

 

.c文件

 

#include <stdio.h> #include <stdlib.h> #include <assert.h> #include "doublelist.h" struct Node { int Element; Position Next; Position Pre; }; List InitList() { List temp; temp = (List)malloc(sizeof(struct Node)); temp->Next = NULL; temp->Pre = NULL; return temp; } int IsEmpty(List L) { assert(L!=NULL); return L->Next == NULL; } Position Find(int X, List L) { Position P; assert(L!=NULL); P = L; while( P != NULL && P->Element != X ) P = P->Next; return P; } void Delete(int X, List L) { Position P; assert(L!=NULL); if ( IsEmpty(L) ) return ; P = L; while( P != NULL && P->Element != X ) P = P->Next; P->Pre->Next = P->Next; if (P->Next != NULL) P->Next->Pre = P->Pre; free(P); } void InsertAfter(int X, List L, Position P) { Position TmpCell; TmpCell = malloc(sizeof(struct Node)); assert(TmpCell!=NULL); TmpCell->Element = X; TmpCell->Next = P->Next; TmpCell->Pre = P; if (P->Next != NULL) { P->Next = TmpCell; P->Next->Pre = TmpCell; } else P->Next=TmpCell; } void DeleteList(List L) { Position P, Tmp; assert(L!=NULL); P = L; P->Next = NULL; while( P!=NULL ) { Tmp = P->Next; free(P); P = Tmp; } } void PrintList(List L) { Position P; assert(L!=NULL); P = L->Next; while(P!=NULL) { printf("---%d---/n", P->Element); P = P->Next; } } int main(int argc, char **argv) { List demo = InitList(); InsertAfter(2, demo,demo); InsertAfter(5, demo, demo->Next); PrintList(demo); Delete(2, demo); PrintList(demo); DeleteList(demo); return 0; }

 

循环链表的实现,头文件

 

#ifndef _List_H #define _List_H struct Node; typedef struct Node *PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; List InitList(); Position Find(int X, List L); void Delete(int X, List L); void InsertAfter(int X, List L, Position p); void DeleteList(List L); #endif

 

.c文件

 

#include <stdio.h> #include <stdlib.h> #include <assert.h> #include "drlist.h" struct Node { int Element; Position Next; Position Pre; }; List InitList() { List temp; temp = (List)malloc(sizeof(struct Node)); temp->Next = temp; temp->Pre = temp; return temp; } int IsEmpty(List L) { assert(L!=NULL); return (L->Next == L && L->Pre == L); } Position Find(int X, List L) { Position P; assert(L!=NULL); P = L->Next; while( P != L && P->Element != X ) P = P->Next; return P; } void Delete(int X, List L) { Position P; assert(L!=NULL); if ( IsEmpty(L) ) return ; P = L->Next; while( P != L && P->Element != X ) P = P->Next; P->Pre->Next = P->Next; P->Next->Pre = P->Pre; free(P); } void InsertAfter(int X, List L, Position P) { Position TmpCell; TmpCell = malloc(sizeof(struct Node)); assert(TmpCell!=NULL); TmpCell->Element = X; TmpCell->Next = P->Next; TmpCell->Pre = P; if ( P->Next == L) L->Pre = TmpCell; P->Next = TmpCell; } void DeleteList(List L) { Position P, Tmp; assert(L!=NULL); P = L; P->Next = NULL; while( P!=L ) { Tmp = P->Next; free(P); P = Tmp; } } void PrintList(List L) { Position P; assert(L!=NULL); P = L->Next; while(P!=L) { printf("---%d---/n", P->Element); P = P->Next; } } int main(int argc, char **argv) { List demo = InitList(); InsertAfter(2, demo,demo); InsertAfter(5, demo, demo->Next); PrintList(demo); Delete(2, demo); PrintList(demo); DeleteList(demo); return 0; }

你可能感兴趣的:(数据结构,算法,struct,list,null,delete)