我需要做的是复制已填充这些值的链表list1:
0000001 3
0000002 2
0000003 1
0000004 1
并将它们与函数CreateMenuList()粘贴到另一个名为list2的列表中,以便list2的每个元素都具有与特定数字匹配的list1成员,ViewAllMenu()的输出应为:
1
0000004
0000003
2
0000002
3
0000001
现在我刚刚写了这个基本概念:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Order {
char code[8];
int menu;
};
typedef struct NodeO {
struct Order order;
struct NodeO *next;
} TNode;
typedef TNode * NodeO;
struct Menu {
int code;
NodeO orders_list;
};
typedef struct NodeM {
struct Menu menu;
struct NodeM * next;
} TNodeM;
typedef TNodeM * NodeM;
//this function is for creating nodes for list1
NewOrder(struct Order p, NodeO * pp)
{
NodeO temp;
temp = malloc(sizeof(struct NodeO));
temp->order = p;
temp->next = *pp;
*pp = temp;
}
void main()
{
NodeO list1 = NULL;
NodeM list2 = NULL;
//supposing list1 is already filled
CreateMenuList(list1,list2);
ViewAllMenu(lista2);
}
假设我们已经使用使用stdin调用list1
的函数填充NewOrder
,函数CreateMenuList
应该做些什么来修改list2
并创建n
会员存储与菜单匹配的所有订单? ViewAllMenu
并不是真正的问题,因为它应该是printf
的简单while循环。
答案 0 :(得分:0)
这些是所需的所有功能:
void Load(NodeO * p)
{
FILE *f;
struct Order s;
char * buffer = malloc(sizeof(struct Order));
if(!(f=fopen("test.txt", "r")))
{
perror("Errore");
exit(EXIT_FAILURE);
}
while(fgets(buffer,sizeof(struct Order), f))
{
if(sscanf(buffer, "%s%d", s.code, &s.menu) == 2 )
{
NewOrder(s,p);
}
}
free(buffer);
fclose(f);
}
void InsertOrder(NodeM * pp, struct Order p)
{
NodeM s;
s = *pp;
while(s != NULL && (s->menu.code != p.menu))
s = s->next;
if( s == NULL)
{
s = (NodeM)malloc(sizeof(TNodeM));
s->next = *pp;
*pp = s;
s->menu.code = p.menu;
s->menu.list = NULL;
}
NewOrder(p, &s->menu.list);
}
void CreateMenuList(NodeO p, NodeM * pp)
{
while(p != NULL)
{
InsertOrder(pp, p->order);
p = p->next;
}
}
void ViewAllMenu(NodeM p)
{
NodeO s;
while(p != NULL)
{
s = p->menu.list;
printf("%d\n", p->menu.code);
while(s != NULL)
{
printf("\t%s\n", s->order.code);
s = s->next;
}
p = p->next;
}
}