#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Node)
struct Node{
int data;
struct Node*next;
};
struct Node*createList()
{
struct Node*headnode=(struct Node*)malloc(LEN);
headnode->next=NULL;
return headnode;
}
struct Node*createNode(int data)
{
struct Node*newnode=(struct Node*)malloc(LEN);
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void insertnode(struct Node*headnode,int data)
{
struct Node*node=createNode(data);
node->next=headnode->next;
headnode->next=node;
}
void deletenode(struct Node*headnode,int data)
{
struct Node*prenode=headnode;
struct Node*backnode=headnode->next;
if(!backnode)
{
printf("NOT Found\n");
}
while(backnode->data!=data)
{
prenode=backnode;
backnode=prenode->next;
}
prenode->next=backnode->next;
free(backnode);
}
void printList(struct Node*headnode)
{
struct Node*pmove=headnode->next;
if(!pmove)
{
printf("链表为空,无法打印!\n");
}
else
{
while(pmove)
{
printf("%d\n",pmove->data);
pmove=pmove->next;
}
}
}
int main()
{
struct Node*List=createList();
insertnode(List,1);
insertnode(List,2);
insertnode(List,3);
deletenode(List,2);
printList(List);
return 0;
}
数据结构之链表的增删查等功能实现
最新推荐文章于 2024-08-27 22:35:52 发布