c 单链表的一些操作

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

typedef struct NODE /链表结构
{
int data;
struct NODE* next;
}Node;

void Initlinkedlist() /初始化
{
Node* head;
head = (Node*)malloc(sizeof(Node));
if (head == NULL)
{
printf(“mistake”);
EXIT_FAILURE;
}
else
head->next = NULL;
}

Node* Creat(int n) /创建一个链表
{
int x;
Node* head;
head = (Node*)malloc(sizeof(Node));
if (head == NULL)
EXIT_FAILURE;
else
{
head->next = NULL;
Node* tail;
tail = head;
printf(“输入”);
while (n–)
{
scanf_s("%d", &x);
Node* p;
p = (Node*)malloc(sizeof(Node));
if (p == NULL)
EXIT_FAILURE;
else
{
p->data = x;
tail->next = p;
tail = p;
}
}
tail->next = NULL;
return head;
}
}

void Output(Node* head) /输出
{
Node* p;
for (p = head->next; p != NULL; p = p->next)
printf("%d", p->data);
}

void Insert(Node** head, int x) /插入
{
Node* current, * new1;
while ((current = *head) != NULL && current->data < x)
head = &current->next;

new1 = (Node*)malloc(sizeof(Node));
if (new1 == NULL)
	EXIT_FAILURE;
else
{
	new1->data = x;
	new1->next = current;
	*head = new1;
}

}

void Del(Node**head,int del) /删除
{
Node* current;
while ((current = *head) != NULL && current->data != del)
head = &current->next;
if (current != NULL)
{
if (current->data == del)
{
*head = current->next;
free(current);
}
else
printf(“没有找到”);
}
}

int main()
{
int n, del, x;
Node* head;
Node** linkp;
Initlinkedlist();
printf(“输入长度”);
scanf_s("%d", &n);
head = Creat(n);
Output(head);
printf("\n插入");
scanf_s("%d", &x);
linkp = &head;
Insert(linkp, x);
Output(head);
printf("\n删除");
scanf_s("%d", &del);
Del(linkp, del);
Output(head);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值