当前位置:首页 > 经验 >

单链表增加节点(单链表删除节点)

来源:原点资讯(www.yd166.com)时间:2022-11-04 00:20:14作者:YD166手机阅读>>

单链表中增加节点,除了增加结点本身的数据域和指针域,还需要更改前、后结点的指针域。

一个简单的实例代码:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct student

{

char name[15];

int mark;

struct student *next;

}node, *node;

node add(char *value,node head)

{

node p,new;

new = (node)malloc(sizeof(Node));

if(new ==NULL)

{

printf("error");

exit(1);

}

else

{

printf("please input the name:");

scanf("%s",new->name);

printf("please input the mark:");

scanf("%d",&new->mark);

}

p = head->next;

while(p!=NULL)

if(strcmp(p->name,value)==0)

{

new->next = p->next;

p->next = new;

return head;

}

else

p=p->next;

return NULL;

}

void print(node head)

{

node p;

p=head->next;

printf("\nthe record:\n");

while(p!=NULL)

{

printf("name:%s ",p->name);

printf(" mark:%d\n",p->mark);

p=p->next;

}

}

int main()

{

int num,i,j;

char name[15];

node p,pTemp,pTemp2,head;

head = (node)malloc(sizeof(Node));

if(head==NULL)

{

printf("error");

exit(1);

}

else

head->next = NULL;

printf("Please input the number of students:\n");

scanf("%d",&num);

printf("Please input the information:");

for(i=0;i<num;i )

{

p = (node)malloc(sizeof(Node));

if(p==NULL)

{

printf("error");

exit(1);

}

else

{

printf("\nname:");

scanf("%s",p->name);

printf("mark:");

scanf("%d",&p->mark);

if(head->next == NULL)

{

head->next = p;

pTemp=p;

}

else

{

pTemp->next = p;

pTemp=p;

}

}

}

pTemp->next=NULL;

p=head->next;

print(head);

printf("please input the position(student's name) where do you want to insert\n");

scanf("%s",name);

pTemp2 =add(name,head);

if(pTemp2==NULL)

printf("no find");

else

print(head);

system("pause");

return 0;

}

运行效果如下:

单链表增加节点,单链表删除节点(1)

-End-

,

栏目热文

十字链表简单理解(十字链表的节点结构)

十字链表简单理解(十字链表的节点结构)

前面之前的数据结构知识,介绍了矩阵的三元组表示法,当然之前只介绍矩阵运算中的转置,至于乘法运算以及加减运算,之前没有介绍...

2022-11-04 00:17:04查看全文 >>

单链表怎么找到上一个节点(链表的节点图解)

单链表怎么找到上一个节点(链表的节点图解)

给定一个单链表,判断其中是否有环,在网上搜集了一些资料,然后总结一下大概可以涉及到的问题,以及相应的解法。首先,关于单链...

2022-11-04 00:32:16查看全文 >>

单链表如何查找中间结点(如何求链表中的中间节点)

单链表如何查找中间结点(如何求链表中的中间节点)

部分数据结构请看常见Java问题及笔试题(二十一),这里只列出主要方法与测试用例!!!寻找单链表的中间节点,最简单的思路...

2022-11-04 00:14:56查看全文 >>

单链表中间节点(单链表中各节点是否连续)

单链表中间节点(单链表中各节点是否连续)

单链表是一种线性数据结构,与顺序表占据一段连续的内存空间不同,链表是用一组地址任意的存储单元来存储数据,每个存储单元分散...

2022-11-04 00:25:29查看全文 >>

单链表查找特定值(单链表的查找并计数)

单链表查找特定值(单链表的查找并计数)

数组中取值可以根据下标获取指定的值,但链表不行,链表中逻辑相邻的元素,在物理上不一定是相邻的。链表中取值只能从首元结点开...

2022-11-04 00:04:49查看全文 >>

单链表的创建图解(单链表的创建步骤)

单链表的创建图解(单链表的创建步骤)

创建单链表除了使用前插法,还可以使用后插法。后插法通过将新结点逐个插入到链表的尾部来创建链表。如下图所示根据上图所示,可...

2022-11-04 00:18:55查看全文 >>

单链表按序号查找(单链表按值查找操作)

单链表按序号查找(单链表按值查找操作)

严蔚敏《数据结构》笔记(1-4章)由于上传限制只能分章节上传,并且图片和公式可能无法上传出现乱码影响阅读。需要完整资料的...

2022-11-04 00:44:17查看全文 >>

单链表查找图示(三链表结构图解)

单链表查找图示(三链表结构图解)

电子计算机的本质是对输入(input)的数据进行处理(processing),对处理的结果形成输出(output)。程序...

2022-11-04 00:06:55查看全文 >>

单链表尾指针示意图(链表的节点图解)

单链表尾指针示意图(链表的节点图解)

作者:少年吉来源:微信公众号: 数据科学CLUB出处:https://mp.weixin.qq.com/s?__biz=...

2022-11-04 00:10:17查看全文 >>

单链表的插入结点图解(链表结点交换图解)

单链表的插入结点图解(链表结点交换图解)

单链表的删除和插入操作是线性表中比较重要的一部分,而这些操作又是线性表中的难点,同时也是考试的重点。对于初学者来说,在看...

2022-11-03 23:58:47查看全文 >>

文档排行