我有一个程序利用链表(作为结构)来存储整数元素(基本上在堆栈上)。问题是,当我将整数插入通过指针访问的数组并尝试打印时,数字不是输入的整数(看起来像指针,虽然不确定,当我尝试取消引用它时,我得到错误在编译中)。 C的新手,这是一个类赋值,所以更多地寻找关于指针和指导的解释,以及我可能做错了什么。我附上了我已经完成的代码。 作为参考,每个节点的默认大小为5个整数。
运行我的代码的内容如下:
应该是什么样的:
我的猜测是问题依赖于push / pop / top方法,尽管我在make_node
方法中使用指针确实有些运气,尽管我在其余方法中遇到了分段错误。
谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "int_stack.h"
/* Structure definitions from header file - all methods defined there as well.
struct is_node {
int *contents; Pointer to memory for storing up to node_capacity ints
(this has a fixed size and is determined at stack creation)
int next_index; Index of the next open position in the array of contents; starts at 0
struct is_node *next; Pointer to subsequent node in list (or NULL)
};
struct int_stack {
int size; Number of elements currently in stack
int node_capacity; Max elements per node
struct is_node *head; First node with stack contents; the contents
may be empty , but head should never be NULL
};
*/
struct is_node *make_node(int node_capacity);
/*
* Creates a stack, assigning appropriate variables.
*/
struct int_stack *make_stack(int node_capacity) {
struct int_stack *stack = NULL;
stack = malloc(sizeof(struct int_stack));
if (stack == NULL) {
fprintf(stderr, "Memory error!\n");
exit(-1);
}
stack->size = 0;
stack->node_capacity = node_capacity;
stack->head = make_node(node_capacity);
return stack;
}
/*
* Cleans up all memory used by the given stack.
*/
void free_stack(struct int_stack *stk) {
struct is_node *curnode = stk->head;
while (curnode != NULL) {
struct is_node *nextnode = curnode->next;
free(curnode);
curnode = nextnode;
}
free(stk);
}
/*
* Resets the stack, but allows it to still be used.
*/
void reset_stack(struct int_stack *stk) {
if (stk != NULL) {
struct is_node *curnode = stk->head;
while (curnode != NULL) {
struct is_node *nextnode = curnode->next;
free(curnode);
curnode = nextnode;
}
stk->size = 0;
stk->head = make_node(stk->node_capacity);
} else {
printf("Error: Stack is NULL. Cannot reset it.");
}
}
/*
* Prints the stack. Contents delimited with [] if node is at capacity
* or (] if not. The values of each node are seperated by commas.
*/
void print_stack(struct int_stack *stk) {
int i;
struct is_node *curnode = stk->head;
/* Count number of nodes */
int node_count = 1;
while (curnode->next != NULL) {
++node_count;
curnode = curnode->next;
}
/* Walk to end of stack and insert */
while (node_count > 0) {
curnode = stk->head;
for (i = 1; i < node_count; ++i) {
curnode = curnode->next;
}
if (curnode->next_index >= stk->node_capacity) {
printf("[");
} else {
printf("(");
}
for (i = curnode->next_index - 1; i >= 0; --i) {
if (i == 0) {
printf("%d]", curnode->contents[i]);
} else {
printf("%d,", curnode->contents[i]);
}
}
--node_count;
}
printf("\n");
}
/*
* Lets the user know if the stack is empty
*/
int is_empty(struct int_stack *stk) {
if(stk->size == 0) {
return 1;
}
return 0;
}
/*
* Pushes an int onto the stack
*/
void push(struct int_stack *stk, int v) {
/* Walk to end of stack and insert */
struct is_node *curnode = stk->head;
while (curnode->next != NULL) {
curnode = curnode->next;
}
if(curnode->next_index >= stk->node_capacity) {
struct is_node *new_node = make_node(stk->node_capacity);
new_node->contents[new_node->next_index] = v;
new_node->next_index += 1;
curnode->next = new_node;
} else {
curnode->contents[curnode->next_index] = v;
curnode->next_index = curnode->next_index + 1;
}
stk->size += 1;
}
/*
* Pulls the first int on the stack off the stack
*/
int pop(struct int_stack *stk) {
if (!is_empty(stk)) {
int top;
struct is_node *prevnode = stk->head;
struct is_node *curnode = stk->head;
struct is_node *nextnode = stk->head->next;
while (nextnode != NULL) {
if (nextnode->next != NULL) {
prevnode = curnode;
}
curnode = nextnode;
nextnode = curnode->next;
}
top = curnode->contents[curnode->next_index - 1];
curnode->next_index = curnode->next_index - 1;
if (curnode->next_index == 0) {
free(curnode);
curnode = NULL;
prevnode->next = NULL;
}
stk->size -= 1;
return top;
}
return -1;
}
/*
* Returns the top value from the stack.
*/
int top(struct int_stack *stk) {
struct is_node *curnode = stk->head;
while (curnode->next != NULL) {
curnode = curnode->next;
}
return curnode->contents[curnode->next_index - 1];
}
/*
* Helper method for creating nodes in the stack.
*/
struct is_node *make_node(int node_capacity) {
struct is_node *node = malloc(sizeof(struct is_node));
if (node == NULL) {
fprintf(stderr, "Memory error!\n");
exit(-1);
}
node->next = NULL;
node->next_index = 0;
int node_contents[node_capacity];
node->contents = node_contents;
return node;
}
答案 0 :(得分:1)
make_node()
将node->contents
设置为一个局部变量,一旦函数结束,它将超出范围。如果您在函数外部使用contents
,则会有未定义的行为。