码迷,mamicode.com
首页 > 其他好文 > 详细

链表的实现

时间:2016-02-27 01:06:20      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:链表

“link.h”

#ifndef __LINK_LIST_H__

#define __LINK_LIST_H__


#include <stdio.h>

#include <assert.h>

#include <malloc.h>

#include <stdlib.h>


typedef int DataType;


typedef struct LinkNode

{

DataType data;

struct LinkNode *next;

}LinkNode, *pLinkNode, *pList;


void InitLinkList(pList* pHead);//初始化

void Destroy(pList *pHead);//销毁

void PushBack(pList* pHead, DataType x);//尾插

void PopBack(pList* pHead);//尾删

void PushFront(pList* pHead, DataType x);//头插

void PopFront(pList* pHead);//头删

void PrintList(pList list);//打印


int GetListLength(pList head);//获取链表长度

pLinkNode Find(pList head, DataType x);//查找某一节点

void Insert(pList *pHead, pLinkNode pos, DataType x);//在某个位置上插入某个元素

void Remove(pList *pHead, DataType x);//删除某一元素

void RemoveAll(pList *pHead, DataType x);//删除所有x元素

void Erase(pList *pHead, pLinkNode pos);//删除某一位置上的元素


void InitLinkList(pList* pHead)//初始化

{

assert(pHead);

*pHead = NULL;

}


pLinkNode buyNode(DataType x)//构造节点

{

pLinkNode newNode = (pLinkNode)malloc(sizeof(LinkNode));

newNode->data = x;

newNode->next = NULL;

return newNode;

}


void PushBack(pList* pHead, DataType x)//尾插

{

pLinkNode newNode;

pLinkNode cur = *pHead;

assert(pHead);

newNode = buyNode(x);

if(*pHead == NULL)

{

*pHead = newNode;

return;

}

else

{

while(cur->next)

{

cur = cur->next;

}

cur->next = newNode;

}


}


void PrintList(pList list)//打印

{

pList cur = list;

while(cur)

{

printf("%d ",cur->data);

cur = cur->next;

}

printf("over");

}


void PushFront(pList* pHead, DataType x)//头插

{

pLinkNode newNode;

pLinkNode cur = *pHead;

assert(pHead);

newNode = buyNode(x);

if(*pHead == NULL)

{

*pHead = newNode;

return;

}

else

{

newNode->next = *pHead;

*pHead = newNode;

}

}


void PopBack(pList* pHead)//尾删

{

pLinkNode cur = *pHead;

pLinkNode del;

assert(pHead);

if(*pHead == NULL)

{

printf("链表无元素\n");

return;

}

else if(cur->next == NULL)

{

cur = NULL;

return;

}

else

{

while(cur->next->next)

{

cur = cur->next;

}

del = cur->next;

cur->next = NULL;

free(del);

return;

}

}


void PopFront(pList* pHead)//头删

{

pLinkNode cur = *pHead;

pLinkNode del;

assert(pHead);

if(*pHead == NULL)

{

printf("链表无元素\n");

}

else if((*pHead)->next == NULL)

{

*pHead = NULL;

return;

}

else

{

*pHead = cur->next;

del = cur;

cur = NULL;

free(del);

}

}

int GetListLength(pList head)//长度

{

DataType count = 0;

pLinkNode cur = head;

while(cur)

{

count++;

cur = cur->next;

}

return count;

}

pLinkNode Find(pList head, DataType x)//查找某一节点

{

pLinkNode cur = head;

if(head == NULL)

{

printf("链表中无节点\n");

}

else 

{

while(cur)

{

if(cur->data == x)

return cur;

cur = cur->next;

}

return NULL;

}

}

void Insert(pList *pHead, pLinkNode pos, DataType x)//在某个位置上插入某个元素

{

pLinkNode cur = *pHead;

pLinkNode newNode = buyNode(x);

assert(pHead);

assert(pos);

if((*pHead) == NULL)

{

printf("链表为空\n");

return;

}

else if((*pHead) == pos) //插入位置为1

{

newNode->next = (*pHead);

*pHead = newNode;

}

else

{

while(cur)

{

if(cur->next == pos)

{

newNode->next = cur->next;

cur->next = newNode;

return;

}

cur = cur->next;

}

}

}

