Ошибки в из отсортированного списка программы на языке C
Привет всем , я должен написать программу для вставки целых чисел в сортированный связанный список. а затем удалите любой элемент, который выберет пользователь. в коде я не могу вставить элементы в свой связанный список. кто-нибудь может мне помочь?
Заранее спасибо
код таков
[C] сортированный связанный список - Pastebin.com[^]
Что я уже пробовал:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> struct node { // int data; int key; struct node *next; }; struct node* init(){ struct node *head =0; return head; } int isempty(struct node * head){ if(head == NULL) return 1; else return 2; } //display the list void printList(struct node * head) { struct node *ptr = head; printf("\n"); int n; n=isempty(head); if(n == 1) printf("list is empty"); //start from the beginning while(ptr != NULL) { printf("%d ",ptr->key); ptr = ptr->next; } } void create(struct node * head,int num) { struct node * tmp = head; struct node * prev = NULL; struct node* new = malloc(sizeof(struct node)); new->key = num; prev = tmp; int n; n=isempty(head); if(n == 1) head->next = new; while(tmp != NULL && tmp->key < num){ prev = tmp; tmp = tmp->next; } new->next = tmp; if(prev !=NULL) prev->next = new; } /* struct node *tmp; tmp=(struct node *)malloc(sizeof(struct node )); tmp->data=data; tmp->link=start; start=tmp;*/ //delete a link with given key struct node* delete(struct node * head, int del) { //start from the first link struct node* current = head; struct node* previous = NULL; struct node* temp = NULL; //if list is empty int n; n=isempty(head); if(n == 1) { printf("list is empty"); return NULL; } //navigate through list while(current->key != del) { if (current == head){ temp = head; temp = temp->next; free(head); head = temp; } //if it is last node if(current->next == NULL) { return NULL; } else { //store reference to current link previous = current; //move to next link current = current->next; } } //found a match, update the link if(current == head) { //change first to point to next link head = head->next; } else { //bypass the current link previous->next = current->next; } return current; } int main() { int op; int num; struct node* head; head=init(); do{ printf("\n Menu \n 1.Insert \n 2.delete element \n 3.display List \n 4. end program "); printf("n \n \n please enter an option : "); scanf("%d",&op); switch (op) { case 1: printf("Enter data:"); scanf("%d",&num); create(head, num); break; case 2: printf("Enter data:"); scanf("%d",&num); delete(head,num); break; case 3: printList(head); break; case 4: free(head); exit(0); default: printf("\n enter an option : "); } }while(1); }
jeron1
Не могли бы вы разместить код в разделе "Что я пробовал" и, пожалуйста, включить теги кода.
Member 14066698
я сделал это