C Stack Error Linked LIst - c

There are huge amounts of error, Seriously what's wrong ?
I tried using it wihout typedef but what's the problem? can anyone help me debug this please?
struct node {
int info;
struct node *link;
};
int main (void)
{
int choice;
struct node *top;
top = NULL;
while (1) {
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}
}
return 0;
}
void push (void)
{
struct node *tmp;
int pushed_item;
tmp = malloc(sizeof(struct node));
printf("Input the new value to be pushed on the stack : ");
scanf("%d", &pushed_item);
tmp->info = pushed_item;
tmp->link = top;
top = tmp;
}
void pop (void)
{
struct node *tmp;
if (top == NULL)
printf("Stack is empty\n");
else {
tmp = top;
printf("Popped item is %d\n", tmp->info);
top = top->link;
free(tmp);
}
}
void display (void)
{
struct node *ptr;
ptr = top;
if (top == NULL)
printf("Stack is empty\n");
else {
printf("Stack elements :\n");
while (ptr != NULL) {
printf("%d\n", ptr->info);
ptr = ptr->link;
}
}
}

First off, the main function is going to have to be below the functions it calls. Secondly, you need to #import <stdio.h> to use printf. Thirdly, top is not a global variable so you can't just use it inside the display function.
Work from there.

Try putting this snippet at the top of your file:
#include <stdio.h>
#include <stdlib.h>
void push(void);
void pop(void);
void display(void);
struct node* top; // good catch by Borealid

Related

difficulty in creating linked list

In this programm, when I create a list then it creates list successfully. But when I try to print it the program is just displaying last two values of node. I debug many times and found (*temp)->next changes the start1 and start2 pointer. I'm unable to solve how temp pointer changes value in start1 and start2.
Compiler didn't produce any error or warning.
#include <stdio.h>
struct node {
int info;
struct node *next;
} *start1 = NULL, *start2 = NULL;
void create_node(struct node **s1);
void display(struct node **s);
void create_node(struct node **s1)
{
struct node *ptr = NULL, **temp = s1;
ptr = (struct node *)malloc(sizeof(struct node));
if (*s1 == NULL)
{
*s1 = ptr;
} else
{
while ((*temp)->next != NULL)
(*temp) = (*temp)->next;
(*temp)->next = ptr;
}
ptr->next = NULL;
printf("enter the value\n");
scanf("%d", &(ptr->info));
}
void display(struct node **s)
{
struct node **temp = s;
while ((*temp) != NULL)
{
printf("%d\t", (*temp)->info);
(*temp) = (*temp)->next;
}
}
void main()
{
int choice = 0;
while (1){
clrscr();
printf("enter your choice\n");
printf("enter 1 to create_node1\nenter 2 to create node 2 \nenter 3 to display node 1\nenter 4 to display node2\nenter 5 to exit\n");
printf("\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
create_node(&start1);//correct//
break;
case 2:
create_node(&start2);
break;
case 3:
display(&start1);
getch();
break;
case 4:
display(&start2);
getch();
break;
case 5:
exit(1);
break;
default:
printf("invalid");
}
}
}
Use this
while ((*temp)->next != NULL) (*temp) = (*temp)->next;.
This will solve the problem.
Otherwise, the index out of memory is accessed and program crash.

Linked List implementation of Stack

