您的位置:首页 > 其它

单链表的基本操作(尾插,尾删,头插,头删,查找,指定位置插入、删除,指定元素删除、全部删除等)

2017-08-01 13:53 736 查看
LinkList.h

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

typedef int DataType;
typedef struct Node
{
DataType data;
struct Node *next;
}Node, *pNode,*pList;

void InitLinkLinst(pList* pplist);//初始化链表
void DestroyList(pList* pplist);//销毁

void PushBack(pList* pplist, DataType d);//尾插
void PopBack(pList* pplist);//尾删

void Display(const pList pplist);//打印
void PushFront(pList* pplist, DataType d);//头插
void PopFront(pList* pplist);//头删

pNode Find(pList* pplist, DataType d);//查找
void Insert(pList* pplist, DataType d, int pos);//指定位置插入
void Erase(pList* pplist, int pos);//删除指定位置

void Remove(pList* pplist, DataType d);//删除指定元素
void RemoveAll(pList* pplist, DataType d);//删除指定所有元素

#endif //__LINKLIST_H__


LinkList.c
#include "LinkList.h"

void InitLinkLinst(pList *pplist)//初始化
{
*pplist = NULL;
}

void PushBack(pList* pplist, DataType d)//尾插
{
Node* cur = *pplist;

Node *p = malloc(sizeof(Node));//动态开辟一块空间存放插入元素
memset(p, 0, sizeof(Node));
p->data = d;
p->next = NULL;

if (cur == NULL)//空链表
{
*pplist = p;
}
else //非空链表
{
while (cur->next != NULL)
{
cur = cur->next;//找到尾部
}
cur->next = p;
}
}

//void Display(pList* pplist)//循环打印
//{
// Node* cur = pplist;
//
// for (; cur != NULL; cur = cur->next)
// {
// printf("%d ",cur->data);
// }
// printf("\n");
//}

void Display(const pList pplist)//递归打印
{
if (pplist == NULL)
{
printf("\n");
}
else
{
printf("%d-->", pplist->data);
Display(pplist->next);
}
}

void PopBack(pList* pplist)//尾删
{
Node* cur = *pplist;
Node* previous = NULL;//用来记录要删除的前一个位置

if (cur == NULL)//空链表
{
return;
}
while (cur->next != NULL)
{
previous = cur;
cur = cur->next;
}
if (previous != NULL)//多于一个节点
{
previous->next = NULL;
free(cur);
}
else//只有一个节点
{
free(cur);
*pplist = NULL;
}
}

void PushFront(pList* pplist, DataType d)//头插
{
Node* cur = *pplist;

Node *p = malloc(sizeof(Node));//动态开辟一块内存用来存放插入的元素
memset(p, 0, sizeof(Node));
p->data = d;
p->next = NULL;

p->next = *pplist;//将插入元素的next指针指向原链表的头结点
*pplist = p;//链表重置后的新的头结点
}

void PopFront(pList* pplist)//头删
{
Node* cur = *pplist;

if (cur == NULL)//空链表
{
return;
}
else //非空链表
{
*pplist = cur->next;//链表重置后的新的头结点
free(cur);
}
}

pNode Find(pList* pplist, DataType d)//查找元素,若找到了返回位置,没找到返回空指针
{
Node* cur = *pplist;

while (cur != NULL)
{
if (cur->data == d)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;
}

void Insert(pList* pplist, DataType d, int pos)//指定位置插入
{
int i = 0;
Node* cur = *pplist;
Node* previous = NULL;

Node *p = malloc(sizeof(Node));//动态开辟一块内存用来存放插入的元素
memset(p, 0, sizeof(Node));
p->data = d;
p->next = NULL;

if (cur == NULL)//空链表
{
*pplist = p;//链表重置后的新的头结点
return;
}
for (i = 0; (i < pos)&&(cur->next != NULL); i++)//找到指定位置
{
previous = cur;
cur = cur->next;
}
if (previous == NULL)//在最前边插入
{
p->next = cur;
*pplist = p;
}
else
{
p->next = cur;
previous->next = p;
}

}

void Erase(pList* pplist, int pos)//删除指定位置
{
int i = 0;
Node* cur = *pplist;
Node* previous = NULL;

if (cur == NULL)//空链表
{
return;
}
while ((i < pos) && (cur->next != NULL))
{
previous= cur;
cur = cur->next;
i++;
}
if (i < pos)//表示从(cur->next != NULL)的条件退出,说明节点个数小于pos
return;
if (previous == NULL)//删除第一个节点
{
*pplist = cur->next;
free(cur);
}
else //删除普通节点
{
previous->next = cur->next;
free(cur);
}
}

void Remove(pList* pplist, DataType d)//删除指定元素
{
Node* cur = *pplist;
Node* previous = NULL;

if (cur == NULL)//空链表
return;

if ((*pplist)->data == d)//删除元素为头结点
{
*pplist = (*pplist)->next;//链表重置后的新的头结点
return;
}
while (cur != NULL)
{
if (cur->data == d)
{
previous->next = cur->next;
cur = cur->next;
free(cur);
cur =NULL;
return;
}
else
{
previous = cur;
cur = cur->next;
}

}
}

void RemoveAll(pList* pplist, DataType d)//删除指定所有元素
{
Node* cur = *pplist;
Node* tmp = NULL;

if (cur == NULL)//空链表
return;
while (cur!= NULL)
{
if ((*pplist)->data == d)//删除元素为头结点
{
*pplist = (*pplist)->next;
cur = *pplist;
continue;
}
if (cur->data == d)
{
cur = cur->next;
free(tmp->next);
tmp->next=cur;
continue;
}
tmp = cur;
cur = cur->next;
}
}


test.c

#include "LinkList.h"

test()
{
Node *head = NULL;
InitLinkLinst(&head);//初始化

PushBack(&head, 1);//尾插1,2,3
PushBack(&head, 2);
PushBack(&head, 3);
Display(head);//打印

PopBack(&head);//尾删3
Display(head);//打印

PushFront(&head, 2);//头插2
Display(head);//打印

PopFront(&head);//头删2
Display(head);//打印

Insert(&head, 5, 1);//将5插入第1个位置
Display(head);//打印

Erase(&head, 0);//删除第0个节点
Display(head);//打印

PushBack(&head, 1);//尾插1,2,3
PushBack(&head, 2);
PushBack(&head, 5);
Remove(&head, 5);//删除指定元素
Display(head);//打印

RemoveAll(&head, 2);
Display(head);//打印

int *p = (int *)Find(&head, 2);//查找
int c = 0;
if (p == NULL)
{
printf("没有找到\n");
}
else
{
printf("找到了\n");
}
}
}
int main()
{
test();
system("pause:");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