标签:
LinkList.h
#pragma once #include<stdio.h> #include<assert.h> #include<malloc.h> typedef int DataType; typedef struct Node { DataType data; struct Node* next; }Node, *plist; plist _CreateNode(DataType x); void InitLinklist(plist* pplist); void PrintLinklist(plist list); void PushBack(plist* pplist, DataType x); int GetLength(plist list); void PushFront(plist* pplist, DataType x); void PopFront(plist* pplist); void PopBack(plist* pplist); Node* Find(plist list, DataType x); void Insert(plist* pplist, Node * n, DataType x); void Remove(plist* pplist, Node * n); int Erase(plist* pplist, DataType x); int EraseAll(plist* pplist, DataType x, DataType all); void DeleteNode(Node* n); void InsertFront(Node* n, DataType x); void PrintEndToBegin(plist list);
LinkList.c
#include"LinkList.h" plist _CreateNode(DataType x) { plist tmp = (plist)malloc(sizeof(Node)); tmp->data = x; tmp->next = NULL; return tmp; } void InitLinklist(plist* pplist) { assert(pplist); *pplist = NULL; } void PrintLinklist(plist list) { plist begin = list; while (begin != NULL) { printf("%d->", begin->data); begin = begin->next; } printf("NULL\n"); } void PushBack(plist* pplist, DataType x) { assert(pplist); if (*pplist == NULL) { (*pplist) = _CreateNode(x); } else { plist tmp = *pplist; plist endNode = _CreateNode(x); while (tmp->next != NULL) { tmp = tmp->next; } tmp->next = endNode; tmp = endNode; } } void PushFront(plist* pplist, DataType x) { assert(pplist); if (*pplist == NULL) { (*pplist) = _CreateNode(x); } else { plist s = _CreateNode(x); s->next = *pplist; *pplist = s; } } int GetLength(plist list) { int len = 0; while (list->next != NULL) { len++; list = list->next; } return len+1; } void PopFront(plist* pplist) { assert(pplist); if ((*pplist) == NULL) { printf("The Link Is Empty\n"); } else { *pplist = (*pplist)->next; } } void PopBack(plist* pplist) { assert(pplist); if ((*pplist) == NULL) { printf("The Link Is Empty\n"); } if ((*pplist)->next == NULL) { free(*pplist); *pplist = NULL; } else { plist prev = 0, end; end = *pplist; while (end->next != NULL) { prev = end; end = end->next; } free(end); prev->next=NULL; } } //返回值为NULL时,找不到此元素或者链表为空;反之,返回值为指向x的指针 Node* Find(plist list, DataType x) { plist begin = list; while (begin != NULL) { if (begin->data == x) { return begin; } else { begin = begin->next; } } return NULL; } void Insert(plist* pplist, Node * n, DataType x) { assert(pplist); //空表时插入值 if (*pplist == NULL) { *pplist = _CreateNode(x); } n = Find(*pplist, 6); if (n == NULL) { printf("此链表中没有此元素\n"); } else { plist anyNode = _CreateNode(x); //一个节点时插入值 if (n->next == NULL) { n->next = anyNode; } //多个节点时插入值 else { anyNode->next = n->next; n->next = anyNode; } } } int Erase(plist* pplist, DataType x) { assert(pplist); Node* n; if (*pplist == NULL) { printf("The Link Is Empty\n"); } n = Find(*pplist, x); if (n == NULL) { printf("此链表中没有此元素\n"); } else { plist prev=0,tmp=0; tmp = *pplist; //删除头结点及包括处理只有一个节点时的情况 if ((*pplist)->data == x) { *pplist = (*pplist)->next; free(tmp); return 0; } //处理有多个节点的情况 while (tmp != NULL && tmp->data != x) { prev = tmp; tmp = tmp->next; } prev->next = tmp->next; free(tmp); } return 0; } void Remove(plist* pplist, Node * n) { assert(pplist); if (*pplist == NULL) { printf("The Link Is Empty\n"); } n = Find(*pplist, 9); if (n == NULL) { printf("此链表中没有此元素\n"); } else { plist pre = 0, tmp; tmp = *pplist; if (*pplist == n) { *pplist = (*pplist)->next; free(tmp); return; } while (tmp != NULL && tmp != n) { pre = tmp; tmp = tmp->next; } pre->next = tmp->next; free(tmp); } } int EraseAll(plist* pplist, DataType x,DataType all) { assert(pplist); Node* n; if (*pplist == NULL) { printf("The Link Is Empty\n"); } n = Find(*pplist, 5); if (n == NULL) { printf("此链表中没有此元素\n"); } else { plist prev = 0, tmp = 0; tmp = *pplist; //删除一个x时的情况 if (all == 0) { if ((*pplist)->data == x) { *pplist = (*pplist)->next; free(tmp); return 0; } //处理有多个节点的情况 while (tmp != NULL && tmp->data != x) { prev = tmp; tmp = tmp->next; } prev->next = tmp->next; free(tmp); } //删除多个x的情况 else { plist ptmp; //删除且为头结点的情况 if ((*pplist)->data == x) { *pplist = (*pplist)->next; free(tmp); } ptmp = *pplist; while (ptmp != NULL) { while (ptmp != NULL && ptmp->data != x) { prev = ptmp; ptmp = ptmp->next; } if (ptmp == NULL) { break; } prev->next = ptmp->next; free(ptmp); ptmp = NULL; ptmp = *pplist; } } } return 0; } void DeleteNode(Node* n) { if (n == NULL) { printf("Node Is NULL\n"); } if (n->next == NULL) { PopBack(&n); } else { plist del = n->next; n->data = del->data; n->next = del->next; free(del); } } void InsertFront(Node* n, DataType x) { Node* tmp = n; DataType ptmp = 0; if (tmp == NULL) { printf("Node Is NULL\n"); } else { Node* begin = _CreateNode(x); begin->next = tmp->next; tmp->next = begin; ptmp = tmp->data; tmp->data = begin->data; begin->data =ptmp; } } void PrintEndToBegin(plist list)//递归 { if (list == NULL) { return; } else { PrintEndToBegin(list->next); printf("%d->", list->data); } } void Test() { plist s; Node* pFind ; int ret = 0; InitLinklist(&s); PushBack(&s, 1); PushBack(&s, 2); PushBack(&s, 3); PrintLinklist(s); PushFront(&s, 1); PushFront(&s, 4); PushFront(&s, 5); PushFront(&s, 5); PushFront(&s, 6); PushFront(&s, 5); PushFront(&s, 1); PrintLinklist(s); ret = GetLength(s); printf("链表的长度为:%d\n", ret); PopFront(&s); PrintLinklist(s); PopBack(&s); PrintLinklist(s); PopFront(&s); pFind = Find(s, 6); Insert(&s, pFind, 9); PrintLinklist(s); pFind = Find(s, 9); Remove(&s, pFind); PrintLinklist(s); Erase(&s, 4); PrintLinklist(s); pFind = Find(s, 5); DeleteNode(pFind); PrintLinklist(s); pFind = Find(s, 3); InsertFront(pFind, 109); PrintLinklist(s); EraseAll(&s, 5,1); PrintLinklist(s); PrintEndToBegin(s); } void main() { Test(); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/kkmdmcgxi/article/details/46900147