单链表实现(LinkedList) for C
// // LinkedList.hpp // test1 // // Created by Zy on 2020/3/28. // Copyright © 2020 Jovan. All rights reserved. // #ifndef LinkedList_hpp #define LinkedList_hpp #include <stdlib.h> #include <memory.h> #include <stdio.h> #define MAXLEN 100 #define OK 1 #define ERROR 0 #define OVERFLOW -1 typedef int Status; typedef char ElemType; typedef struct node{ ElemType data; struct node *next=NULL; }LinkedList; //初始化、创建->插入,删除,查找,读取->清空 LinkedList * InitList(); void CreateList(LinkedList *);//尾插入构建list int GetLength(LinkedList *); LinkedList *Locate(LinkedList *, ElemType );//查找元素位置f,返回一个指针 LinkedList *GetAt(LinkedList *,int);//按照index查找,下标从1开始 Status InsertElem(LinkedList *, ElemType);//按照指针位置插入元素(如果参数是头指针,则是首插入,可以通过p=p->next改变插入位置) Status InsertElemAt(LinkedList *, int, ElemType);//按序插入 Status DeleteElem(LinkedList *,ElemType *);//删除指针位置后第一个结点,并得到该点的值x Status DeleteElemAt(LinkedList *, int ,ElemType *);//按序删除 void Erase(LinkedList *); void PrintList(LinkedList *); #endif /* LinkedList_hpp */
// // LinkedList.cpp // test1 // // Created by Zy on 2020/3/28. // Copyright © 2020 Jovan. All rights reserved. // #include "LinkedList.hpp" LinkedList *InitList(){ LinkedList *L; L = (LinkedList *)malloc(sizeof(LinkedList)); L->next = NULL; return L; } void CreateList(LinkedList *L){ LinkedList *s,*last; char ch; last=L; while((ch=getchar())!='#'){ s = (LinkedList *)malloc(sizeof(LinkedList)); s->data = ch; //构建一个结点 s->next = NULL; last->next = s; last=s; } } int GetLength(LinkedList *L){ LinkedList *p; p=L; int cnt=0; while(p->next!=NULL){ p=p->next; cnt++; } return cnt; } LinkedList *Locate(LinkedList *L, ElemType x){ LinkedList *p; p=L->next; while(p!=NULL&&p->data!=x){ p=p->next; } return p; } LinkedList *GetAt(LinkedList *L,int index){ LinkedList *p = L; int i; for(i=0;i<index;i++) if(p->next) p = p->next; else return NULL; return p; } Status InsertElem(LinkedList *p, ElemType x){ if(!p) return ERROR; LinkedList *q=(LinkedList *)malloc(sizeof(LinkedList)); q->data = x; q->next = p->next; p->next=q; return OK; } Status InsertElemAt(LinkedList *L, int index, ElemType x){ LinkedList *p = GetAt(L, index-1); if(!L) return ERROR; InsertElem(p, x); return OK; }//按位插入O(1),按序插入O(n) Status DeleteElem(LinkedList *L,ElemType *x){ LinkedList *p = L->next; if(!p) return ERROR; *x = p->data; L->next=p->next; free(p); return OK; }//删除指针后一个元素,并参数返回该点值 Status DeleteElemAt(LinkedList *L, int index,ElemType *x){ LinkedList *p = GetAt(L, index-1); if(p) return DeleteElem(p, x); else return ERROR; } void Erase(LinkedList *L){ LinkedList *p = L->next; LinkedList *t = L->next; while(p){ t = p->next; //printf("%c;",p->data); free(p); p=t; } L->next = NULL; } void PrintList(LinkedList *L){ LinkedList *p = L; while(p->next){ p = p->next; printf("%c ",p->data); } printf("\n"); }
测试代码:
LinkedList *L1 = InitList(); //利用初始化函数构建实例; LinkedList L2; CreateList(&L2);//输入 abcdef# PrintList(&L2); InsertElem(&L2,'0'); PrintList(&L2); InsertElemAt(&L2, 2, 's'); PrintList(&L2); char x=' '; DeleteElem(&L2, &x); PrintList(&L2); DeleteElemAt(&L2, 3, &x); PrintList(&L2); printf("长度为%d\n",GetLength(&L2)); Erase(&L2); free(L1);
abcdef#
a b c d e f
0 a b c d e f
0 s a b c d e f
s a b c d e f
s a c d e f
长度为6