| 
| 
查看: 955|回复: 0
 | 
字母sorting
[复制链接] |  
 |  | 
 
| 我做auto sorting当我insert data的时候 怎样才能做到呢? 复制代码#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
        char student_name[50];
        char student_intake[20];
        char student_branch[50];
        int student_level;
};
struct node
{
        struct node *prev;
        struct student data;
        struct node *next;
}*newNode, *head, *temp, *temp2, *end;
void insertNode();
void displayNode();
void deleteNode();
void sorting();
void exit();
int i = 1;
void main()
{
        int choice;
        do
        {
                system("cls");
                printf("==========Linked List==========\n\n");
                printf("1 - Insert Element\n");
                printf("2 - Display list\n");
                printf("3 - Delete node\n");
                printf("4 - Exit\n");
                printf("Please enter your option: ");
                scanf("%d", &choice);
                switch (choice)
                {
                case 1:
                        system("cls");
                        insertNode();
                        break;
                case 2:
                        system("cls");
                        displayNode();
                        break;
                case 3:
                        system("cls");
                        deleteNode();
                        break;
                case 4:
                        system("cls");
                        exit();
                        break;
                }
        } while (choice != 4);
        
        system("pause");
}
void insertNode()
{
        char std_name[50];
        char std_intake[20];
        char std_branch[50];
        int std_level;
        newNode = (struct node*) malloc(sizeof(struct node));
        printf("\nEnter student's name: ");
        fflush(stdin);
        gets(std_name);
        printf("\nEnter student's intake: ");
        fflush(stdin);
        gets(std_intake);
        printf("\nEnter student's branch: ");
        fflush(stdin);
        gets(std_branch);
        printf("\nEnter student's level: ");
        scanf("%d", &std_level);
        strcpy(newNode->data.student_name, std_name);
        strcpy(newNode->data.student_intake, std_intake);
        strcpy(newNode->data.student_branch, std_branch);
        newNode->data.student_level = std_level;
        newNode->prev = NULL;
        newNode->next = NULL;
        if (head == NULL)
        {
                end = head = newNode;
        }
        else
        {
                end->next = newNode;
                newNode->prev = end;
                end = end->next;
        }
        
        printf("\nStudent details had successful insert...\n");
        system("pause");
        sorting();
        main();
}
void displayNode()
{
        printf("==========Display==========\n");
        printf("Student No.\tName\tIntake\tBranch\tLevel\n\n");
        if (head == NULL)
        {
                printf("List is empty...\n");
                system("pause");
        }
        else
        {
                temp = head;
                while (temp != NULL)
                {
                        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);
                        temp = temp->next;
                        i++;
                }
                system("pause");
        }
}
        
void deleteNode()
{
        int remove_node;
        int j;
        printf("==========Delete==========\n");
        if (head == NULL)
        {
                printf("List is empty...\n");
                system("pause");
        }
        else
        {
                printf("Enter the student no to delete (Enter 00 to back): ");
                fflush(stdin);
                scanf("%d", &remove_node);
                if (remove_node == 1)
                {
                        if (head->next == NULL)
                        {
                                free(head);
                                head = NULL;
                                system("pause");
                                main();
                        }
                        else
                        {
                                temp = head;
                                head = head->next;
                                head->prev = NULL;
                                free(temp);
                        }
                }
                else if (remove_node == 00)
                {
                        main();
                }
                else
                {
                        temp = head;
                        for (j = 1; j < remove_node; j++)
                        {
                                if (temp->next == NULL)
                                {
                                        printf("Invalid option...");
                                        system("pause");
                                        deleteNode();
                                }
                                else
                                {
                                        temp2 = temp;
                                        temp = temp->next;
                                }
                        }
                        if (temp->next == NULL)
                        {
                                temp2->next = NULL;
                                free(temp);
                        }
                        else
                        {
                                temp2->next = temp2->next->next;
                                temp2 = temp2->next->prev;
                                free(temp);
                        }
                }
                printf("\nStudent details had deleted successful...\n");
                system("pause");
                main();
        }
}
void exit()
{
        int choice;
        printf("==========Exit==========\n");
        printf("Are you sure want to exit?\n");
        printf("Yes[1] or No[2]\n");
        printf("Please enter your option: ");
        scanf("%d", &choice);
        if (choice == 1)
                exit(1);
        else if (choice == 2)
                main();
        else
                printf("Invalid option...\n");
                system("pause");
                system("cls");
                exit();
}
 | 
 |  |  |  |
 
|  |  |  
|  |  |  |  | 
            本周最热论坛帖子 |