SlinkList(不带头节点的单链表)20171003
来源:互联网 发布:centos 7 minimal桌面 编辑:程序博客网 时间:2024/05/17 01:55
#include<stdio.h>
typedef int Datatype;
typedef struct link_node {
Datatype info;
struct link_node *next;
}node;
node *init(node *head)
{
return NULL;
}
void display(node *head)
{
if (!head)
printf("链表为空,无法进行输出!\n");
else
{
node *p = head;
while (p)
{
printf("%5d", p->info);
p = p->next;
}
}
}
node *find(node *head, int i)
{
node *p = head;
int j = 2;;
if (i == 1)
return head;
else
{
p = p->next;
while (p&&i != j)
{
p = p->next;
++j;
}
if (!p&&i != j)
{
printf("没有找到第%d个结点\n", i);
return NULL;
}
else
if(i==j)
return p;
}
}
node *insert(node *head, Datatype x, int i)
{
node *p;
node *q=(node *)malloc(sizeof(node));
q->info = x;
q->next = NULL;
if (i == 0)
{
q->next = head;
head = q;
}
else
{
p = find(head, i);
if (p)
{
q->next = p->next;
p->next = q;
}
}
return head;
}
node *find_last(node *head)
{
node *pre = init(&pre), *p=head;
while (p)
{
pre = p;
p = p->next;
}
return pre;
}
node *build(node *head, Datatype x)
{
node *p, *q;
p = (node *)malloc(sizeof(node));
p->info = x;
p->next = NULL;
if (head == NULL)
head = p;
else
{
q = find_last(head);
q->next = p;
}
return head;
}
node *dele(node *head, Datatype x)
{
node *p = head,*pre=init(&pre);
if (!p)
printf("链表为空,无法进行删除!\n");
else
{
while (p&&p->info != x)
{
pre = p;
p = p->next;
}
if (!p&&p->info != x)
printf("没有找到要删除的值!\n");
else
if (p->info == x)
pre->next = p->next;
}
return head;
}
void main()
{
node *head;
head = init(&head);
int i;
node *f;
for(i=0;i<10;i++)
head = build(head, i*2);
display(head);
putchar('\n');
f = find(head, 8);
printf("第8个结点的值为:%d\n", f->info);
head = insert(head, 666, 8);
printf("在第8个结点后插入一个值后链表为:");
display(head);
putchar('\n');
head = dele(head, 8);
printf("删除值为8的结点后链表为:");
display(head);
putchar('\n');
}
typedef int Datatype;
typedef struct link_node {
Datatype info;
struct link_node *next;
}node;
node *init(node *head)
{
return NULL;
}
void display(node *head)
{
if (!head)
printf("链表为空,无法进行输出!\n");
else
{
node *p = head;
while (p)
{
printf("%5d", p->info);
p = p->next;
}
}
}
node *find(node *head, int i)
{
node *p = head;
int j = 2;;
if (i == 1)
return head;
else
{
p = p->next;
while (p&&i != j)
{
p = p->next;
++j;
}
if (!p&&i != j)
{
printf("没有找到第%d个结点\n", i);
return NULL;
}
else
if(i==j)
return p;
}
}
node *insert(node *head, Datatype x, int i)
{
node *p;
node *q=(node *)malloc(sizeof(node));
q->info = x;
q->next = NULL;
if (i == 0)
{
q->next = head;
head = q;
}
else
{
p = find(head, i);
if (p)
{
q->next = p->next;
p->next = q;
}
}
return head;
}
node *find_last(node *head)
{
node *pre = init(&pre), *p=head;
while (p)
{
pre = p;
p = p->next;
}
return pre;
}
node *build(node *head, Datatype x)
{
node *p, *q;
p = (node *)malloc(sizeof(node));
p->info = x;
p->next = NULL;
if (head == NULL)
head = p;
else
{
q = find_last(head);
q->next = p;
}
return head;
}
node *dele(node *head, Datatype x)
{
node *p = head,*pre=init(&pre);
if (!p)
printf("链表为空,无法进行删除!\n");
else
{
while (p&&p->info != x)
{
pre = p;
p = p->next;
}
if (!p&&p->info != x)
printf("没有找到要删除的值!\n");
else
if (p->info == x)
pre->next = p->next;
}
return head;
}
void main()
{
node *head;
head = init(&head);
int i;
node *f;
for(i=0;i<10;i++)
head = build(head, i*2);
display(head);
putchar('\n');
f = find(head, 8);
printf("第8个结点的值为:%d\n", f->info);
head = insert(head, 666, 8);
printf("在第8个结点后插入一个值后链表为:");
display(head);
putchar('\n');
head = dele(head, 8);
printf("删除值为8的结点后链表为:");
display(head);
putchar('\n');
}
阅读全文
1 0
- SlinkList(不带头节点的单链表)20171003
- 单链表的实现(不带头节点)
- 带头节点和不带头节点的单链表的区别
- 数据结构(一)单链表的基本操作(不带头节点)
- 实验2 不带头节点的单链表
- 递归算法-不带头节点的单链表
- 链表的创建(带头节点以及不带头节点)
- 带头节点和不带头节点的链表
- 带头节点的单链表
- 带头节点的单链表
- 带头节点的单链表
- 带头节点的单链表
- 数据结构之单链表——带头节点和不带头节点(C/C++)
- 链表 单链表 带头节点和不带头节点
- 求不带头结点的单链表的节点的个数
- linux大牛们的不带头节点的单链表
- 数据结构双语课->不带头节点的单链表实现
- 不带头结点的单链表删除任意一个节点
- 迭代和递归的差别与作用以及理解
- 20几岁,你的存款有多少?
- Spring MVC 文件上传下载
- idm安卓版(手机下载软件)中文专业版V5.0下载 | Android IDM+ v5.0
- Cube-web系统之Quartz+Spring实现任务监控管理
- SlinkList(不带头节点的单链表)20171003
- 最大数max(x,y,z)
- 线程池总结
- Mysql修改数据库密码
- 【简单易上手】虚拟机及win XP系统安装经验
- jq代码学习----文本框得到失去焦点
- 文字溢出隐藏显示省略号
- 阿里云服务器ECS Ubuntu16.04-64-bit学习之一:配置桌面
- python之Web Server Gateway Interface