#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define LENGTH 12
#define OK 1
#define ERROR 0
#define Null 0
struct Node /*定义职工信息的结点*/
{
char jobnum[LENGTH];
long wages;
struct Node *next;
};
typedef struct Node staff;
typedef struct Node *StInfor;
int InitLinklist(StInfor List,int num) /*初始化链表,默认所有信息为空*/
{
StInfor peop,p=List; /*peop:指向新结点的指针。p:指向当前结点的指针。*/
while(num--)
{
peop=(staff *)malloc(sizeof(staff));
p->next=peop;
p=peop;
}
p->next=Null;
printf("\nThe InitLinklist operation is OK.\n");
}
int InputInfor(StInfor List,int num) /*向链表中输入信息*/
{
StInfor p=List->next; /*p:指向当前结点的指针。*/
int i=1;
printf("\nThe number of the staff is %d.\n",num);
while(num--)
{
printf("\nPlease enter the %d staff's information:",i);
printf("\nEnter the staff's jobnumber:");
fflush(stdin);
scanf("%s",p->jobnum);
printf("\nEnter the staff's wages:");
scanf("%ld",&p->wages);
p=p->next;
i++;
}
printf("\nThe InputInfor operation is OK.\n");
}
int printfLinklist(StInfor List) /*输出链表*/
{
StInfor p=List->next;
printf("\nThe information of the list are:\n");
printf("\n staff's number \tstaff's wages");
while(p)
{
printf("\n %-14s \t%12ld",p->jobnum,p->wages);
p=p->next;
}
}
int InsertLinklist(StInfor List) /*向链表中插入结点*/
{
StInfor peop,q=List,p=q->next;
/*peop:指向待插入的新结点的指针。q:指向新节点的前驱结点的指针。p:指向新节点的后继结点的指针。*/
printf("\nPlease enter the staff's information which you want to insert.");
peop=(staff *)malloc(sizeof(staff));
printf("\nEnter the staff's jobnumber:");
fflush(stdin);
scanf("%s",peop->jobnum);
printf("\nEnter the staff's wages:");
scanf("%ld",&peop->wages);
while(strcmp(p->jobnum,peop->jobnum)!=1)
{
q=p;
p=p->next;
if(!p)
{
peop->next=q->next;
q->next=peop;
return OK;
}
}
peop->next=q->next;
q->next=peop;
printf("\nThe InsertLinklist operation is OK.\n");
}
int deleteLinklist(StInfor List) /*删除链表中相应的职工信息*/
{
char jobnum[LENGTH]; /*jobnum:待删职工的工号。*/
StInfor q=List,p=q->next; /*q:指向待删职工结点的前驱结点的指针。p:指向待删职工结点的指针。*/
printf("\nPlease enter the staff's information which you want to delete.");
fflush(stdin);
scanf("%s",jobnum);
while(strcmp(p->jobnum,jobnum))
{
q=p;
p=p->next;
if(!p)
{
printf("\nDoesn't find the staff.\n");
return ERROR;
}
}
q->next=p->next;
free(p);
printf("\nThe InsertLinklist operation is OK.\n");
}
int main()
{
StInfor List; /*List:定义链表*/
int num; /*num:链表中的职工数目*/
char conti; /*conti:是否继续进行操作*/
printf("\nplease enter the total number of the staff:");
scanf("%d",&num);
InitLinklist(List,num); /*初始化链表*/
InputInfor(List,num); /*向链表输入信息*/
do{
printfLinklist(List); /*输出链表*/
InsertLinklist(List); /*插入职工*/
deleteLinklist(List); /*删除职工*/
printf("\nContinue,enter 'Y/y';or enter others.\n"); /*确定是否继续进行操作*/
fflush(stdin);
conti=getchar();
}while(conti=='Y'||conti=='y');
}
希望对你能有帮助