[英]Getting Segmentation fault, during the implementation of Stack, using linked list
在下面的代碼中,我嘗試使用鏈表來實現堆棧數據結構。 我假設在我的推送函數的某個地方,我已經實現了對指針的不當使用,因為在執行代碼時,編譯器會拋出一個分段錯誤(核心轉儲)
我該如何糾正?
#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;
}
這是我的推送功能。
// 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; }
這被聲明為返回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;
}
即使這樣,您也可能會出現分段錯誤,因為malloc
不必成功。 在嘗試訪問成員字段之前,您需要檢查它的返回值。
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;
}
然后很明顯,當您使用createStack
時,您需要檢查以確保它沒有返回NULL
。
createStack()
不會返回指向您創建的堆棧的指針,它不會返回任何內容。
您需要通過添加將指針返回到堆棧: 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;
此外, malloc()
可能會失敗,因此您需要檢查以確保它不返回NULL
指針,因為這將導致未定義的行為。 但是,您想在函數之外處理這些錯誤完全取決於您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.