- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过 Stephen Kochan 的《C 语言编程》一书自学 C,并进行了以下关于指针的练习:
insertEntry
的函数将新条目插入到链接列表中。让该过程将指向要插入的列表条目的指针(类型为本章中定义的struct entry
)和指向列表中要插入新条目的元素的指针作为参数。struct entry
结构体定义如下:
struct entry
{
int x;
struct entry *ptr;
};
这是我的代码:
#include <stdio.h>
void insert_entry (struct entry *new_entry, struct entry *prev_entry);
struct entry
{
int x;
struct entry *ptr;
};
int main (void)
{
struct entry n1, n2, n3, new_entry;
struct entry *list_ptr = &n1;
n1.x = 1;
n1.ptr = &n2;
n2.x = 2;
n2.ptr = &n3;
n3.x = 4;
n3.ptr = (struct entry *) 0;
insert_entry (&new_entry, &n2);
// Loop to display the list to check if insert_entry worked
while ( list_ptr != (struct entry *) 0 ) {
printf ("%i\n", list_ptr->x);
list_ptr = list_ptr->ptr;
}
}
void insert_entry (struct entry *new_entry, struct entry *prev_entry)
{
new_entry->ptr = prev_entry->ptr;
prev_entry->ptr = new_entry;
// Assign a value to make it easier to check if the function worked
new_entry->x = 3;
}
这段代码工作得很好并且完成了工作。但是还有练习#3:
我不知道如何继续。如果我设置另一个结构,我将无法在不更改 insert_entry
的形式参数的情况下将其作为参数传递。函数需要。但我无法使用list_ptr
指针,因为如果我将它作为第二个参数传递给 insert_entry
执行语句new_entry->ptr = prev_entry->ptr;
没有意义。
有关此问题的其他问题集中在练习 #2 上,但我无法找到任何相关内容。帮助将永远感激不已。提前致谢。
*编辑:这就是我的代码现在的样子(感谢 u/ringzero):
#include <stdio.h>
void insert_entry (struct entry *new_entry, struct entry *prev_entry);
struct entry
{
int x;
struct entry *ptr;
};
int main (void)
{
struct entry n_start, n1, n2, n3, new_entry;
struct entry *list_ptr = &n_start;
n_start.ptr = &n1;
n1.x = 1;
n1.ptr = &n2;
n2.x = 2;
n2.ptr = &n3;
n3.x = 3;
n3.ptr = (struct entry *) 0;
insert_entry (&new_entry, &n_start);
// Loop to display the list to check if insert_entry worked
while ( list_ptr != (struct entry *) 0 ) {
printf ("%i\n", list_ptr->x);
list_ptr = list_ptr->ptr;
}
}
void insert_entry (struct entry *new_entry, struct entry *prev_entry)
{
new_entry->ptr = prev_entry->ptr;
prev_entry->ptr = new_entry;
// Assign a value to make it easier to check if the function worked
new_entry->x = 0;
}
n_start
应该是一个虚拟结构。该代码的唯一问题是循环显示 n_start.x
的值。 。我怎样才能使它不显示?
最佳答案
作者 Stephen Kochan 有一个网站:classroomm.com
那里有一个论坛,其中有一小部分与他的书“C 语言编程,第三版”相关。它包括奇数章末练习的答案。您会发现它非常有用。
以下是该网站上的相关信息,作为如何回答练习的小提示:
You can solve this problem by setting up a "dummy" structure variable called listHead,
for example:
struct entry listHead;
and you can then set it pointing to the head of the list by assigning the next member of listHead to point to the actual first entry of the list:
listHead.next = &entry1;
Now to insert a new entry called newEntry at the front of the list, you can write:
insertEntry (&new_entry, &list_head);
我不会发布我为练习编写的代码 - 如果您自己研究它,您会学到更多。
关于c - C 语言编程,Stephen Kochan - 第 11 章,练习 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36532241/
我正在学习 Kochan 的 C 编程书籍,并且正在做一项练习,该练习需要一个函数将一个字符串插入另一个字符串中,函数调用包括要插入字符串的位置。 我已经编写了以下代码,但每当我输入输入时,我都会收到
我一直在自学 C 编程,这是一位精通 C 的 friend 推荐的书。书名是 Stephen Kochan 的“Programming in C”。 我有 Java 背景,我对 Stephen 书中的
正在研究 Kochan 的书,并在练习 7.12 中遇到了一些困难。目标是输入一个可变长度的二维数组并转置它。我已经完成了转置部分,但是当我将值输入到数组中时,它们并没有全部成功。代码如下。假设我指定
我正在学习 Stephen Kochan 的《C 语言编程》一书。我在第 82 页的程序上遇到了问题。该程序采用数学表达式并解决它。我的问题是最后的 else 语句,该语句的目的是在用户输入无效字符时
我在 Stephen G Kochan 的书 Programming in c 中看到这段代码。这可能吗? float absolute_value(x) float x; { -----
我正在开发一个程序,该程序接受文本字符串并从中提取子字符串(Kochan 的 C 语言编程练习 9.4)。目标是创建一个名为 substring 的函数,该函数将字符串、从哪里开始提取、要提取多少个字
这是我的 C 源代码: details that stackoverflow want to see or i can't edit #include int main (void) { i
我无法获得解决此问题的功能程序(Kochan 的“C 语言编程”中的练习 5.6。目标是让程序接受输入数字,例如 123,并输出“一二三”。 现在,我有两个循环,结果是我想要输出的数字(2345 中的
希望得到位运算符的帮助。练习内容如下: Write a function called bitpat_search() that looks for the occurence of a specif
我正在通过 Stephen Kochan 的《C 语言编程》一书自学 C,并进行了以下关于指针的练习: 编写一个名为 insertEntry 的函数将新条目插入到链接列表中。让该过程将指向要插入的列表
为什么它说MathOps类别中的分子和分母是未声明的变量? 它位于第11章和程序11.1 不明白为什么它不起作用,因为它与书中的代码相同,没有任何错误。 请帮忙 代码: #import #
我对 Stephen Kochan 编写的《C 语言编程》一书中第 10 章的练习 10 有疑问。 问题是: Write a function called dictionarySort that s
您好! 我在21.2练习中遇到了麻烦。在那个练习中,我构建了分数计算器。我有 9 个按钮和数学操作。当我在 iOS 模拟器上按任何数字(1、2、3 或任何其他数字)时,它会生成错误: 2012
我是一名优秀的程序员,十分优秀!