如何用C语言解决练习3编程,Stephen Kochan-第11章
我想知道“考虑建立一个指向列表开头的特殊结构”这句话是什么意思。这是否意味着他希望在练习中创建一个指向列表开头的新结构,而不是在练习中用于创建列表的结构?或者他是想用练习2中创建的相同结构制作一个虚拟结构?我制作了这个节目;看起来像这样如何用C语言解决练习3编程,Stephen Kochan-第11章,c,C,我想知道“考虑建立一个指向列表开头的特殊结构”这句话是什么意思。这是否意味着他希望在练习中创建一个指向列表开头的新结构,而不是在练习中用于创建列表的结构?或者他是想用练习2中创建的相同结构制作一个虚拟结构?我制作了这个节目;看起来像这样 #include <stdio.h> #include <stdlib.h> struct entry { int value; struct entry *next; }; struct entry *head, n
#include <stdio.h>
#include <stdlib.h>
struct entry
{
int value;
struct entry *next;
};
struct entry *head, n1, n2, n3, n4, temp;
void insertentry (struct entry *newEntry/*= temp*/, struct entry *EntryNo/*=head*/ )
{
newEntry->next = EntryNo->next;
EntryNo->next = newEntry;
}
int main()
{
void insertentry (struct entry *newEntry, struct entry *EntryNo );
head = &n1;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = (struct entry *) 0;
n4.value = 340;
printf("the value before the new entry\n");
while(head != (struct entry *) 0) {
printf("%i\n", head->value);
head = head->next;
}
head = &temp;
temp.next = &n1;
insertentry(&n4, &temp.next);
printf("the value after the new entry\n");
while(head != (struct entry *) 0) {
printf("%i\n", head->value);
head = head->next;
}
}
我制作了一个名为“temp”的虚拟结构来指向列表的开头,
但是当程序开始运行时,虚拟结构中的值出现了。我到底该怎么办?有人能帮忙吗?大家好,欢迎来到StackOverflow。发布练习的文本将非常有帮助,因为大多数人都没有这本书的副本。
insertentry(&n4,&temp.next)代码>类型不匹配。不需要在main
内部声明insertentry
。它已经可见,因为该定义位于main
的定义之前。即使不是这样,在函数定义中声明函数也不是一个好主意。列表头目前只是struct entry*head
。作者可能鼓励将其放入适当的结构中,而不是直接使用它,例如结构列表{struct entry*head;}
。下一步可能是将所有列表管理放在专用函数中,这些函数始终至少获取一个struct list
参数(或struct list*
)。乍一看,这似乎只是额外的努力。稍后您将认识到,这是“模块化和面向对象编程的第一步”-将数据与处理数据的函数封装在一起(在最低级别)。我的想法与Scheff基本相同,我很确定这就是练习的内容。当修改(添加、删除)列表的开头时,它将特别有用。
the value before the new entry
100
200
300
the value after the new entry
340
0
100
200
300