struct node
{
int data;
struct node* next;
};
//创建头结点
struct node* creatlink() //创建头结点
{
struct node* head = (struct node*)malloc(sizeof(struct node));
assert(head);
head->next = NULL;
return head;
}
//创建中间链表
struct node* creatnode(int data) //创建中间链表
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
assert(newnode);
newnode->data = data;
newnode->next = NULL;
return newnode;
}
//头插链表
void insertnode(struct node*head,int data) //头插链表
{
struct node* newnode = creatnode(data);
newnode->next =head->next;
head->next = newnode;
}
//尾插链表
void inserttail(struct node* head, int data) //尾插链表
{
//先找到尾结点 使用遍历pmove
struct node* pnew = creatnode( data);
struct node* pmove = head;
while (pmove->next != NULL)
{
pmove = pmove->next;
}
//找到了最后的链表
pmove->next = pnew;
}
//指定数据前面插入
void insertoppion(struct node* head, int postdata, int data) //指定数据前面插入
{
struct node* pre = head;
struct node* cru = head->next;
while(cru != NULL && cru->data != postdata)
{
pre = cru;
cru = cru->next;
}
if (cru == NULL)
{
printf("输入错误");
}
else
{
struct node* pnew = creatnode(data);
pre->next = pnew;
pnew->next = cru;
}
}
//特定序号插入
void insertindex(struct node* head, int index, int data) //特定序号插入
{
struct node* pre = head;
struct node* cur = head->next;
int i = 1;
while (i < index&& cur != NULL)
{
pre = cur;
cur = cur->next;
i++;
}
if (cur == NULL)
{
printf("数据输入错误");
}
struct node* pnew = creatnode(data);
pnew->next = cur;
pre->next = pnew;
}
// 头部删除节点
void deletehead(struct node* head) // 头部删除节点
{
struct node* deletehead = head->next;
if (deletehead == NULL)
{
printf("无法删除");
return;
}
head->next = deletehead->next;
free(deletehead);
deletehead = NULL;
}
//尾部删除
void deletetail(struct node* head) //尾部删除
{
// 先找到尾部节点 所以需要遍历
struct node* pre = head;
struct node* cur = head->next;
if (cur == NULL) //先判断
{
printf("链表为空无法删除");
return;
}
while (cur->next != NULL) //遍历 指针往后移动
{
pre = cur;
cur = cur->next;
}
//走出循环后cur ->next指向了空
free(cur);
cur = NULL;
// pre = cur;
pre->next = NULL;
}
//制定数据删除
void deleteappoin(struct node* head,int postdata) //制定数据删除
{
struct node* pre = head;
struct node* cur = head->next;
while (cur != NULL && cur->data != postdata)
{
pre = cur;
cur = cur->next;
}
if (cur == NULL)
{
printf("无法删除,未找到指定位置");
}
else
{
pre->next = cur ->next;
free(cur);
cur = NULL;
}
}
//查找数据
struct node* search(struct node* head, int posdata)//查找数据
{
struct node* pmove = head->next;
while (pmove != NULL && pmove->data != posdata)
{
pmove = pmove->next;
}
return pmove;
}
//打印 遍历链表
void printnode(struct node* head) //打印 遍历链表
{
struct node*pmove=head->next;
while (pmove != NULL)
{
printf("%d", pmove->data);
pmove = pmove->next;
printf("\t");
}
printf("\n");
}
//调用
int main()
{
struct node* head = creatlink();
for (int i = 0; i < 10; i++)
{
insertnode(head,i);
}
printnode(head);
inserttail(head, 888);
printnode(head);
insertoppion(head, 1, 666);
printnode(head);
insertindex(head, 4, 33);
printnode(head);
deletehead(head);
printnode(head);
deletetail(head);
printnode(head);
deleteappoin(head, 33);
printnode(head);
deleteappoin(head, 4);
printnode(head);
struct node*result=search(head, 3);
if (result != NULL)
printf("%d\n", result->data);
else
printf("无法找到");
return 0;
}
//完整代码
#include<stdio.h>
#include<malloc.h>
#include<assert.h>
struct node
{
int data;
struct node* next;
};
struct node* creatlink() //创建头结点
{
struct node* head = (struct node*)malloc(sizeof(struct node));
assert(head);
head->next = NULL;
return head;
}
struct node* creatnode(int data) //创建中间链表
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
assert(newnode);
newnode->data = data;
newnode->next = NULL;
return newnode;
}
void insertnode(struct node*head,int data) //头插链表
{
struct node* newnode = creatnode(data);
newnode->next =head->next;
head->next = newnode;
}
void inserttail(struct node* head, int data) //尾插链表
{
//先找到尾结点 使用遍历pmove
struct node* pnew = creatnode( data);
struct node* pmove = head;
while (pmove->next != NULL)
{
pmove = pmove->next;
}
//找到了最后的链表
pmove->next = pnew;
}
void insertoppion(struct node* head, int postdata, int data) //指定数据前面插入
{
struct node* pre = head;
struct node* cru = head->next;
while(cru != NULL && cru->data != postdata)
{
pre = cru;
cru = cru->next;
}
if (cru == NULL)
{
printf("输入错误");
}
else
{
struct node* pnew = creatnode(data);
pre->next = pnew;
pnew->next = cru;
}
}
void insertindex(struct node* head, int index, int data) //特定序号插入
{
struct node* pre = head;
struct node* cur = head->next;
int i = 1;
while (i < index&& cur != NULL)
{
pre = cur;
cur = cur->next;
i++;
}
if (cur == NULL)
{
printf("数据输入错误");
}
struct node* pnew = creatnode(data);
pnew->next = cur;
pre->next = pnew;
}
void deletehead(struct node* head) // 头部删除节点
{
struct node* deletehead = head->next;
if (deletehead == NULL)
{
printf("无法删除");
return;
}
head->next = deletehead->next;
free(deletehead);
deletehead = NULL;
}
void deletetail(struct node* head) //尾部删除
{
// 先找到尾部节点 所以需要遍历
struct node* pre = head;
struct node* cur = head->next;
if (cur == NULL) //先判断
{
printf("链表为空无法删除");
return;
}
while (cur->next != NULL) //遍历 指针往后移动
{
pre = cur;
cur = cur->next;
}
//走出循环后cur ->next指向了空
free(cur);
cur = NULL;
// pre = cur;
pre->next = NULL;
}
void deleteappoin(struct node* head,int postdata) //制定数据删除
{
struct node* pre = head;
struct node* cur = head->next;
while (cur != NULL && cur->data != postdata)
{
pre = cur;
cur = cur->next;
}
if (cur == NULL)
{
printf("无法删除,未找到指定位置");
}
else
{
pre->next = cur ->next;
free(cur);
cur = NULL;
}
}
struct node* search(struct node* head, int posdata)//查找数据
{
struct node* pmove = head->next;
while (pmove != NULL && pmove->data != posdata)
{
pmove = pmove->next;
}
return pmove;
}
void printnode(struct node* head) //打印 遍历链表
{
struct node*pmove=head->next;
while (pmove != NULL)
{
printf("%d", pmove->data);
pmove = pmove->next;
printf("\t");
}
printf("\n");
}
int main()
{
struct node* head = creatlink();
for (int i = 0; i < 10; i++)
{
insertnode(head,i);
}
printnode(head);
inserttail(head, 888);
printnode(head);
insertoppion(head, 1, 666);
printnode(head);
insertindex(head, 4, 33);
printnode(head);
deletehead(head);
printnode(head);
deletetail(head);
printnode(head);
deleteappoin(head, 33);
printnode(head);
deleteappoin(head, 4);
printnode(head);
struct node*result=search(head, 3);
if (result != NULL)
printf("%d\n", result->data);
else
printf("无法找到");
return 0;
}