- 首页 > it技术 > >
//.h
#ifndef__LINK_LIST__
#define__LINKLIST__
#include.h>
#include
#include.h>
typedef int DateType;
typedef struct Node
{
DateType data;
struct Node* next;
}Node,*pNode,list;
//初始化链表和销毁链表
void InitLinkList(list** pplist);
void Destroylist(list**pplist);
//尾部插入和删除元素
void pushback(list**pplist,DateType x);
void popback(list**pplist);
//头部插入和删除元素
void pushfront(list**pplist, DateType x);
void popfront(list**pplist);
//查找
pNode find(list *plist, DateType x);
//指定位置插入和删除
void insert(list **pplist,int pos, DateType x);
void erase(list**pplist, int pos);
//删除指定元素
void Remove(list**pplist, DateType x);
//删除指定所有元素
void Removeall(list**pplist, DateType x);
//遍历链表
void display(list* plist);
#endif
//.c
#include"linklis.h"
void display(list* plist)
{
assert(plist);
while (plist != NULL)
{
printf("%d ", plist->data);
plist = plist->next;
}
printf("\n");
}void InitLinkList(list** pplist)
{
assert(pplist);
*pplist = NULL;
}
void Destroylist(list**pplist)
{
assert(pplist);
while (*pplist != NULL)
{
pNode temp = (*pplist)->next;
free(*pplist);
*pplist = temp;
}
}
//尾插
void pushback(list**pplist,DateType x)
{
assert(pplist);
pNode head = *pplist;
pNode temp = (pNode)malloc(sizeof(Node));
if (!temp)
{
perror("malloc");
exit(EXIT_FAILURE);
}
temp->data = https://www.it610.com/article/x;
temp->next = NULL;
if (head == NULL)//空链表处理
{
*pplist = temp;
return;
}
while (head->next != NULL)//非空链表处理
{
head = head->next;
}
head->next = temp;
}
//尾删
void popback(list**pplist)
{
assert(pplist);
pNode head = *pplist;
pNode ptemp = NULL;
if (*pplist == NULL)//空链表处理
return;
if ((*pplist)->next == NULL)//一个节点的处理
{
free(*pplist);
*pplist = NULL;
return;
}
while (head->next != NULL)//多节点处理
{
ptemp = head;
head = head->next;
}
free(ptemp->next);
ptemp->next = NULL;
}
//头插
void pushfront(list**pplist,DateType x)
{
assert(pplist);
pNode node = (pNode)malloc(sizeof(Node));
if (node == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
node->data = https://www.it610.com/article/x;
node->next = NULL;
node->next = *pplist;
*pplist = node;
}
//头删
void popfront(list**pplist)
{
assert(pplist);
if (*pplist == NULL)
return;
pNode temp=NULL;
temp = *pplist;
*pplist = (*pplist)->next;
free(temp);
}
//查找
pNode find(list *plist, DateType x)
{
assert(plist);
pNode head = plist;
while (head != NULL)
{
if (head->data =https://www.it610.com/article/= x)
return head;
head = head->next;
}
return NULL;
}
//指定位置插入,头节点位置为1
void insert(list **pplist, int pos, DateType x)
{
assert(pplist);
int i = 1;
pNode head = *pplist;
pNode temp = (pNode)malloc(sizeof(Node));
if (temp == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
temp->data = https://www.it610.com/article/x;
temp->next = NULL;
if (head == NULL)//空链表处理
{
*pplist = temp;
return;
}
if (pos <= 1)//插入位置为1或小于1处理
{
temp->next = *pplist;
*pplist = temp;
return;
}
while( (i < pos-1)&&(head->next != NULL))//插入位置大于1处理
{
head = head->next;
i++;
}
temp->next = head->next;
head->next = temp;
}
//删除指定位置,头节点位置为1
void erase(list**pplist, int pos)
{
assert(pplist);
pNode head = *pplist;
pNode temp = *pplist;
int i = 1;
//空链表处理
if (head == NULL)
return;
//一个节点处理
if (head->next == NULL)
{
free(head);
*pplist = NULL;
return;
}
//删除位置小于等于1时处理
if (pos <= 1)
{
temp = (*pplist)->next;
free(*pplist);
*pplist = temp;
return;
}
//删除位置大于1时处理
while (i < pos && head->next!= NULL)
{
temp = head;
head = head->next;
i++;
}
head = temp->next->next;
free(temp->next);
temp->next = head;
}
//删除指定元素
void Remove(list**pplist, DateType x)
{
assert(pplist);
pNode head = *pplist;
pNode temp = NULL;
//空链表时处理
if (head == NULL)
return;
//当指定元素为头节点时处理
if ((*pplist)->data =https://www.it610.com/article/= x)
{
*pplist = (*pplist)->next;
return;
}
//当指定元素为非头节点时处理
while (head!= NULL)
{
if (head->data =https://www.it610.com/article/= x)
{
head = head->next;
free(temp->next);
temp->next = head;
return;
}
temp = head;
head = head->next;
}
}
//删除指定所有元素
void Removeall(list**pplist, DateType x)
{
assert(pplist);
pNode head = *pplist;
pNode temp = NULL;
//空链表时处理
if (head == NULL)
return;
//删除指定所有元素,只要遍历该链表,然后删除指定元素
while (head!= NULL)
{
//删除的指定元素为头节点处理
if ((*pplist)->data =https://www.it610.com/article/= x)
{
*pplist = (*pplist)->next;
head = *pplist;
//结束本次循环
continue;
}
if (head->data =https://www.it610.com/article/= x)
{
//head的指向本节点的下一个节点
head = head->next;
//删除指定元素
free(temp->next);
//保存下一个节点的位置
temp->next = head;
continue;
}
//其他情况时
//使temp保存当前节点的位置
temp = head;
//使head指向下一个节点
head = head->next;
}
}
//.c
#include"linklis.h"
int main()
{
list*List;
InitLinkList(&List);
//pushback(&List, 1);
//display(List);
//pushback(&List, 2);
//display(List);
//pushback(&List, 3);
//display(List);
//pushback(&List, 4);
//display(List);
//pushback(&List, 5);
//display(List);
//popback(&List);
//display(List);
//popback(&List);
//display(List);
//popback(&List);
//display(List);
//popback(&List);
//display(List);
//popback(&List);
//display(List);
pushfront(&List, 1);
display(List);
pushfront(&List, 2);
display(List);
pushfront(&List, 3);
display(List);
pushfront(&List, 4);
display(List);
pushfront(&List, 5);
display(List);
/*popfront(&List);
display(List);
popfront(&List);
display(List);
popfront(&List);
display(List);
*/
//insert(&List, 3, 1);
//display(List);
//insert(&List, 9, 9);
//display(List);
//insert(&List, 7, 8);
//display(List);
//insert(&List, 1, 6);
//display(List);
//erase(&List, 1);
//display(List);
//erase(&List, 7);
//display(List);
//erase(&List, 9);
//display(List);
//erase(&List, 3);
//display(List);
//pushfront(&List, 1);
//display(List);
//pushfront(&List, 1);
//display(List);
//pushfront(&List, 1);
//display(List);
//pushfront(&List, 1);
//display(List);
//pushfront(&List, 1);
//display(List);
//pushfront(&List, 1);
//display(List);
//Removeall(&List, 1);
//display(List);
//Remove(&List, 3);
//display(List);
//Remove(&List, 2);
//display(List);
//Remove(&List, 4);
//display(List);
//Remove(&List, 5);
//display(List);
/*if (find(List, 2) != NULL)
printf("找到了\n");
else
printf("没有找到\n");
*/
Destroylist(&List);
system("pause");
return 0;
}
推荐阅读