C语言调试
#include <stdio.h> #include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <string.h>
typedef struct STRUCT/*定义结构体*/
{
int empnumber;/*员工编号*/
int productscount; /*产品数量*/
struct STRUCT *next;/*指向下一个节点的指针*/
}TS;/*结构体的别名*/
TS *CreateLink(TS *head) /*创建链表函数*/
{
char temp[100];
TS *p1;
p1=(TS*)malloc(sizeof(TS)); /*为一个链表节点分配存储空间*/
printf(" input employee number and the counts of products\n");
printf("exit: Double times 0 !\n"); /*两次输入0,表示退出*/
gets(temp);
p1->empnumber=atoi(temp); /*输入员工编号*/
gets(temp);
p1->productscount=atoi(temp);/*输入产品数量*/
p1->next=NULL; /*链表节点的尾指针赋值为NULL*/
while(p1->empnumber>0)/*当输入的员工编号不为0*/
{/*if(head==NULL)
head=p1;第一次插入,插在表头*/
TS *InsertIntoLink(TS* head,int empnumber,int productscount);
head=InsertIntoLink(head,p1->empnumber,p1->productscount);/*调用函数,进行节点插入*/
p1=(TS*)malloc(sizeof(TS)); /*为p1节点分配存储空间*/
printf(" input employee number and the counts of products\n");
printf("exit: Double times ENTER\n");
gets(temp);
p1->empnumber=atoi(temp); /*为p1的成员变量empnumber赋值*/
gets(temp);
p1->productscount=atoi(temp); /*为p1的成员变量赋值*/
p1->next=NULL;
}
return head; /*返回首指针*/
}
TS *InsertIntoLink(TS* head,int empnumber,int productscount) /*插入函数,有序插入*/
{
TS *p1,*p2,*p3;
p1=(TS*)malloc(sizeof(TS)); /*新建一个节点*/
p1->empnumber=empnumber; /*为节点赋值*/
p1->productscount=productscount;
p2=head; /*p2指向首节点*/
if(head==NULL) /*首节点为空*/
{
head=p1;p1->next=NULL;/*新节点插入在表头*/
printf("the head is null,so,the linklist head inserted success!\n");
}
else /*首节点不为空*/
{
while(productscount<p2->productscount&&p2->next!=NULL) /*插入产品数量少于当前节点的产品数量*/
{
p3=p2;
p2=p2->next; /*指针下移,继续搜索*/
}
if(productscount>=p2->productscount)
{
if(productscount==p2->productscount)
while(productscount==p2->productscount&&empnumber>p2->empnumber&&p2->next!=NULL) /*员工编号从小到大排序,如果要插入的员工编号比当前节点大,就继续往下搜索*/
{
p3=p2;
p2=p2->next;
}
if(head==p2) /*插在表头*/
{
head=p1;
p1->next=p2;
printf("the head is NOT null,so,the linklist head inserted success!\n");
}
else /*插在表中间*/
{
p3->next=p1;
p1->next=p2;
printf("the midle of linklist inserted success!\n");
}
}
else /*插在表尾*/
{
p2->next=p1;
p1->next=NULL;
printf("the end of linklist inserted success!\n");
}
}
printf("return the head of the linklist!\n");
return(head);
}
TS *DeletFromLink(TS *head,int empnumber) /*删除函数*/
{
TS *temp,*p;
temp=head;
if(head==NULL) printf("\n LinkList is null!\n");
else
{
temp=head;
while(temp->empnumber!=empnumber&&temp->next!=NULL) /*寻找要删除的节点*/
{
p=temp;
temp=temp->next;
}
if(temp->empnumber==empnumber) /*找到删除的员工编号*/
{
if(temp==head) /*如果是表头*/
{
head=head->next;
printf("the head :delets employee Number :%d\n",temp->empnumber); /*
打印要删除节点*/
free(temp);
}
else /*如果不是表头,也不是表尾*/
{
p->next=temp->next;
printf("delets employee Number :%d\n",temp->empnumber); /*打印要删除节点*/
free(temp);
}
}
else
printf("\n Cannot find Such Employee Number\n"); /*找不到要删除的员工*/
}
return(head);
}
TS *Search(TS *head,int empnumber)
{
TS *t;
t=head;
while(empnumber!=t->empnumber&&t->next!=NULL)
{
t=t->next;
}
if(empnumber==t->empnumber)
{
printf("Find it!\n");
return(t);
}
else
{
printf("Unable to find such employee number in the Linklist!\n");
t=NULL;
return(t);
}
void print(TS *head) /*打印函数*/
{
TS *temp,*p1;
int i=1;
temp=head;/*头指针赋值给temp,temp指向链表头*/
printf("\n output the result:\n");
printf("\n order\t\tporductscount\t\tempnumber\n");
while(temp!=NULL)
{
printf("\t%d\t\t%d\t\t\t%d\n",i,temp->productscount,temp->empnumber); /*输出序号i,产品数量,员工编号*/
i++;
temp=temp->next; /*指针下移*/
}
return 0;
}
void main()
{
TS *CreateLink(TS*); /*声明创建链表的函数*/
TS *InsertIntoLink(); /*声明插入链表的函数*/
TS *DeletFromLink(); /*声明删除函数*/
TS *Search();
void print();/*声明打印链表的函数*/
TS *head;
TS *temp;
TS *t2;
char s[100];
int empnumber=0;
int productscount=0;
int n;
head=NULL;
head=CreateLink(head); /*先创建空链表*/
temp=(TS*)malloc(sizeof(TS));
t2=(TS*)malloc(sizeof(TS));
while(1) /*一个可控制的循环*/
{
LOOP: printf("\t\t**********职工工作量统计系统**********\n\n");
printf("************1 改变职工信息***********\t");
printf("************2删除职工信息***********\n\n");
printf("************3显示职工信息***********\t");
printf("\t\t**********0 退出系统***********\n\n");
printf("your choice:");
gets(s);
if(strcmp(s,"1")!=0&&strcmp(s,"2")!=0&&strcmp(s,"3")!=0)
return 0;/*如果不是1或者2选项,就退出系统*/
if(strcmp(s,"1")==0) /*选择选项1,进行改变员工信息操作*/
{
printf("\nPlease Input you Employee Number And Products Count you are to change!\nthe employee Number :");
gets(s);
empnumber=atoi(s); /*输入员工编号*/
printf("\nSearching for the number,Please wait.....\n");
temp=Search(head,empnumber);
printf("\nprint the nod of temp: temp.empnumber= %d",temp->empnumber);
printf("\nprint the nod of temp: temp.productscount= %d",temp->productscount);
if(temp==NULL)
goto LOOP;
else
t2->empnumber=temp->empnumber;
t2->productscount=temp->productscount; /* memcpy(t2,temp,sizeof(TS));*/
printf("\nprint the nod of temp: temp.empnumber= %d",temp->empnumber);
printf("\nprint the nod of temp: temp.productscount= %d",temp->productscount);
printf("\nprint the nod of temp: t2.empnumber= %d",t2->empnumber);
printf("\nprint the nod of temp: t2.productscount= %d",t2->productscount);
t2->next=NULL;
printf("\nbefore Delete the temp node....\n");
print(head);
head=DeletFromLink(head,t2->empnumber);
printf("\nafter delete the temp node....\n");
print(head);
productscount+=t2->productscount;
}
while(1) /*改循环实现产品数量累加计算*/
{
printf("Would you like to input more products count? if not,please type 0\n your Input: ");
gets(s);
if(strcmp(s,"0")!=0)
productscount+=atoi(s);/*累加操作*/
else
break; /*停止累加*/
}
t2->productscount=productscount;
head=InsertIntoLink(head,t2->empnumber,t2->productscount);/* 把员工编号和产品数量插入到链表中*/
print(head); /*打印改链表*/
else if(strcmp(s,"2")==0) /*选择选项2,进行了删除员工信息操作*/
{
printf("input the Employee Number you wanna delete:");
gets(s); /*读取员工编号*/
empnumber=atoi(s); /*Search(head,empnumber); */
head=DeletFromLink(head,empnumber); /*调用删除函数*/
}
else
print(head);
}
return 0;
}
} 这个程序问题错在哪?怎么改呢?欢迎各位大神来试试