C Program For Stack using Linked List

/*Stack using Linked List*/


#include<stdio.h>
#include<conio.h>
struct node
{
int n;
struct node *next;
};
main()
{
struct node *s;
clrscr();
s=NULL;
printf("1.Create\n");
printf("2.Display\n");
printf("3.Push\n");
printf("4.Pop\n");
printf("5.Isempty\n");
printf("6.Exit\n");
printf("Enter the choice");
scanf("%d",&ch);
 switch(ch)
 {
 case 1:s=create(s);
  break;
 }
}

struct node *create(struct node *x)
{
 if(x==NULL)
 {
 x=(struct node*)malloc(sizeof(struct node));
 printf("Enter the number");
 scanf("%d",&x->n);
 x->next=NULL;
 return x;
 }
 else
 {
 printf("The node already created\n");
 return x;
 }
}

void display(struct node *x)
{
curr=x;
 while(curr!=NULL)
 {
 printf("%d->",curr->n);
 curr=curr->next;
 }
}
void push(struct node *x)
{
curr=x;
 while(curr->next!=NULL)
 {
 curr=curr->next;
 }
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the number");
scanf("%d",&temp->n);
temp->next=NULL
curr->next=temp;
}
struct node* pop(struct node *x)
{
curr=x;
 if (curr==NULL)
 {
 printf("No node to be poped\n");
 return x;
 }
 else if (curr->next==NULL)
 {
 printf("u r poping final node\n");
 free(curr);
 x=NULL;
 return x;
 }
 else
 {
  while(curr->next!=NULL)
  {
  prev=curr;
  curr=curr->next;
  }
 prev->next=NULL;
 free(curr);
 return x;
 }
}

void isempty(struct node *x)
{
 if (x==NULL)
 {
 printf("Stack is empty\n");
 }
 else
 {
 printf("Stack is not empty\n");
 }
}
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment