#include <stdio.h>
#define null 0
#define max 99
typedef struct dnode /*定义双向链表结构体*/
{ int number; /*货物编号*/
char name[max]; /*货物名称*/
int counter; /*货物数量*/
struct dnode *prior,*next; /*定义两指针,分别指向其前趋和后继*/
}dlnode;
void *initiate(dlnode **h) /*初始化*/
{ *h=(dlnode*)malloc(sizeof(dlnode));
(*h)->next=null;
}
dlnode *append(dlnode *p,dlnode thing[8]) /*建立仓库的双向链表*/
{ dlnode *s;
int i,j;
for(i=0; i<8;i++) /*建立结点*/
{ s=(dlnode*)malloc(sizeof(dlnode));
for(j=0;thing[i].name[j]!='\0';j++)
s->name[j]=thing[i].name[j];
s->name[j]='\0';
s->counter=thing[i].counter;
s->number=thing[i].number;
s->next=p->next;
p->next->prior=s;
p->next=s;
s->prior=p;
}
}
dlnode *search(dlnode *p,char kname[max]) /*查找指定货物的指针*/
{ dlnode *s;
s=p->next;
while(s!=null)
{ if(strcmp(s->name,kname)==0)
return(s); /*返回查找到的指针*/
s=s->next;
}
return(null); /*所查的货物不存在*/
}
int insert(dlnode *p,char kname[max]) /*在指定货物后插入新的货物*/
{ dlnode *m,*n;
dlnode *search();
m=search(p,kname); /*用search查找指定货物*/
if(m!=null)
{ n=(dlnode*)malloc(sizeof(dlnode)); /*申请新结点*/
printf("Input number name counter:");
scanf("%d%s%d",&n->number,n->name,&n->counter);/*输入新货物的存储情况*/
n->next=m->next;
m->next->prior=n;
m->next=n;
n->prior=m;
return(0);
}
else
{ printf("%s not found.\n",kname);
return(-1);
}
}
int delete(dlnode *p,char kname[max]) /*删除指定结点的后继结点*/
{ dlnode *s,*q,*r;
dlnode *search();
s=search(p,kname); /*查找指定结点*/
if(s->next==null) /*后继结点不存在*/
{ printf("node k has no follow.\n");
return(0);
}
else
{ q=s->next;
r=q;
q->next->prior=q->prior;
q->prior->next=q->next;
//s->next=q->next;
free(r);
return(1);
}
}
void print(dlnode *p,int i) /*打印第i个结点*/
{ dlnode *s;
int j=0;
if(i<0 || i>7)
{ printf("i is wrong.\n");
return;
}
s=p->next;
while(s!=null&&j !=i)
{ s=s->next;
j++;
}
printf("number\tname\tcounter\n");
printf("%d\t%s\t%d\n",s->number,s->name,s->counter);
}
void follow(dlnode *p,char kname[max]) /*打印指定结点的前趋和后继*/
{ dlnode *s,*q;
dlnode *search();
q=search(p,kname);
if(q==null)
{ printf("kname is wrong.\n");
return;
}
s=q->prior;
if(s==p) /*打印其前趋结点*/
printf("node k has not front node.\n");
else
{ printf("node k's front node:\n");
printf("number\tname\tcounter\n");
printf( "%d\t%s\t%d\n ",s->number,s->name,s->counter) ;
}
s=q->next;
if(s==null) /*打印其后继结点*/
{ printf("node k has no rear node.\n");
return;
}
else
{ printf("node k's rear node:\n");
printf("number\tname\tcounter\n");
printf( " %d\t%s\t%d\n ",s->number,s->name, s->counter);
}
}
void travel(dlnode *p) /*列出整A个仓库存货情况*/
{ dlnode *s;
s=p->next;
printf("number\tname\tcounter\n");
while(s!=null)
{ printf( "%d\t% s\t% d\n" , s->number, s->name, s->counter);
s=s->next;
}
}
void main()
{ int cmd,i;
char name[max],kname[max];
dlnode *q,*p;
dlnode *append();
int delete(),insert();
void print(),travel(),follow();
void *initiate();
static dlnode thing[8]={ {80,"motor",802}, {70,"machine",612}, {60,"housing",507}, {50,"ignit",408},{40,"brakes",230}, {30,"eank",118},{20,"filter",117},{10,"block",105} };
/*初始化仓库存货情况*/
initiate(&p);
append(p,thing); /*建立双向链表*/
printf("p:print No.i node;\n"); /*提示信息*/
printf("f:printf node ks front and rear node;\n");
printf("d:delete node k's rear'node;\n");
printf("i:insert new node beside node k;\n");
printf("t:travel all nodes;\n");
printf("q:exit;\n");
for(;;)
{
printf("cmd="); /*输入操作符*/
cmd=getchar();
getchar();
switch(cmd)
{ case'i': /*插入新结点*/
printf("Input kname:");
scanf("%s",kname);
insert(p,kname);
getchar();
break;
case'd': /*删除后继结点*/
printf("Input kname: ");
scanf("%s",kname);
delete(p,kname);
getchar();
break;
case'p': /*打印第i个结点*/
printf("Input i:");
scanf("%d",&i);
print(p,i);
getchar();
break;
case'f': /*打印指定结点的前趋和后继结点*/
printf("Input kname:");
scanf("%s",kname);
follow(p,kname);
getchar();
break;
case't': /*遍历整个仓库存货情况*/
travel(p);
break;
case'q':
printf("\n");
exit(0);
}//exit switch
}//exit for
}