zoukankan
html css js c++ java
实验十 链表
1、
#include
<
stdio.h
>
#include
<
stdlib.h
>
void
main()
{
int
*
a,
*
b,
*
c,
*
min;
a
=
(
int
*
)malloc(
sizeof
(
int
));
b
=
(
int
*
)malloc(
sizeof
(
int
));
c
=
(
int
*
)malloc(
sizeof
(
int
));
min
=
(
int
*
)malloc(
sizeof
(
int
));
printf(
"
输入三个整数:
"
);scanf(
"
%d %d %d
"
,a,b,c);
printf(
"
输出这三个整数:%d %d %d \n
"
,
*
a,
*
b,
*
c);
*
min
=*
a;
if
(
*
a
>*
b)
*
min
=*
b;
if
(
*
a
>*
c)
*
min
=*
c;
printf(
"
输出最小整数:%d\n
"
,
*
min);
free(a);free(b);free(c);free(min);
}
2、
#include
<
stdio.h
>
#include
<
stdlib.h
>
struct
list
{
char
data;
struct
list
*
next;
}
;
struct
list
*
create()
{
struct
list
*
h,
*
p,
*
q;
char
ch;
h
=
(
struct
list
*
)malloc(
sizeof
(
struct
list));
p
=
q
=
h;
ch
=
getchar();
while
(ch
!=
'
?
'
)
{
p
=
(
struct
list
*
)malloc(
sizeof
(
struct
list));
p
->
data
=
ch;
q
=
p;
ch
=
getchar();
}
p
->
next
=
NULL;
return
h;
}
void
main()
{
}
3、
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
conio.h
>
#include
<
ctype.h
>
#define
LEN sizeof(struct student)
struct
student
{
long
num;
char
name[
20
];
float
score[
3
];
struct
student
*
next;
}
;
int
n;
struct
student
*
create()
{
struct
student
*
h,
*
p,
*
q;
n
=
0
;h
=
NULL;
p
=
(
struct
student
*
)malloc(LEN);
scanf(
"
%ld %s %f %f %f
"
,
&
p
->
num,p
->
name,
&
p
->
score[
0
],
&
p
->
score[
1
],
&
p
->
score[
2
]);
p
->
next
=
NULL;
while
(p
->
num
!=
0
)
{
++
n;
if
(n
==
1
) h
=
p;
else
q
->
next
=
p;
q
=
p;
p
=
(
struct
student
*
)malloc(LEN);
scanf(
"
%ld %s %f %f %f
"
,
&
p
->
num,p
->
name,
&
p
->
score[
0
],
&
p
->
score[
1
],
&
p
->
score[
2
]);
p
->
next
=
NULL;
}
free(p);
return
(h);
}
void
print(
struct
student
*
head)
{
struct
student
*
p;
printf(
"
\n现在共有%d个学生信息:\n
"
,n);
p
=
head;
if
(head
!=
NULL)
do
{
printf(
"
学号\t姓名\t语文\t数学\t英语\n
"
);
printf(
"
%ld\t%s\t%.2f\t%.2f\t%.2f\n
"
,p
->
num,p
->
name,p
->
score[
0
],p
->
score[
1
],p
->
score[
2
]);
p
=
p
->
next;
}
while
(p
!=
NULL);
}
struct
student
*
insert(
struct
student
*
head,
struct
student
*
stud)
{
struct
student
*
p0,
*
p1,
*
p2;
p1
=
head;
p0
=
stud;
if
(head
==
NULL)
{head
=
p0;p0
->
next
=
NULL;}
else
{
while
((p0
->
num
>
p1
->
num)
&&
(p1
->
next
!=
NULL))
{p2
=
p1;p1
=
p1
->
next;}
if
(p0
->
num
<=
p1
->
num)
{
if
(head
==
p1) head
=
p0;
else
p2
->
next
=
p0;
p0
->
next
=
p1;
}
else
{p1
->
next
=
p0;p0
->
next
=
NULL;}
}
++
n;
return
head;
}
struct
student
*
del(
struct
student
*
head,
long
num)
{
struct
student
*
p1,
*
p2;
if
(head
==
NULL)
{
printf(
"
\n无学生信息!\n
"
);
goto
end;
}
p1
=
head;
while
(num
!=
p1
->
num
&&
p1
->
next
!=
NULL)
{p2
=
p1;p1
=
p1
->
next;}
if
(num
==
p1
->
num)
{
if
(p1
==
head)
head
=
p1
->
next;
else
p2
->
next
=
p1
->
next;
printf(
"
删除:%ld\n
"
,num);
free(p1);
n
--
;
}
else
printf(
"
无学号%ld记录!\n
"
,num);
end:
return
head;
}
struct
student
*
find(
struct
student
*
head,
long
num)
{
struct
student
*
p1,
*
p2;
if
(head
==
NULL)
{
printf(
"
\n无学生信息!\n
"
);
goto
end;
}
p1
=
head;
while
(num
!=
p1
->
num
&&
p1
->
next
!=
NULL)
{p2
=
p1;p1
=
p1
->
next;}
if
(num
==
p1
->
num)
printf(
"
查找结果:%ld\t%s\t%.2f\t%.2f\t%.2f\n
"
,num,p1
->
name,p1
->
score[
0
],p1
->
score[
1
],p1
->
score[
2
]);
else
printf(
"
没有发现%ld学生记录!\n
"
,num);
end:
return
head;
}
void
main()
{
struct
student
*
head,
*
stu;
long
find_num,del_num;
char
letter;
printf(
"
请输入学生信息(学号为0结束):\n
"
);
head
=
create();
print(head);
do
{
printf(
"
A 插入学生信息\n
"
);
printf(
"
B 查找学生信息\n
"
);
printf(
"
C 删除学生信息\n
"
);
printf(
"
Q 退出\n
"
);
printf(
"
请选择: \n
"
);
letter
=
getch();
letter
=
toupper(letter);
switch
(letter)
{
case
'
A
'
:
printf(
"
请输入要插入的学生信息:\n
"
);
stu
=
(
struct
student
*
)malloc(LEN);
scanf(
"
%ld %s %f %f %f
"
,
&
stu
->
num,stu
->
name,
&
stu
->
score[
0
],
&
stu
->
score[
1
],
&
stu
->
score[
2
]);
head
=
insert(head,stu);
print(head);
break
;
case
'
B
'
:
printf(
"
请输入要查找的学生学号:\n
"
);
scanf(
"
%ld
"
,
&
find_num);
head
=
find(head,find_num);
//
print(head);
break
;
case
'
C
'
:
printf(
"
请输入要删除的学生学号:\n
"
);
scanf(
"
%ld
"
,
&
del_num);
head
=
del(head,del_num);
print(head);
break
;
}
}
while
(letter
!=
'
Q
'
);
}
查看全文
相关阅读:
HDU 3389 Game (阶梯博弈)
国内操作系统OS分析(上)
激光雷达应用技术分析
构建深度学习框架运行平台
GitHub上YOLOv5开源代码的训练数据定义
GitHub上开源的YOLOv5
深度学习调用TensorFlow、PyTorch等框架
CUDA C 纹理提取Texture Fetching
CPU,GPU,GPGPU
毫米波RADAR与LIDAR探秘
原文地址:https://www.cnblogs.com/qixin622/p/651408.html
最新文章
P2622 关灯问题II (状态压缩入门)
Friends and Berries URAL
D
Distance Gym
C
D
Python 字典(Dictionary) copy()方法
Python 字典(Dictionary) clear()方法
Python 字典(Dictionary) type()方法
Python 字典(Dictionary) str()方法
热门文章
Python 字典(Dictionary) len()方法
虚拟机linux操作系统上安装vmwareTools
hdu 4315 Climbing the Hill && poj 1704 Georgia and Bob阶梯博弈--尼姆博弈
HDU 1564 Play a game && HDU 2147 kiki's game
HDU3544 Alice's Game && POJ 2960 S-Nim(SG函数)
hdu 1517 Multiplication Game
Poj-3922 A simple stone game(k倍动态减法)
HDU 3032 Nim or not Nim?(SG打表找规律)
HDU 3537 Daizhenyang's Coin 翻硬币博弈
翻硬币游戏
Copyright © 2011-2022 走看看