佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 939|回复: 0

字母sorting

[复制链接]
发表于 11-10-2014 03:53 PM | 显示全部楼层 |阅读模式
我做auto sorting当我insert data的时候 怎样才能做到呢?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct student
  5. {
  6.         char student_name[50];
  7.         char student_intake[20];
  8.         char student_branch[50];
  9.         int student_level;
  10. };

  11. struct node
  12. {
  13.         struct node *prev;
  14.         struct student data;
  15.         struct node *next;
  16. }*newNode, *head, *temp, *temp2, *end;

  17. void insertNode();
  18. void displayNode();
  19. void deleteNode();
  20. void sorting();
  21. void exit();

  22. int i = 1;

  23. void main()
  24. {
  25.         int choice;

  26.         do
  27.         {
  28.                 system("cls");
  29.                 printf("==========Linked List==========\n\n");
  30.                 printf("1 - Insert Element\n");
  31.                 printf("2 - Display list\n");
  32.                 printf("3 - Delete node\n");
  33.                 printf("4 - Exit\n");
  34.                 printf("Please enter your option: ");
  35.                 scanf("%d", &choice);
  36.                 switch (choice)
  37.                 {
  38.                 case 1:
  39.                         system("cls");
  40.                         insertNode();
  41.                         break;
  42.                 case 2:
  43.                         system("cls");
  44.                         displayNode();
  45.                         break;
  46.                 case 3:
  47.                         system("cls");
  48.                         deleteNode();
  49.                         break;
  50.                 case 4:
  51.                         system("cls");
  52.                         exit();
  53.                         break;
  54.                 }
  55.         } while (choice != 4);
  56.        
  57.         system("pause");
  58. }

  59. void insertNode()
  60. {
  61.         char std_name[50];
  62.         char std_intake[20];
  63.         char std_branch[50];
  64.         int std_level;

  65.         newNode = (struct node*) malloc(sizeof(struct node));

  66.         printf("\nEnter student's name: ");
  67.         fflush(stdin);
  68.         gets(std_name);

  69.         printf("\nEnter student's intake: ");
  70.         fflush(stdin);
  71.         gets(std_intake);

  72.         printf("\nEnter student's branch: ");
  73.         fflush(stdin);
  74.         gets(std_branch);

  75.         printf("\nEnter student's level: ");
  76.         scanf("%d", &std_level);

  77.         strcpy(newNode->data.student_name, std_name);
  78.         strcpy(newNode->data.student_intake, std_intake);
  79.         strcpy(newNode->data.student_branch, std_branch);
  80.         newNode->data.student_level = std_level;

  81.         newNode->prev = NULL;
  82.         newNode->next = NULL;

  83.         if (head == NULL)
  84.         {
  85.                 end = head = newNode;
  86.         }
  87.         else
  88.         {
  89.                 end->next = newNode;
  90.                 newNode->prev = end;
  91.                 end = end->next;
  92.         }
  93.        
  94.         printf("\nStudent details had successful insert...\n");
  95.         system("pause");
  96.         sorting();
  97.         main();
  98. }

  99. void displayNode()
  100. {
  101.         printf("==========Display==========\n");
  102.         printf("Student No.\tName\tIntake\tBranch\tLevel\n\n");

  103.         if (head == NULL)
  104.         {
  105.                 printf("List is empty...\n");
  106.                 system("pause");
  107.         }
  108.         else
  109.         {

  110.                 temp = head;

  111.                 while (temp != NULL)
  112.                 {
  113.                         printf("Student %d:\t%s\t%s\t%s\t%d\n", i, temp->data.student_name, temp->data.student_intake, temp->data.student_branch, temp->data.student_level);
  114.                         temp = temp->next;
  115.                         i++;
  116.                 }
  117.                 system("pause");
  118.         }
  119. }
  120.        

  121. void deleteNode()
  122. {
  123.         int remove_node;
  124.         int j;
  125.         printf("==========Delete==========\n");
  126.         if (head == NULL)
  127.         {
  128.                 printf("List is empty...\n");
  129.                 system("pause");
  130.         }
  131.         else
  132.         {
  133.                 printf("Enter the student no to delete (Enter 00 to back): ");
  134.                 fflush(stdin);
  135.                 scanf("%d", &remove_node);

  136.                 if (remove_node == 1)
  137.                 {
  138.                         if (head->next == NULL)
  139.                         {
  140.                                 free(head);
  141.                                 head = NULL;
  142.                                 system("pause");
  143.                                 main();
  144.                         }
  145.                         else
  146.                         {
  147.                                 temp = head;
  148.                                 head = head->next;
  149.                                 head->prev = NULL;
  150.                                 free(temp);
  151.                         }

  152.                 }
  153.                 else if (remove_node == 00)
  154.                 {
  155.                         main();
  156.                 }
  157.                 else
  158.                 {
  159.                         temp = head;
  160.                         for (j = 1; j < remove_node; j++)
  161.                         {
  162.                                 if (temp->next == NULL)
  163.                                 {
  164.                                         printf("Invalid option...");
  165.                                         system("pause");
  166.                                         deleteNode();
  167.                                 }
  168.                                 else
  169.                                 {
  170.                                         temp2 = temp;
  171.                                         temp = temp->next;
  172.                                 }
  173.                         }
  174.                         if (temp->next == NULL)
  175.                         {
  176.                                 temp2->next = NULL;
  177.                                 free(temp);
  178.                         }
  179.                         else
  180.                         {
  181.                                 temp2->next = temp2->next->next;
  182.                                 temp2 = temp2->next->prev;
  183.                                 free(temp);
  184.                         }
  185.                 }
  186.                 printf("\nStudent details had deleted successful...\n");
  187.                 system("pause");
  188.                 main();
  189.         }
  190. }



  191. void exit()
  192. {
  193.         int choice;
  194.         printf("==========Exit==========\n");
  195.         printf("Are you sure want to exit?\n");
  196.         printf("Yes[1] or No[2]\n");
  197.         printf("Please enter your option: ");
  198.         scanf("%d", &choice);
  199.         if (choice == 1)
  200.                 exit(1);
  201.         else if (choice == 2)
  202.                 main();
  203.         else
  204.                 printf("Invalid option...\n");
  205.                 system("pause");
  206.                 system("cls");
  207.                 exit();
  208. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 24-8-2025 04:44 PM , Processed in 0.117186 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表