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, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors and Social Networking. Feel Free to let me know any of your concerns about hacking or let me know if you need any more methods on hacking anything. Enjoy Learning

No comments:

Post a Comment