程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c語言的學習秘籍之鏈表,c語言秘籍

c語言的學習秘籍之鏈表,c語言秘籍

編輯:關於C語言

c語言的學習秘籍之鏈表,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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved