Я не могу понять почему этот код не работает пожалуйста помогите мне
После вставки имени сотрудника код перестает работать.
Пожалуйста, решите эту проблему, я сохраню весь код ниже.
Что я уже пробовал:
#include <stdio.h> #include <stdlib.h> #include<malloc.h> typedef struct emp { int id; char nm; struct emp *prev; struct emp *next; }list; list *start; list *end; ///////////////////////////////////// Functios ///////////////////////////////////// void initlist (void); struct emp *create_emp(void); void append_emp( struct emp *element); int delete_emp(int d); void edit_emp(int old , int current); list *display (); struct emp *search_emp(int s); ////////////////////////////////////////////////////////////////////// ///////////////////////////////////// MAIN //////////////////////////// ////////////////////////////////////////////////////////////////////// int main() { int cases; char nm1;int id1;list *c1; list *i1; int c2,del; int old3,new3; int i5;list *c5; initlist(); printf("\nLinked List for Employees\n"); printf("---------------------------------------------\n"); printf("Press 1 to INSERT an Employee into the list \n"); printf("Press 2 to DELETE an Employee from the list \n"); printf("Press 3 to EDIT Employee \n"); printf("Press 4 to DISPLAY the list \n"); printf("Press 5 to SEARCH the list \n"); printf("Press 6 to EXIT the program\n"); printf("---------------------------------------------\n"); while (1) { scanf("%d",&cases); if (cases ==6) { break; } switch(cases) { case 1: //why when i run this code it's stop working c1=create_emp(); append_emp(c1); printf("Enter the new name : "); scanf("%c\n",& nm1); printf("Enter the new id : "); scanf("%d\n",& id1); i1 ->id =id1; c1->nm=nm1; break; case 2: //why when i run this code it's stop working printf("Enter the Employee id you want to delete\n"); scanf("%d\n",c2); del=delete_emp(c2); if (del == 0) { printf("Not found"); } else { printf("Removed form list"); } break; case 3: //why when i run this code it's stop working printf("Enter the Employee id you want to Edit \n"); scanf("%d",old3); printf("Enter the new Employee id"); scanf("%d",new3); edit_emp(old3,new3); break; case 4: display(); break; case 5: printf("Enter the id you'r searching for :"); scanf("%d",i5); c5=search_emp(i5); if(c5==NULL) printf("not found\n"); else printf("found\n"); break; } } } ///////////////////////////////////// Creat list /////////////////////////// void initlist (void) { start=end=NULL; } ///////////////////////////////////// Create emp ///////////////////////////////////// struct emp *create_emp(void) { return ((struct emp*)malloc(sizeof(struct emp))); } ///////////////////////////////////// Insert emp ///////////////////////////////////// void append_emp( struct emp *element ) { if ((start==NULL)&&(end==NULL)) { start=element; end=element; element->next=NULL; element->prev=NULL; } else { end->next=element; element->prev=end; end=element; element->next=NULL; } } ///////////////////////////////////// Delete emp ///////////////////////////////////// int delete_emp(int d) { struct emp *temp; temp=search_emp (d); if(temp==NULL) return 0; if(start==end) { start=NULL; end=NULL; free(temp); return 1; } else if(temp==start) { start=start->next; start->prev=NULL; free(temp); return 1; } else if (temp==end) { end=temp->prev; end->next=NULL; free(temp); return 1; } else { temp->prev->next=temp->next; temp->next->prev=temp->prev; free(temp); return 1; } } ///////////////////////////////////// Edit emp ///////////////////////////////////// void edit_emp(int old , int current) { list * temp; temp = search_emp(old); if (temp==NULL) { printf("NOT EXIST\n"); } else { temp->id =current; } } ///////////////////////////////////// Display list ///////////////////////////////////// list *display () { int d=1; list*temp; temp=start; while (temp!=NULL) { printf("The Employee %d ID is : %d\n",d,temp->id); temp=temp->next; d++; } } ///////////////////////////////////// Search dy ID ///////////////////////////////////// struct emp *search_emp(int s) { struct emp *temp; temp=start; while((temp!=NULL)&&(temp->id!=s)) { temp=temp->next; } return temp; }
phil.o
Отладьте свой код, как вам сказал ОГ в своем решении. Никто здесь не захочет делать это за вас; более того, вы можете обнаружить, что это довольно интересное занятие.