c语言的学习秘籍之链表
刚翻出来的作品,有点低级,但希望能起到作用:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define SLEEP() asm("sleep")
struct student *head;
struct student
{
int a;
int b;
struct student * next;
};
int n;
void add()
{
printf("请输入俩个你要储存的数字
");
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(sizeof(struct student));
head->next = p1;
scanf_s("%d%d", &p2->a, &p2->b);
while (p2->a != NULL)
{
n = n + 1;
p1->next = p2;
p1 = p2;
p2 = (struct student *)malloc(sizeof(struct student));
scanf_s("%d%d", &p2->a, &p2->b);
}
p1->next = NULL;
}
void print(struct student*head)
{
if (head->next != NULL)
{
struct student *p1;
p1 = (struct student *)malloc(sizeof(struct student));
p1 = head->next;
while (p1 != NULL)
{
printf("%d %d
", p1->a, p1->b);
p1 = p1->next;
}
}
else printf("无法执行!!!");
}
void look()
{
if (head->next != NULL)
{
struct student *p1;
p1 = (struct student *)malloc(sizeof(struct student));
p1 = head->next;
int m = 1;
printf("请输入你要查找的位置
");
int a;
scanf_s("%d", &a);
while (m != a)
{
m = m + 1;
p1 = p1->next;
}
printf("%d %d
", p1->a, p1->b);
}
else printf("无法执行!!!");
}
void gai()
{
if (head->next!=NULL)
{
struct student *p1;
p1 = (struct student *)malloc(sizeof(struct student));
p1 = head->next;
int m = 1;
printf("请输入你要修改的位置
");
int a;
scanf_s("%d", &a);
while (m != a)
{
m = m + 1;
p1 = p1->next;
}
scanf_s("%d%d", &p1->a, &p1->b);
}
else printf("无法执行!!!");
}
void shan()
{
if (head->next != NULL)
{
struct student *p1;
p1 = (struct student *)malloc(sizeof(struct student));
p1 = head;
int m = 1;
printf("请输入你要删除的位置
");
int a;
scanf_s("%d", &a);
while (m != a)
{
m = m + 1;
p1 = p1->next;
}
p1->next = p1->next->next;
}
else printf("无法执行!!!");
}
void zeng()/************************************************************************************/
{
if (head->next!=NULL)
{ struct student *p1, *p2, *p3;
p1 = (struct student *)malloc(sizeof(struct student));
p1 = head->next;
while (p1->next != NULL)
{
printf("%d %d
", p1->a, p1->b);
p1 = p1->next;
}
printf("请输入你要储存的数据
");
p2 = p3 = (struct student *)malloc(sizeof(struct student));
p1->next = p2;
scanf_s("%d%d", &p3->a, &p3->b);
while (p3->a!=0)
{
n = n + 1;
p2->next = p3;
p2 = p3;
p3 = (struct student *)malloc(sizeof(struct student));
scanf_s("%d%d", &p3->a, &p3->b);
}
p2->next = NULL;
}
else printf("无法执行!!!");
}
int main()/*******************************************************************************************/
{
head = (struct student *)malloc(sizeof(struct student));
head->next = NULL;
n = 0;
int a=1;
printf("***************************************************************************************************************
");
printf("* <<输入请按1>> <<输出请按2>> *
");
printf("* <<增加请按3>> <<删除请按4>> *
");
printf("* <<修改请按5>> <<退出请按0>> *
");
printf("请选择接下来的功能
");
while (a!=0)
{
scanf_s("%d", &a);
switch (a)
{
case 0:break;
case 1: add(); break;
case 2: print(head); break;
case 3: zeng(); break;
case 4:shan(); break;
case 5:gai(); break;
default:printf("孩子不要玩了!!!"),Sleep(1000),system("cls"),printf("爸爸永远爱你!!
");
}
if (a != 0){
printf("请选择接下来的功能
");
}
}
printf("等待两分钟有惊喜哦!!!!
");
Sleep(20000);
printf("让你等你就等,你傻了吧!!!sb
");
return 0;
}