c语言的学习秘籍之链表

2018-06-18 04:12:11来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

刚翻出来的作品,有点低级,但希望能起到作用:

 

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#define SLEEP() asm(&quot;sleep&quot;)
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;
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C Primer Plus(第六版)中文版 中的错误1

下一篇:C语言中指针定义的字符串和数组定义的字符串的区别