[英]Getting Segmentation fault, during the implementation of Stack, using linked list
In the following code, I've tried to implement the stack data structure using a linked list.在下面的代码中,我尝试使用链表来实现堆栈数据结构。 Somewhere in my push function, I assume, I have implemented an improper use of pointers, since, upon execution of code, the compiler throws up a segmentation fault(core dumped)
我假设在我的推送函数的某个地方,我已经实现了对指针的不当使用,因为在执行代码时,编译器会抛出一个分段错误(核心转储)
How do I correct this?我该如何纠正?
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* nextlink;
};
struct Stack
{
int top;
struct node* head;
struct node* tail;
};
//creates an instance of struct Stack, and return a pointer to it
struct Stack* createStack()
{
struct Stack* stack=(struct Stack*)malloc(sizeof(struct Stack));
stack->head=(struct node*)malloc(sizeof(struct node));;
stack->head->nextlink=NULL;
}
// Stack is empty when head node points to null value
int isEmpty(struct Stack* stack)
{
return stack->head ==NULL;
}
this is my push function.这是我的推送功能。
// Function to add an item to stack. It inserts a node between head pointer, and fist element, and initializes the node that was inserted in between before.
void push(struct Stack* stack, int item)
{
stack->head->data=item;
struct node* intermediate_pointer=(struct node*)malloc(sizeof(struct node));
intermediate_pointer->nextlink=stack->head;
stack->head=intermediate_pointer;
printf("%d pushed to stack\n",item);
}
// Function to remove an item from stack.
void pop(struct Stack* stack)
{
if (isEmpty(stack))
{
printf("warning! stack is empty!\n");
}
else
{
struct node* intermediate_pointer=stack->head;
stack->head=stack->head->nextlink;
free(intermediate_pointer);
}
}
void print_stack(struct Stack* stack)
{
struct node* trans_pointer=stack->head;
while(trans_pointer!=NULL)
{
printf("%d",trans_pointer->data);
trans_pointer=trans_pointer->nextlink;
}
}
//function to respond to user's choice of operation
void stackoper_imp(int ch, struct Stack* stack)
{
if(ch==1)
{
int ele;
printf("enter the integer element you want to push on top of the stack:");
scanf("%d",&ele);
push(stack,ele);
print_stack(stack);
}
else if(ch==2)
{
pop(stack);
print_stack(stack);
}
else
{
return;
}
}
int main()
{
int choice;
struct Stack* stack = createStack();
while(choice!=3)
{
printf("which operation do you want to perform on your stack?\n1.push\n2.pop\n3.exit\n");
scanf("%d",&choice);
stackoper_imp(choice, stack);
}
}
struct Stack* createStack() { struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); stack->head = (struct node*)malloc(sizeof(struct node));; stack->head->nextlink = NULL; }
This is declared as returning struct Stack *
, but then you don't return stack
: the pointer to the struct Stack
you've created.这被声明为返回
struct Stack *
,但是您不返回stack
:指向您创建的struct Stack
的指针。
struct Stack* createStack()
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->head = (struct node*)malloc(sizeof(struct node));;
stack->head->nextlink = NULL;
return stack;
}
Even with this you risk a segmentation fault as malloc
doesn't have to succeed.即使这样,您也可能会出现分段错误,因为
malloc
不必成功。 You'd want to check the return value of it before trying to access member fields.在尝试访问成员字段之前,您需要检查它的返回值。
struct Stack* createStack()
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
if (!stack) return NULL;
stack->head = (struct node*)malloc(sizeof(struct node));;
if (!stack->head) return NULL;
stack->head->nextlink = NULL;
return stack;
}
And then obviously when you use createStack
you'll want to check to ensure it did not return NULL
.然后很明显,当您使用
createStack
时,您需要检查以确保它没有返回NULL
。
createStack()
does not return a pointer to the stack you created, it does not return anything. createStack()
不会返回指向您创建的堆栈的指针,它不会返回任何内容。
You need to return the pointer to the stack by adding: return stack
您需要通过添加将指针返回到堆栈:
return stack
struct Stack* stack= malloc(sizeof(struct Stack));
if(stack == NULL)
{
fprintf(stderr, "Failed to allocate memory for stack\n");
return NULL;
}
struct node* head = malloc(sizeof(struct node));;
stack->head = head;
if(head == NULL)
{
fprintf(stderr, "Failed to allocate memory for node\n");
return NULL;
}
stack->head->nextlink=NULL;
return stack;
Also, malloc()
can fail, so you need to check to ensure that it does not return a NULL
pointer as that will result in undefined behavior.此外,
malloc()
可能会失败,因此您需要检查以确保它不返回NULL
指针,因为这将导致未定义的行为。 However you want to handle these errors outside of the function is entirely up to you.但是,您想在函数之外处理这些错误完全取决于您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.