单链表的增、删、改、查(原创)
由 王宇 原创并发布:
以下代码均在linux 环境下,gcc v4.5 编译调试通过。
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }*head; void append(int number) { struct node *append_node = NULL, *right = NULL; append_node = (struct node *)malloc(sizeof(struct node)); if(append_node == NULL) { perror("Error: memory alloced failed at function of append!"); exit(EXIT_FAILURE); } append_node->data = number; append_node->next = NULL; if(head == NULL) { head = append_node; return; } right = head; while(right->next != NULL) { right = right->next; } right->next = append_node; } void add(int number) { struct node *add_node = NULL; add_node = (struct node *)malloc(sizeof(struct node)); if(add_node == NULL) { perror("Error: memory alloced failed at function of add!"); exit(EXIT_FAILURE); } add_node->data = number; add_node->next = NULL; if(head== NULL) { head = add_node; } else { add_node->next = head; head = add_node; } } void insert_at(int number, int position) { int i; struct node *insert_node = NULL, *left = NULL, *right = NULL; left = head; if(left == NULL) { perror("Error: linked list wasnot created!"); return; } if(position < 1) { perror("Error: Did not find position!"); return; } for(i = 0; i < position; i++) { if(left != NULL ) { if(i == position - 1) { right = left->next; break; } else { left = left->next; } } else { perror("Error: Did not find position!"); return; } } insert_node = (struct node *)malloc(sizeof(struct node)); if(insert_node == NULL) { perror("Error: memory alloced failed at function of append!"); exit(EXIT_FAILURE); } insert_node->data = number; insert_node->next = right; left->next = insert_node; } int delete_number(int number) { struct node *linked_list = NULL, *previous = NULL; linked_list = head; while (linked_list != NULL) { if(linked_list->data == number) { if(linked_list == head) { head = linked_list->next; free(linked_list); return 1; } else { previous->next = linked_list->next; free(linked_list); return 1; } } else { previous = linked_list; linked_list = linked_list->next; } } return 0; } void delete_all() { struct node *linked_list = NULL; linked_list = head; while ( head != NULL) { linked_list = head->next; head = head->next; free(linked_list); } } void display() { struct node *linked_list = NULL; linked_list = head; while ( linked_list != NULL) { printf("Data of linked list is %d \n", linked_list->data); linked_list = linked_list->next; } } int count_sum() { int sum = 0; struct node *linked_list = NULL; linked_list = head; while ( linked_list != NULL) { sum++; linked_list = linked_list->next; } return sum; } int main() { int i, number, position; while(1) { printf("\nList Operations\n"); printf("===============\n"); printf("1.Create\n"); printf("2.Add\n"); printf("3.Append\n"); printf("4.Insert\n"); printf("5.Display\n"); printf("6.Size\n"); printf("7.Delete\n"); printf("8.DeleteAll\n"); printf("0.Exit\n"); printf("Enter your choice : "); if(scanf("%d",&i) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { switch(i) { case 1: for(i = 1; i < 10; i++) { append(i); } break; case 2: printf("Enter the number to add: \n"); if(scanf("%d",&number) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { add(number); } break; case 3: printf("Enter the number to append: \n"); if(scanf("%d",&number) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { append(number); } break; case 4: printf("Enter the number to insert: \n"); if(scanf("%d",&number) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { printf("Enter the position: \n"); if(scanf("%d",&position) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { insert_at(number, position); } } break; case 5: display(); break; case 6: number = count_sum(); printf("Sum of linked list is %d", number); break; case 7: printf("Enter the number to delete: \n"); if(scanf("%d",&number) <=0) { printf("Enter only an Integer\n"); exit(EXIT_FAILURE); } else { delete_number(number); } break; case 8: delete_all(); break; case 0: exit(1); break; default: printf("Invalid option\n"); } } } exit(1); }