我是 C 的新手,这是我的第一个关于链表的代码,我已经在这个代码上工作了 3 天,还没有完成,该程序是关于从用户那里获取有关员工的数据并编辑、删除或搜索按身份证。
我在开关盒的主要功能中遇到问题;案例 1,2,3。
case1 : 我需要程序停止使用相同的数据(重复),代码可以多次使用相同的名称和 id
案例2,案例3:当我运行此代码时,它停止工作
我正在使用带有 gcc 编译器的 win7 的 code-blocks10.05
#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);
// what code i can write here to stop enter the same data to diff. emp.
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;
}