Write a simple program in C (which comiles in gcc a unix environment ) implementing a stack
using a linked list and a struct holding the linked list, which stores the location of the top and
bottom of the stack.
struct node { int val; //integer node value
struct node *next; //pointer to next node };
struct stack { struct node *head; //pointer to first node in stack
struct node *tail; //pointer to top of stack };
Begin by implementing the following stack operations:
void init(struct stack *); //initialize stack members
void push(struct stack *, int); //grow contents to store int
int pop(struct stack *); //shrink contents and return top int
Include for printf/scanf • Include for malloc/free and NULL • Use the -> to access members
from a struct pointer
Solution
#include <stdio.h>
#include <stdlib.h>
//structure declared
struct node
{
int val;
struct node *next;
}*top;
//method to pop data from stack
void pop()
{
struct node *temp, *stackVar=top;
if(stackVar==top) //removing data from stack
{
top = top->next;
free(stackVar);
}
else{ //if stack empty
printf(" Empty stack");
}
}
//adding number to stack
void push(int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->val=value;
if (top == NULL) //if stack empty
{
top=temp;
top->next=NULL;
}
else //if stack not empty..just append
{
temp->next=top;
top=temp;
}
}
//prinring stack
void display()
{
struct node *stackVar=top;
if(stackVar!=NULL)
{
printf(" Elements are as: ");
while(stackVar!=NULL)
{
printf("t%d ",stackVar->val);
stackVar=stackVar->next;
}
printf(" ");
}
else
printf(" Stack is Empty");
}
int main(int argc, char *argv[])
{
int choice=0;
top=NULL;
//displaying menu
printf(" 1. Push");
printf(" 2. Pop");
printf(" 3. Display");
printf(" 4. Exit ");
while(1)
{
//reading option
scanf("%d",&choice);
switch(choice)
{
//adding number to stack
case 1:
{
int val;
printf(" Enter number to push: ");
scanf("%d",&val);
push(val);
display();
break;
}
//removing number from stack
case 2:
{
pop();
display();
break;
}
//displaying stack
case 3:
{
display();
break;
}
//exit from app
case 4:
{
printf(" Thank you");
break;
}
default:
{
printf(" Enter correct choice: ");
}
}
}
}

Write a simple program in C (which comiles in gcc a unix environment ).docx

  • 1.
    Write a simpleprogram in C (which comiles in gcc a unix environment ) implementing a stack using a linked list and a struct holding the linked list, which stores the location of the top and bottom of the stack. struct node { int val; //integer node value struct node *next; //pointer to next node }; struct stack { struct node *head; //pointer to first node in stack struct node *tail; //pointer to top of stack }; Begin by implementing the following stack operations: void init(struct stack *); //initialize stack members void push(struct stack *, int); //grow contents to store int int pop(struct stack *); //shrink contents and return top int Include for printf/scanf • Include for malloc/free and NULL • Use the -> to access members from a struct pointer Solution #include <stdio.h> #include <stdlib.h> //structure declared struct node { int val; struct node *next; }*top;
  • 2.
    //method to popdata from stack void pop() { struct node *temp, *stackVar=top; if(stackVar==top) //removing data from stack { top = top->next; free(stackVar); } else{ //if stack empty printf(" Empty stack"); } } //adding number to stack void push(int value) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->val=value; if (top == NULL) //if stack empty { top=temp; top->next=NULL; } else //if stack not empty..just append { temp->next=top; top=temp; } } //prinring stack void display() { struct node *stackVar=top; if(stackVar!=NULL) { printf(" Elements are as: "); while(stackVar!=NULL) { printf("t%d ",stackVar->val); stackVar=stackVar->next; } printf(" "); }
  • 3.
    else printf(" Stack isEmpty"); } int main(int argc, char *argv[]) { int choice=0; top=NULL; //displaying menu printf(" 1. Push"); printf(" 2. Pop"); printf(" 3. Display"); printf(" 4. Exit "); while(1) { //reading option scanf("%d",&choice); switch(choice) { //adding number to stack case 1: { int val; printf(" Enter number to push: "); scanf("%d",&val); push(val); display(); break; } //removing number from stack case 2: { pop(); display(); break; } //displaying stack case 3: { display(); break; } //exit from app case 4: { printf(" Thank you");
  • 4.