带头结点的链表的创建与操作

来源:互联网 发布:新浪邮箱smtp端口号 编辑:程序博客网 时间:2024/04/30 05:54

链表比数组的优势在于,它可提供高效的重排数据的能力,插入和删除操作比数组简单,因为在进行插入和删除过程中不需要移动收到影响的数据项之后的所有元素。

                        劣势在于:不能快速访问链表中的任意项。

 

list.c

#include<stdio.h>#include<stdlib.h>#include "list.h"List MakeEmpty(List L){if(L !=NULL)DeleteList(L);L=malloc(sizeof(struct Node));if(NULL==L)printf("out of memeory!");L->Next=NULL;return L;}///* Return true if L is empty */int IsEmpty(List L){return L->Next==NULL;}/* Return true if P is the last position in list L *//* Parameter L is unused in this implementation */int IsLast(Position P,List L){return P->Next==NULL;}/* Return Position of X in L; NULL if not found */Position Find(ElementType X,List L){Position P;P=L->Next;while(P){if(P->Element== X)break;P=P->Next;}return P;}void Delete(ElementType X,List L){Position P,TmpCell;P=FindPrevious(X,L);if(!IsLast(P,L)){TmpCell=P->Next;P->Next=TmpCell->Next;free(TmpCell);}}Position FindPrevious(ElementType X,List L){Position P;P=L;while(P->Next!=NULL && P->Next->Element!=X)P=P->Next;return P;}//将一个元素插入到由P所指示的位置之后void Insert(ElementType X,List L,Position P){Position TmpCell;TmpCell=malloc(sizeof(struct Node));if(TmpCell==NULL)printf("out of memory");TmpCell->Element=X;TmpCell->Next=P->Next;    P->Next=TmpCell;}void DeleteList(List L){Position P,Tmp;//L->Next=NULL;P=Advance(L);//p=L->Next;L->next=NULL;while(NULL!=P){      Tmp=P->Next;  free(P);  P=Tmp;}} Position Header(List L){return L;//返回头结点指针}Position Frist(List L){return L->Next;//返回头结点之后的第一个实结点的指针}Position Advance(Position P){   return P->Next;//返回指针p指向结点的下一个结点}ElementType Retrieve(Position P){return P->Element;}

list.h

 

typedef int ElementType;#ifndef _List_H#define _List_Hstruct Node;typedef struct Node *PtrToNode;typedef PtrToNode List;typedef PtrToNode Position;struct Node{ElementType Element;Position Next;};List MakeEmpty(List L);int IsEmpty(List L);int IsLast(Position P,List L);Position Find(ElementType X,List L);void Delete(ElementType X,List L);Position FindPrevious(ElementType X,List L);void Insert(ElementType X,List L,Position P);void DeleteList(List L);Position Header(List L);Position Frist(List L);Position Advance(Position P);ElementType Retrieve(Position P);#endif

main.c

/******************************************************************* *author:jia                                                        * *purpose:带有头结点的链表的创建与相关操作                             * *Date:2014.3.10                                                   * *******************************************************************/#include<stdio.h>#include "list.h"void PrintList(const List L){Position P = L->Next;if(IsEmpty(L))printf("Empty list!\n");while(P != NULL){printf("%d ",P->Element);P=P->Next;}    printf("\n");}int main(void){List L;Position P;int i;    L=MakeEmpty(NULL);P=Header(L);PrintList(L);for(i=0;i<10;i++){Insert(i,L,P);PrintList(L);P=Advance(P);}for(i=0;i<10;i+=2){Delete(i,L);}    if(NULL==Find(2,L)){printf("Find fails!\n");}    printf("Finished deletions\n");PrintList(L);DeleteList(L);    return 0;}



0 0