C语言 简单链表插入,修改,删除问题.

C语言 简单链表插入,修改,删除问题.,第1张

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define Len sizeof(stu)

struct stu {

int num

int score

char name[30]

struct stu *next

}

struct stu *intnew() {

struct stu *head,*p

head = p = (struct stu *)malloc(Len)

int num,score

char name[30]

puts("学号 姓名 分数:") 

while(scanf("%d%s%d",&num,name,&score) == 3) {

p->next = (struct stu *)malloc(Len)

p->next->num = num

p->next->score = score

strcpy(p->next->name,name)

p = p->next

puts("学号 姓名 分数(q to quit):") 

}

fflush(stdin)

p->next = NULL

return head

}

void show(struct stu *head) {

struct stu *p = head->next

while(p) {

printf("%d\t%s\t%d\n",p->num,p->name,p->score)

p = p->next

}

}

void del(struct stu *head) {

int number,flag = 1

struct stu *q,*p = head 

printf("要删除学生的学号:")

scanf("%d",&number)

while(p->next) {

if(p->next->num == number && flag) {

q = p->next 

p->next = q->next

free(q)

flag = 0

}

p = p->next

}

if(flag) printf("没有找到学号是%d的学生。\n",number)

}

int main() {

struct stu *head = intnew()

show(head)

del(head)

show(head)

return 0

}

#include

<stdio.h>

#include

<stdlib.h>

//使用结构体构建链表

struct

node{

int

data

struct

node

*next

}

void

main()

{

int

a,n=1

struct

node

*p,*head,*t

head=(struct

node

*)malloc(sizeof(struct

node))

//p=(struct

node

*)malloc(sizeof(struct

node))

//申请动态空间

p=head

//申请动态空间

t=(struct

node

*)malloc(sizeof(struct

node))

for(n<=5n++)

//输入1,3,5,7,9

{

p->data=2*n-1

p->next=(struct

node

*)malloc(sizeof(struct

node))

p=p->next

}

printf("原始链表如下:\n")

//输出原始链表

for(p=headp->next!=NULLp=p->next)

{

printf("%d

",p->data)

}

printf("\n请输入需要插入的数据\n")

//输入所要插入的新数据

scanf("%d",&a

)

for(p=headp->next!=NULL)

//按顺序插入相应位置

{

if(p->data

<=

a

&&

(p->next)->data

>=

a)

{

t->data

=a

t->next

=p->next

p->next=t

break

}

p=p->next

}

printf("插入新数据后的链表\n")

//输出插入新数据的链表

for(p=headp->next!=NULL)

{

printf("%d

",p->data)

p=p->next

}

printf("\n")

free(p)

free(head)

free(t)

}

C语言创建单链表如下:

#include"stdio.h"

#include"stdlib.h"

#include"malloc.h"

#include "iostream.h"

typedef struct node

{

int  data

node * next

}node , * List

void create(int n)

{

int c

List s,L

L=(List)malloc(sizeof(node))

L->next=NULL

printf("请输入第1个数据:")

scanf("%d",&c)

L->data=c

for(int i=2i<=ni++)

{

s=(List)malloc(sizeof(node))

printf("请输入第%d个数据:",i)

scanf("%d",&c)

s->data=c

s->next=L

L->next =s

}

printf("链表创建成功!")

}

void main()

{

int n

printf("请你输入链表的个数:")

scanf("%d",&n)

create(n)

}

单链表创建方法:

单链表的建立有头插法、尾插法两种方法。

1. 头插法

单链表是用户不断申请 存储单元和改变链接关系而得到的一种特殊 数据结构,将链表的左边称为链头,右边称为链尾。头插法建单链表是将链表右端看成固定的,链表不断向左延伸而得到的。头插法最先得到的是尾结点

由于链表的长度是随机的,故用一个while循环来控制链表中结点个数。假设每个结点的值都大于O,则循环条件为输入的值大于o。申请 存储空间可使用malloc()函数实现,需设立一申请单元 指针,但malloc()函数得到的指针并不是指向 结构体的指针,需使用 强制类型转换,将其转换成结构体型指针。刚开始时,链表还没建立,是一空链表,head 指针为NULL。

链表建立的过程是申请空间、得到数据、建立链接的循环处理过程。

2. 尾插法

若将链表的左端固定,链表不断向右延伸,这种建立链表的方法称为尾插法。尾插法建立链表时,头 指针固定不动,故必须设立一个搜索指针,向链表右边延伸,则整个算法中应设立三个链表指针,即头指针head、搜索指针p2、申请单元指针pl。尾插法最先得到的是 头结点。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11645179.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存