I am trying to implement stack as a linked list. Here is my current code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
typedef struct node {
int data;
struct node* link;
} Node;
typedef Node* list;
list head;
void push(list top, int item) {
if (head == NULL) {
head = (list) malloc(sizeof(Node));
head->data = item;
head->link = NULL;
top = head;
} else{
list temp = (list) malloc(sizeof(Node));
temp->data = item;
temp->link = top;
top = temp;
}
}
int pop(list top) {
if (top == NULL) {
printf("stack is empty");
/*int tdata=top->data;
top=top->link;
return tdata;*/
} else {
int tdata = top->data;
top = top->link;
return tdata;
}
}
void display(list top){
while (top != NULL) {
printf("%d", top->data);
top = top->link;
}
}
int main() {
int ch, item;
list top;
for (;;) {
printf("1.push\t2.pop\t3.display\t4.exit");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("enter the element to be pushed");
scanf("%d",&item);
push(top,item);
break;
case 2:
item=pop(top);
printf("element popped is: %d",item);
break;
case 3:
display(top);
break;
case 4:
exit(0);
break;
default:
printf("enter valid choice");
}
}
}
When I press '2' the pop method is called, but irrespective of whatever item is on the top it prints the message "element popped is: 11". When I press '3' for the display method, I get "segmentation fault(core dumped)". Why is this happening? What modifications are needed to get this code working?
I have made several alterations to your program. The most important is to pass a pointer to the list head to functions, which itself is a pointer, so that it can be altered by the function.
I also removed the global head and initialised the local top. I have commented in the code about other matters.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *link;
} Node; // removed pointer type to Node
void push(Node **top, int item) { // takes address of the pointer
Node *temp= malloc(sizeof(Node)); // do not cast malloc
if(temp == NULL) // check that malloc did work
exit(42);
temp->data = item; // no need for separate clause at first push
temp->link = *top; // link is previous top
*top = temp; // top is new struct, pass it back
}
int pop(Node **top) { // takes address of the pointer
int tdata;
Node *temp;
if (*top == NULL) {
printf("stack is empty\n"); // added newline here and other places
return 0;
}
tdata = (*top)->data; // collect the data
temp = (*top)->link; // remember the next list item
free(*top); // give memory back
*top = temp; // pass new top back to caller
return tdata;
}
void display(Node *top){
while (top != NULL) {
printf("%d ", top->data); // added spacing
top = top->link;
}
printf("\n"); // added newline
}
int main(void) {
int ch, item;
Node *top = NULL; // initialise the list !!
do {
printf("1.push 2.pop 3.display 4.exit ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("enter the element to be pushed ");
scanf("%d",&item);
push(&top,item); // pass address so pointer can be updated
break;
case 2:
item=pop(&top); // pass address so pointer can be updated
printf("element popped is: %d\n",item);
break;
case 3:
display(top);
break;
case 4:
break;
default:
printf("enter valid choice\n");
}
} while(ch != 4); // changed the loop style
}
What is happening is that you simply do not initialize or set any value to your list pointer top.Take a good look at your insertion function push. It receives a pointer. If you want to send your member top as an [out] paramter to this function you should send a pointer to a pointer (**) or a reference to a pointer (*&). Sending top like this does not modify its value and leaves it with junk as this variable was also never initialized...

How can I create this linked list with stack in C?

I can create the linked list. But I could not managed to create the stack with it. (Stack cannot be more than 5, and it can be empty as shown in the link). How can I do it? (C language but C++ functions like new int are allowed)
The structure could be something like:
struct linkedStack {
int elements[5];
int top;
struct linkedStack *next;
};
Then manage the stack with top (equals to zero at the beginning)...
Post your code please so we can understand what you are trying to make.
This example can help you
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<alloc.h>
void Push(int, node **);
void Display(node **);
int Pop(node **);
int Sempty(node *);
typedef struct stack {
int data;
struct stack *next;
} node;
void main() {
node *top;
int data, item, choice;
char ans, ch;
clrscr();
top = NULL;
printf("\nStack Using Linked List : nn");
do {
printf("\n\n The main menu");
printf("\n1.Push \n2.Pop \n3.Display \n4.Exit");
printf("\n Enter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the data");
scanf("%d", &data);
Push(data, &top);
break;
case 2:
if (Sempty(top))
printf("\nStack underflow!");
else {
item = Pop(&top);
printf("\nThe popped node is%d", item);
}
break;
case 3:
Display(&top);
break;
case 4:
printf("\nDo You want To Quit?(y/n)");
ch = getche();
if (ch == 'y')
exit(0);
else
break;
}
printf("\nDo you want to continue?");
ans = getche();
getch();
clrscr();
} while (ans == 'Y' || ans == 'y');
getch();
}
void Push(int Item, node **top) {
node *New;
node * get_node(int);
New = get_node(Item);
New->next = *top;
*top = New;
}
node * get_node(int item) {
node * temp;
temp = (node *) malloc(sizeof(node));
if (temp == NULL)
printf("\nMemory Cannot be allocated");
temp->data = item;
temp->next = NULL;
return (temp);
}
int Sempty(node *temp) {
if (temp == NULL)
return 1;
else
return 0;
}
int Pop(node **top) {
int item;
node *temp;
item = (*top)->data;
temp = *top;
*top = (*top)->next;
free(temp);
return (item);
}
void Display(node **head) {
node *temp;
temp = *head;
if (Sempty(temp))
printf("\nThe stack is empty!");
else {
while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}
getch();
}

implementing stack with linked list in C

