【发布时间】:2018-05-23 21:30:08
【问题描述】:
我需要做的是复制已经填充了这些值的链表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);
}
假设我们已经使用一个使用标准输入调用NewOrder 的函数填充了list1,那么函数CreateMenuList 应该如何修改list2 并创建n 成员来存储与菜单匹配的所有订单? ViewAllMenu 并不是一个真正的问题,因为它应该是一个带有 printf 的简单 while 循环。
【问题讨论】:
-
我看到
malloc,但我在任何地方都没有看到free。卡布姆! -
void main()是古老的。 -
temp = (NodeO)malloc(sizeof(struct NodeO))缺少;并且类型转换是不必要的。
标签: c linked-list