Ошибка при добавлении нового элемента в список - C (СИ)
Формулировка задачи:
Есть код для односвязного списка.
Без функций всё работает, при попытке сделать в виде функций, при добавлении нового элемента в список, - программа вылетает.
В чем причина?
Листинг программы
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
- struct Node
- {
- int number;
- Node* next;
- };
- struct Node* head;
- void add_numb(Node *head);
- void show_list(Node *head);
- void search_numb(Node *head);
- void del_elem(Node *head);
- void del_list(Node *head);
- void count_elem (Node *head);
- void sort(Node *head);
- void item()
- {
- int Key;
- do {
- system("cls");
- printf("\n");
- printf("\nHello! Possible operations (select 0 to 7):\n");
- printf ("1. Add Element\n");
- printf ("2. Show List\n");
- printf ("3. Search Element\n");
- printf ("4. Delete Element\n");
- printf ("5. Delete List\n");
- printf ("6. Count Elements\n");
- printf ("7. Sort List\n");
- printf ("0. Exit\n");
- scanf("%d",&Key);
- switch(Key)
- {
- case 1:add_numb(head);break;
- case 2:show_list(head);break;
- case 3:search_numb(head);break;
- case 4:del_elem(head);break;
- case 5:del_list(head);break;
- case 6:count_elem(head);break;
- case 7:sort(head);break;
- }
- } while(Key!=0);
- }
- void add_numb(Node *head)
- {
- system("cls");
- int numb = -1;
- printf ("Input Number: ");
- scanf ("%d", numb);
- Node* ptr = new Node;
- ptr->number = numb;
- ptr->next = NULL;
- if (head == NULL)
- head = ptr;
- else
- {
- Node * temp = head;
- while (temp->next)
- temp = temp->next;
- temp->next = ptr;
- }
- system("pause");
- }
- void show_list(Node *head)
- {
- system("cls");
- if (head == NULL)
- {
- printf ("\t!!! List is Empty !!!\n\n");
- system("pause");
- } else
- {
- printf ("* * * * * List * * * * *\n\n");
- Node* ptr = head;
- while (ptr)
- {
- printf ("%d ", ptr->number);
- ptr = ptr->next;
- }
- printf ("\n\n");
- system("pause");
- }
- }
- void search_numb(Node *head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- } else
- {
- int key = -1;
- printf("Input element for search: ");
- scanf("%d", key);
- Node * ptr = head;
- while ((ptr) && (key != ptr->number))
- ptr = ptr->next;
- if (ptr && (key == ptr->number))
- printf("\n\t!!! Element is found !!!\n");
- else
- printf("\n\t!!! Element is not found !!!\n");
- system("pause");
- }
- }
- void del_elem(Node *head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- } else
- {
- int key = -1;
- printf("Input element for delete: ");
- scanf ("%d", key);
- Node * ptr = head;
- Node* prevPtr = ptr;
- while ((ptr) && (key != ptr->number))
- {
- prevPtr = ptr;
- ptr = ptr->next;
- }
- if (ptr && (key == ptr->number))
- {
- if (ptr == head)
- head = ptr->next;
- else
- prevPtr->next = ptr->next;
- delete ptr;
- printf("\n\t!!! Element successfully deleted !!!\n");
- } else
- printf("\n\t!!! Element is not found !!!\n");
- system("pause");
- }
- }
- void del_list(Node *head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- } else
- {
- Node *ptr = head;
- Node *temp;
- while(ptr)
- {
- temp = ptr;
- ptr = ptr->next;
- delete temp;
- }
- head = 0;
- printf("\n\t!!! List successfully deleted !!!\n");
- system("pause");
- }
- }
- void count_elem(Node *head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- } else
- {
- int count = 0;
- Node* ptr = head;
- while (ptr)
- {
- count++;
- ptr = ptr->next;
- }
- printf("Amount of elements in list: \n\n");
- printf("%d", count);
- printf("\n\n");
- system("pause");
- }
- }
- void sort(Node *head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty! !!!\n\n");
- system("pause");
- } else
- {
- Node *a = NULL;
- Node *b = NULL;
- Node *c = NULL;
- Node *e = NULL;
- Node *tmp = NULL;
- while (e != head->next)
- {
- c = a = head;
- b = a->next;
- while (a != e)
- {
- if (a->number > b->number)
- {
- if (a == head)
- {
- tmp = b -> next;
- b->next = a;
- a->next = tmp;
- head = b;
- c = b;
- }
- else
- {
- tmp = b->next;
- b->next = a;
- a->next = tmp;
- c->next = b;
- c = b;
- }
- }
- else
- {
- c = a;
- a = a->next;
- }
- b = a->next;
- if (b == e)
- e = a;
- }
- }
- printf("\n\t!!! List successfully sorted !!!\n");
- printf("\n\n");
- system("pause");
- }
- }
- int main()
- {
- head = NULL;
- item();
- printf("\n");
- return 0;
- }
Решение задачи: «Ошибка при добавлении нового элемента в список»
textual
Листинг программы
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct node_t
- {
- int number;
- struct node_t* next;
- } Node;
- void add_numb(Node** head);
- void show_list(Node* head);
- void search_numb(Node* head);
- void del_elem(Node** head);
- void del_list(Node** head);
- void count_elem(Node* head);
- void sort(Node** head);
- void item()
- {
- Node* head = NULL;
- int Key;
- do
- {
- system("cls");
- printf("\n");
- printf("\nHello! Possible operations (select 0 to 7):\n");
- printf("1. Add Element\n");
- printf("2. Show List\n");
- printf("3. Search Element\n");
- printf("4. Delete Element\n");
- printf("5. Delete List\n");
- printf("6. Count Elements\n");
- printf("7. Sort List\n");
- printf("0. Exit\n");
- scanf("%d", &Key);
- switch (Key)
- {
- case 1:
- add_numb(&head);
- break;
- case 2:
- show_list(head);
- break;
- case 3:
- search_numb(head);
- break;
- case 4:
- del_elem(&head);
- break;
- case 5:
- del_list(&head);
- break;
- case 6:
- count_elem(head);
- break;
- case 7:
- sort(&head);
- break;
- }
- }
- while (Key != 0);
- del_list(&head);
- }
- void add_numb(Node** head)
- {
- system("cls");
- int numb = -1;
- printf("Input Number: ");
- scanf("%d", &numb);
- Node* ptr = (Node*) malloc(sizeof(Node));
- ptr->number = numb;
- ptr->next = NULL;
- if (*head == NULL)
- {
- *head = ptr;
- }
- else
- {
- Node* temp = *head;
- while (temp->next)
- {
- temp = temp->next;
- }
- temp->next = ptr;
- }
- system("pause");
- }
- void show_list(Node* head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- }
- else
- {
- printf("* * * * * List * * * * *\n\n");
- Node* ptr = head;
- while (ptr)
- {
- printf("%d ", ptr->number);
- ptr = ptr->next;
- }
- printf("\n\n");
- system("pause");
- }
- }
- void search_numb(Node* head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- }
- else
- {
- int key = -1;
- printf("Input element for search: ");
- scanf("%d", &key);
- Node* ptr = head;
- while ((ptr) && (key != ptr->number))
- {
- ptr = ptr->next;
- }
- if (ptr && (key == ptr->number))
- {
- printf("\n\t!!! Element is found !!!\n");
- }
- else
- {
- printf("\n\t!!! Element is not found !!!\n");
- }
- system("pause");
- }
- }
- void del_elem(Node** head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- }
- else
- {
- int key = -1;
- printf("Input element for delete: ");
- scanf("%d", &key);
- Node* ptr = *head;
- Node* prevPtr = ptr;
- while ((ptr) && (key != ptr->number))
- {
- prevPtr = ptr;
- ptr = ptr->next;
- }
- if (ptr && (key == ptr->number))
- {
- if (ptr == *head)
- {
- *head = ptr->next;
- }
- else
- {
- prevPtr->next = ptr->next;
- }
- free(ptr);
- printf("\n\t!!! Element successfully deleted !!!\n");
- }
- else
- {
- printf("\n\t!!! Element is not found !!!\n");
- }
- system("pause");
- }
- }
- void del_list(Node** head)
- {
- system("cls");
- if (*head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- }
- else
- {
- Node* ptr = *head;
- Node* temp;
- while (ptr)
- {
- temp = ptr;
- ptr = ptr->next;
- free(temp);
- }
- *head = 0;
- printf("\n\t!!! List successfully deleted !!!\n");
- system("pause");
- }
- }
- void count_elem(Node* head)
- {
- system("cls");
- if (head == NULL)
- {
- printf("\t!!! List is Empty !!!\n\n");
- system("pause");
- }
- else
- {
- int count = 0;
- Node* ptr = head;
- while (ptr)
- {
- count++;
- ptr = ptr->next;
- }
- printf("Amount of elements in list: \n\n");
- printf("%d", count);
- printf("\n\n");
- system("pause");
- }
- }
- void sort(Node** head)
- {
- system("cls");
- if (*head == NULL)
- {
- printf("\t!!! List is Empty! !!!\n\n");
- system("pause");
- }
- else
- {
- Node* a = NULL;
- Node* b = NULL;
- Node* c = NULL;
- Node* e = NULL;
- Node* tmp = NULL;
- while (e != (*head)->next)
- {
- c = a = *head;
- b = a->next;
- while (a != e)
- {
- if (a->number > b->number)
- {
- if (a == *head)
- {
- tmp = b -> next;
- b->next = a;
- a->next = tmp;
- *head = b;
- c = b;
- }
- else
- {
- tmp = b->next;
- b->next = a;
- a->next = tmp;
- c->next = b;
- c = b;
- }
- }
- else
- {
- c = a;
- a = a->next;
- }
- b = a->next;
- if (b == e)
- {
- e = a;
- }
- }
- }
- printf("\n\t!!! List successfully sorted !!!\n");
- printf("\n\n");
- system("pause");
- }
- }
- int main()
- {
- item();
- return 0;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д