Implementation of Stack by linked list - c

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;

Related

pushing data into stack using LinkedList

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
struct nodeStack{
char operator;
struct nodeStack *next;
};
typedef struct nodeStack node;
node *start=NULL;
node *tail=NULL;
int top=-1;
int isEmpty()
{
if(top==-1)
return 1;
}
void push(char c){
node *tempNode,*tail;
tempNode=(node*) malloc(sizeof(node));
if(tempNode==NULL){
printf("Memory Unvailable\n");
return;
}
tempNode->operator=c;
if(start==NULL){
start=tempNode;
tail=start;
tempNode->next=NULL;
top++;
}
else{
tail->next=tempNode;
tempNode->next=NULL;
tail=tail->next;
top++;
}
}
/*
struct node* pop(){
if(top==-1){
printf("stack is empty");
return;
}
else
{
node *temp;
temp=start;
while(temp->next!=tail){
temp->next=NULL;
free(tail);
tail=temp;
}
}
}*/
void displayStack(){
node *i;
for(i=start;i!=tail;i=i->next){
printf("%c -> ",i->operator);
}
}
int main(){
int i;
int flag=1;
char choice='y';
printf("pushing data into the stack......");
while(flag==1){
char ch;
printf("enter a character\n");
scanf(" %c",&ch);
push(ch);
printf("want to push more operator (y\n)");
scanf(" %c",choice);
if(choice=='y')
flag=1;
else
flag=0;
}
displayStack();
return 0;
}
Its giving me segmentation error when I am trying to run this.
It is accepting only one input and not taking further and at the same time it is giving segmentation error
Its giving me segmentation error when I am trying to run this.
It is accepting only one input and not taking further and at the same time it is giving segmentation error
Take a look at:
scanf(" %c",choice);
The second parameter should be passed as address, because scanf modifies it.
Try to modify with:
scanf("%c", &choice);
I'm not going to explain the reason in detail, because I think you just made a mistake since in some previous lines scanf(" %c",&ch); you've invoke right.
Note: I'd avoid the space in the scanf string format.
This is a bit modified code! Working code!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct nodeStack{
char op;
struct nodeStack *next;
}node;
node *start = NULL;
node *tail = NULL;
int top=-1;
int isEmpty()
{
if(top==-1)
return 1;
else return 0;
}
void push(char c){
node *tempNode;
tempNode=(node*) malloc(sizeof(node));
tempNode->op=c;
if(start==NULL){
//start = (node*) malloc(sizeof(node));
start=tempNode;
tail=start;
tempNode->next=NULL;
top++;
}
else{
tail->next=tempNode;
tempNode->next=NULL;
tail=tail->next;
top++;
}
}
/*
struct node* pop(){
if(top==-1){
printf("stack is empty");
return;
}
else
{
node *temp;
temp=start;
while(temp->next!=tail){
temp->next=NULL;
free(tail);
tail=temp;
}
}
}*/
void displayStack(){
node *i;
for(i=start;i!=NULL;i=i->next){
printf("%c -> ",i->op);
}
}
int main(){
int i;
int flag=1;
char choice='y', ch;
printf("pushing data into the stack......");
start=NULL; tail=NULL;
while(flag){
printf("enter a character\n");
scanf(" %c",&ch);
push(ch);
printf("want to push more operator (y/n)");
scanf(" %c",&choice);
if(choice=='y')
flag=1;
else
flag=0;
}
displayStack();
return 0;
}

Can't push after using pop

