Skip to content

Instantly share code, notes, and snippets.

@luk0y
Created March 9, 2019 16:26
Show Gist options
  • Save luk0y/248f78a0a3ca686d8249292409a56dc4 to your computer and use it in GitHub Desktop.
Save luk0y/248f78a0a3ca686d8249292409a56dc4 to your computer and use it in GitHub Desktop.
Creation of stack using Linked list
#include <stdio.h>
#include <stdlib.h>
struct node{
char k;
struct node *addr;
};
struct node *start,*ptr,*nn,*top;
int main(){
struct node *ms;
int y;
char x;
printf("\nEnter how many nodes you want : ");
scanf("%d", &y);
printf("\n\nEnter the data : \n");
for(;y!=0;y--)
{
scanf(" %c",&x);
nn=(struct node *) malloc(sizeof(struct node));
if (nn==NULL)
{
printf("No Space left");
}
else
{
if (start==NULL)
{
start=nn;
nn->k=x;
nn->addr=NULL;
}
else
{
ptr=start;
while(ptr->addr!=NULL)
{
ptr=ptr->addr;
}
ptr->addr=nn;
nn->k=x;
nn->addr=NULL;
}
}
}
ptr=start;
printf("\nLinked list : \n");
while(ptr!=0)
{
printf("%c\n",ptr->k);
ptr=ptr->addr;
}
top=start;
int m;
printf("\n\nSelect 1 for Peek \nSelect 2 for Push\nSelect 3 for pop\n\n-----------------------\n :");
scanf("%d",&m);
switch(m)
{
case 1:
printf("\nPeek Value of the Stack is %c\n\n",top->k);
break;
case 2:
nn=(struct node *) malloc(sizeof(struct node));
printf("Enter the data : ");
char ll;
scanf(" %c",&ll);
nn->k=ll;
nn->addr=top;
start=nn;
ptr=start;
printf("\nNew Linked list : \n");
while(ptr!=0)
{
printf("%c\n",ptr->k);
ptr=ptr->addr;
}
break;
case 3 :
ptr=start;
start=ptr->addr;
free(ptr);
top=start;
ptr=start;
printf("\nNew Linked list : \n");
while(ptr!=0)
{
printf("%c\n",ptr->k);
ptr=ptr->addr;
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment