• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
成长中的利剑
博客园    首页    新随笔    联系   管理    订阅  订阅

c语言的学习秘籍之链表

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

 

#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;
}

posted @ 2016-12-06 14:07  成长中的利剑  阅读(294)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3