I've been at this for about 7 hours trying to get this to work on my own, but I can't figure it out. With my testing earlier I was able to successfully fully push all the values of my char[] into the stack and I could properly pop them off and return the value.
However, if I try to push something back onto the stack then the whole program crashes.
Notes:
The code is supposed to be used to create nodes for an Expression Tree later on
The commented out code in the switch statement is code that I had when the error occurred, was just saving it for when I could have fixed it.
-When pushing after using a pop, the function is properly called, however it does not enter either the if or the else statement within push.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Stack;
typedef struct StackNode;
typedef struct StackPtr;
struct StackPtr
{
struct StackPtr *prev;
struct StackNode *ptr;
};
struct StackNode
{
char c;
struct StackNode *lchild;
struct StackNode *rchild;
};
struct Stack
{
struct StackPtr *top;
};
// Prototypes
void initStack(struct Stack*);
struct StackNode* createNode(char);
struct StackNode* pop(struct Stack*);
void push(struct Stack*, struct StackNode*);
int isEmpty(struct Stack*);
int main()
{
struct Stack *s = malloc(sizeof(s));
initStack(s);
char exp[7] = "45+67+*";
int length = 0;
for(length; length<3; length++) // push the expression onto the stack
{
struct StackNode *exp1 = malloc(sizeof(exp1));
struct StackNode *exp2 = malloc(sizeof(exp2));
struct StackNode *c = malloc(sizeof(c));
if(exp[length] > 47 && exp[length] < 58) // is a number
{
c->c = exp[length];
push(s,c);
}
else
{
switch(exp[length])
{
default:
printf("exp[length] error\n");
break;
case '+':
exp2 = pop(s);
exp1 = pop(s);
c->c = '7';
push(s, c);
//push(s, exp1->ptr);
//push(s, exp2->ptr);
//c->c = exp[length];
//c->rchild = exp2;
//c->lchild = exp1;
//printf("test ");
//push(s, c);
//printf("test ");
break;
case '-':
break;
case '*':
break;
case '/':
break;
}
}
}
return 0;
}
void initStack(struct Stack *s)
{
struct StackNode *n = malloc(sizeof(n));
struct StackPtr *p = malloc(sizeof(p));
n->c = NULL;
n->lchild = NULL;
n->rchild = NULL;
p->prev = NULL;
p->ptr = n;
s->top = p;
}
struct StackNode* createNode(char c)
{
struct StackNode *n = malloc(sizeof(n));
n->c = c;
n->lchild = NULL;
n->rchild = NULL;
return n;
}
void push(struct Stack *s, struct StackNode *n)
{
if(s->top->ptr->c == NULL) // First item being pushed
{
s->top->ptr = n;
printf("1Added: %c to the stack\n", n->c);
}
else
{
struct StackPtr *o = malloc(sizeof(o));
o->prev = s->top;
s->top = o;
s->top->ptr = n;
printf("2Added: %c to the stack\n", n->c);
}
}
struct StackNode* pop(struct Stack *s)
{
if(isEmpty(s) == 1)
{
printf("Stack is empty!\n");
return;
}
else
{
struct StackPtr *tmp = malloc(sizeof(tmp));
tmp = s->top;
s->top = s->top->prev;
printf("Popped: %c from the stack.\n",tmp->ptr->c);
return tmp->ptr;
}
}
int isEmpty(struct Stack *s)
{
if(s->top == NULL)
return 1;
else
return 0; // false
your POP function is setting
s->top = s->top->prev;
after two nodes are popped out the Prev value is NULL so top is pointing to NULL, which is causing the segmentation fault during the next PUSH
Few more things to consider, change value to 8 instead of 7 as \0 needs to be fit. or use it as
char exp[]
char exp[8] = "45+67+*";

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;
}

C Stack Error Linked LIst

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

Linked List Push Function

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct stackNode
{
int data;
struct stackNode *nextPtr;
};
void instructions()
{
printf("[1]Push a value on the stack\n");
printf("[2]Pop a value off the stack\n");
printf("[3]Display the whole stack\n");
printf("[4]Exit");
}
void push(struct stackPtr *topPtr, int info)
{
struct stackPtr *newPtr;
newPtr= malloc(sizeof(struct stackNode));
if(newPtr !=NULL)
{
newPtr->data = info;
newPtr->nextPtr=*topPtr;
*topPtr=newPtr;
}
else
{
printf("%d not inserted no memory available");
}
int main()
{
struct StackNodePtr *stackPtr;
stackPtr = NULL;
int choice, value;
do
{
instructions();
printf("\nEnter Your Choice: ");
scanf("%d",&choice);
if(choice == 1)
{
printf("Enter a value for the stack");
}
if(choice == 2)
{
printf(" ");
}
if(choice == 3)
{
printf(" ");
}
if(choice == 4 )
{
printf("bye!");
return 0;
}
} while(choice !=4);
system("pause");
}
I made a function push for my linked list and stack code but the thing is it's not working there are Enormous errors in the function push what's wrong with it? it's not allowing the malloc to be used why is that?
Does this work for you?
struct stackNode { int data; struct stackNode *nextPtr; };
int main() { struct stackNode * stackPtr = NULL; }
struct stackNode
{
int data;
struct stackNode *nextPtr;
};
int main()
{
struct stackNode *stackPtr = NULL;
}
// just precede the struct type with ... struct
struct stackNode* stackPtr = NULL;
Happy coding.
This line:
typedef struct StackNode *StackNodePtr;
is wrong by the way. Your typedefs should be:
typedef struct stackNode StackNode;
typedef StackNode* StackNodePtr;
No idea why you're against using typedef - it tends to make the code a lot more readable.

Resources