I'm having trouble implementing a Stack using a linked list with struct. The program compiles fine but when I run it, it prints the first element but then reads the next node as a NULL. I think it might be an error with my passing of the stack to the push method but I am not sure and I have not been successful in fixing it so I'm asking for your help:
#include <stdio.h>
#include <stdlib.h>
struct stackNode{
char data;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
void convertToPostfix(char infix[], char postfix[]);
int isOperator(char c);
int precedence(char operator1, char operator2);
void push(StackNodePtr *topPtr, char value);
char pop(StackNodePtr *topPtr);
char stackTop(StackNodePtr topPtr);
int isEmpty(StackNodePtr topPtr);
void printStack(StackNodePtr topPtr);
int main(){
convertToPostfix(NULL, NULL);
return 0;
}
void convertToPostfix(char infix[], char postfix[]){
StackNode stack = {'(', NULL};
StackNodePtr stackPtr = &stack;
push(stackPtr, 'a');
//printf("%s\n", stackPtr->data);
printStack(&stack);
}
void push(StackNodePtr *topPtr, char value){
StackNode *node;
node=(StackNodePtr)malloc(sizeof(StackNodePtr));
node->data=value;
node->nextPtr=*topPtr;
*topPtr=node;
}
void printStack(StackNodePtr topPtr){
if(topPtr == NULL){
printf("%s\n", "NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
return;
}
printf("%c\n", topPtr->data);
printStack(topPtr->nextPtr);
}
Any help would be appreciated.
Thanks
Several problems I could see:
1) printStack(&stack); should be printStack(stackPtr); as you are passing address of stackPtr to the push function.
2)
node = (StackNodePtr)malloc(sizeof(StackNodePtr));
should be:
node = malloc(sizeof(StackNode));
3)
push(stackPtr, 'a');
should be:
push(&stackPtr, 'a');
As you need to pass the address of the top pointer.
This is incorrect:
node=(StackNodePtr)malloc(sizeof(StackNodePtr));
as it is only allocating memory for a struct stackNode* (commonly 4-bytes for any pointer type), when it should be allocating memory for a struct stackNode (at least 5 bytes):
node = malloc(sizeof(StackNode));
--
See Do I cast the result of malloc?
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * link;
};
void push(struct node **, int);
int pop(struct node **);
void display(struct node *);
void printMenu();
int main() {
struct node * p;
p = NULL;
int data, ch, data1;
//char choice[10];
do {
printMenu();
printf("Enter your choice\n");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to be pushed\n");
scanf("%d", &data);
push(&p, data);
break;
case 2:
data1 = pop(&p);
if (data1 != -1000)
printf("The popped element is %d\n", data1);
break;
case 3:
printf("The contents of the stack are");
display(p);
printf("\n");
break;
default:
return 0;
}
} while (1);
return 0;
}
void printMenu() {
printf("Choice 1 : Push\n");
printf("Choice 2 : Pop\n");
printf("Choice 3 : Display\n");
printf("Any other choice : Exit\n");
}
void push(struct node **q, int num) {
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->link = (*q);
temp->data = num;
(*q) = temp;
}
void display(struct node *q) {
//Fill in the code here
struct node *temp = q;
if (temp == NULL)
printf(" {}");
while (temp != NULL)
{
printf(" %d", temp->data);
temp = temp->link;
}
}
int pop(struct node **q) {
//Fill in the code here
struct node *temp;
int item;
if (*q == NULL)
{
printf("Stack is empty\n");
return -1000;
}
temp = *q;
item = temp->data;
*q = (*q)->link;
free(temp);
return item;
}

Implementation of Stack by linked list

I am getting below error message.
Could not able to solve it. Googled a lot. Finally thought of to put it here.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int stop;
struct stack
{
int data;
struct stack *next;
};
typedef struct stack *node, *top;
//typedef struct stack *top;
void push()
{
int i;
struct stack *x;
x = malloc(sizeof(struct stack));
printf("\n Enter the element your want to insert");
scanf("%d", &i);
x->data = i;
x->next = top;
top = x;
}
void pop()
{
int i;
if(top == NULL)
{
printf("\nStack is empty\n");
}
else{
i = top->data;
free(top);
top = top->next;
}
}
void display()
{
if(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
int main()
{
int ch;
while(1)
{
printf("\nEnter your option \n1. Insert(Push) \n2. Delete(Pop) \n3. Display : \n");
scanf("%d", &ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("Invalid Entery, Try Again");
}
}
return 0;
}
remove typedef and all will be fine.
You don't want a new type:
typedef struct stack *node, *top;
Instead, you want a new variable:
struct stack *node, *top;
You cannot combine variable declarations with typedef. If you would like to create an alias for struct stack and call it simply stack, modify your code as follows:
struct stack {
int data;
struct stack *next;
};
typedef struct stack stack;
stack *node, *top;

Resources