c言語的學習秘籍之鏈表。本站提示廣大學習愛好者:(c言語的學習秘籍之鏈表)文章只能為提供參考,不一定能成為您想要的結果。以下是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("請輸出倆個你要貯存的數字\n");
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\t%d\n", 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("請輸出你要查找的地位\n");
int a;
scanf_s("%d", &a);
while (m != a)
{
m = m + 1;
p1 = p1->next;
}
printf("%d\t%d\n", 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("請輸出你要修正的地位\n");
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("請輸出你要刪除的地位\n");
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\t%d\n", p1->a, p1->b);
p1 = p1->next;
}
printf("請輸出你要貯存的數據\n");
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("***************************************************************************************************************\n");
printf("* <<輸出請按1>> <<輸入請按2>> *\n");
printf("* <<添加請按3>> <<刪除請按4>> *\n");
printf("* <<修正請按5>> <<加入請按0>> *\n");
printf("請選擇接上去的功用\n");
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("爸爸永遠愛你!!\n");
}
if (a != 0){
printf("請選擇接上去的功用\n");
}
}
printf("等候兩分鐘有驚喜哦!!!!\n");
Sleep(20000);
printf("讓你等你就等,你傻了吧!!!sb\n");
return 0;
}