void Remove(pList *pHead, DataType x)//删除某一元素

{

pLinkNode cur = *pHead;

pLinkNode del;

assert(pHead);

if((*pHead) == NULL)

{

return;

}

else if((*pHead)->data == x)

{

del = *pHead;

*pHead = del->next;

free(del);

return;

}

else

{

while(cur)

{

if(cur->next->data == x)

  {

del = cur->next;

cur->next = del->next;

free(del);

del = NULL;

return;

  }

  cur = cur->next;

}

}

}

void Erase(pList *pHead, pLinkNode pos)//删除某一位置上的元素

{

pLinkNode cur = *pHead;

pLinkNode del;

assert(pHead);

if((*pHead) == NULL)

{

return;

}

else if((*pHead) == pos)

{

del = (*pHead);

*pHead = del->next;

free(del);

del = NULL;

return;

}

else

{

while(cur)

{

if(cur->next == pos)

{

del = cur->next;

cur->next = del->next;

free(del);

del = NULL;

return;

}

cur = cur->next;

}

}


}

void RemoveAll(pList *pHead, DataType x)

{

pLinkNode cur = *pHead;

pLinkNode prev,del;

assert(pHead);

while(cur)

{

if(cur->data == x)

{

del = cur;

if(cur == (*pHead))

{

*pHead = (*pHead)->next;

cur = *pHead;

}

else

{

prev->next = cur->next;

cur = prev->next;

}

free(del);

}

else

{

prev = cur;

   cur = cur->next;

}

}


}

#endif  //__LINK_LIST_H__


"test.c"

#include "link.h"

void Test1()//尾插

{

pList mylist;

InitLinkList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

PushBack(&mylist,3);

PushBack(&mylist,4);

PrintList(mylist);

}

void Test2()//头插

{

pList mylist;

InitLinkList(&mylist);

PushFront(&mylist,1);

PushFront(&mylist,2);

PushFront(&mylist,3);

PushFront(&mylist,4);

PrintList(mylist);

}

void Test3()//尾删

{

pList mylist;

InitLinkList(&mylist);

PushBack(&mylist,1);

//PushBack(&mylist,2);

        //PushBack(&mylist,3);

//PrintList(mylist);

PopBack(&mylist);

PrintList(mylist);

}

void Test4()//头删

{

pList mylist;

InitLinkList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

        PushBack(&mylist,3);

PopFront(&mylist);

PrintList(mylist);


}

void Test5()

{

pList mylist;

DataType ret = 0;

InitLinkList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

        PushBack(&mylist,3);

ret = GetListLength(mylist);

printf("%d ",ret);

}

void Test6()

{

pList mylist;

pLinkNode ret;

InitLinkList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

        PushBack(&mylist,3);

ret = Find(mylist,4);

if(ret != NULL)

{

printf("%d ",ret->data);

}

else

{

printf("链表中无此元素\n");

}

}

void Test7()

{

pList mylist;

pLinkNode ret;

InitLinkList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

        PushBack(&mylist,3);

        PushBack(&mylist,4);

        PushBack(&mylist,5);

ret = Find(mylist,1);

Insert(&mylist,ret,6);

PrintList(mylist);

}

void Test8()

{

pList mylist;

InitLinkList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

        PushBack(&mylist,3);

        PushBack(&mylist,4);

        PushBack(&mylist,5);

Remove(&mylist,3);

PrintList(mylist);

}

void Test9()

{

pList mylist;

InitLinkList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,3);

        PushBack(&mylist,2);

        PushBack(&mylist,1);

        PushBack(&mylist,3);

RemoveAll(&mylist,1);

PrintList(mylist);

}

void Test10()

{

pList mylist;

pLinkNode ret;

InitLinkList(&mylist);

PushBack(&mylist,1);

PushBack(&mylist,2);

        PushBack(&mylist,3);

        PushBack(&mylist,4);

        PushBack(&mylist,5);

ret = Find(mylist,4);

Erase(&mylist,ret);

PrintList(mylist);

}

int main()

{

Test9();

return 0;

}


链表的实现

标签:链表

原文地址:http://10810429.blog.51cto.com/10800429/1745434

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!