我尝试创建一个包含20个表的列表。每个表应该有: - 数字从1到20; - 每张桌子的座位数(表1-10 - 2个座位;表11-15-4个座位;表16-20-6个座位); - 它应该表明天气是否被占用。
我的程序中需要这个列表,包含这些信息,这样我以后可以查找具有n个席位的表,将表的状态从免费更改为占用等等。
我觉得我的代码有问题。 我收到以下警告:
警告C4047:' =':'表格*'间接水平不同 '表*'
我的猜测是我犯了一个新手的错误,我误解了C中列表上的在线摘录。任何帮助都将不胜感激。 到目前为止我所做的是:
#include<stdio.h>
typedef struct tableList *table;
struct table {
int numTable; // number of table
int numPeople;
int free; //0 - free; 1 - occupied
table *next;
};
int main(void) {
struct table a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ;
a.numTable = 1; a.numPeople = 2; a.free = 0;
b.numTable = 2; b.numPeople = 2; b.free = 0;
c.numTable = 3; c.numPeople = 2; c.free = 0;
d.numTable = 4; d.numPeople = 2; d.free = 0;
e.numTable = 5; e.numPeople = 2; e.free = 0;
f.numTable = 6; f.numPeople = 2; f.free = 0;
g.numTable = 7; g.numPeople = 2; g.free = 0;
h.numTable = 8; h.numPeople = 2; h.free = 0;
i.numTable = 9; i.numPeople = 2; i.free = 0;
j.numTable = 10; j.numPeople = 2; j.free = 0;
k.numTable = 11; k.numPeople = 4; k.free = 0;
l.numTable = 12; l.numPeople = 4; l.free = 0;
m.numTable = 13; m.numPeople = 4; m.free = 0;
n.numTable = 14; n.numPeople = 4; n.free = 0;
o.numTable = 15; o.numPeople = 4; o.free = 0;
p.numTable = 16; p.numPeople = 6; p.free = 0;
q.numTable = 17; q.numPeople = 6; q.free = 0;
r.numTable = 18; r.numPeople = 6; r.free = 0;
s.numTable = 19; s.numPeople = 6; s.free = 0;
t.numTable = 20; t.numPeople = 6; t.free = 0;
a.next = &b;
b.next = &c;
c.next = &d;
d.next = &e;
e.next = &f;
g.next = &g;
h.next = &i;
i.next = &j;
j.next = &k;
k.next = &l;
l.next = &m;
m.next = &n;
n.next = &o;
o.next = &p;
p.next = &q;
q.next = &r;
r.next = &s;
s.next = &t;
t.next = NULL;
}
答案 0 :(得分:1)
你有声明:
typedef struct tableList *table;
表示table
是指向struct tableList
的指针,因此指针next
是指向table
的指针或指向struct tableList
的指针,或struct tableList **next
,但随后您为其指定struct table
。
将您的代码更改为:
struct table {
int numTable; // number of table
int numPeople;
int free; //0 - free; 1 - occupied
struct table *next;
};
typedef struct table *tableList;
现在,tableList
是一个显示struct table
的指针,因此您可以将其用作指向列表第一个节点的指针。
了解您的警告here。
通常,为了创建单个链表,首先应该定义一个如下所示的节点:
struct node {
... //whatever you want your node to contain
struct node *next;
}
然后如果需要,定义指向节点的指针:
typedef struct node *NodePtr;
然后,您可以动态分配内存以创建节点:
NodePtr = malloc(sizeof(struct node));
if (NodePtr == NULL)
...
现在NodePtr
指向struct node
。