STACK In C - Programming Languages

Latest

Wednesday 1 February 2017

STACK In C

                                      STACK




CONTENT 1. Implement a stack
2. Reverse a Stack using Recursion
3. Reverse a Stack without using Recursion

----------------------------------------------------------------------------------------
1. solution:-

#include<stdio.h>
#include<conio.h>
#define MAXSIZE 5

struct stack
{
int stk[MAXSIZE];
int top;
};
typdef struct stackSTACK;
STACK s;

void push(void);
intt pop(void);
void display(void);

void main()
{
int choice;
int option=1;
s.top=-1;
clrscr();
printf("STACK OPERATION");
while(option)
{
printf("--------------------------------------");
printf("1->push\n");
printf("2->pop\n");
printf("3->display\n");
printf("4->exit\n");
printf("-------------------------------------");
printf("enter your option\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
default:
printf("you have enter wrong input\n");
break;
}
fflush(stdin);
printf(do you want to continue (type 0 or 1)?\n");
scanf("%d",&option);
}
getch();
}
 void push()
{
int num;
if(s.top==(MAXSIZE-1))
{
printf("stack is full\n");
return;
}
else
{
printf("enter the element to be pushed \n");
scanf("%d",&num);
s.top=s.top+1;
s.stk[a.top]=num;
}
return;
}
/*function to delete an element from stack*/
int pop()
{
int num;
if(s.top==-1)
{
printf("stack is empty\n");
return(s.top);
}
else
{
num=s.stk[s.top];
printf("poped element is=%d\n",s.stk[s.top]);
s.top=s.top-1;
}
return(num);
}
/*function to display the status of stack*/
void display()
{
int i;
if(s.top==-1)
{
printf("stack is empty\n");
return;
}
else
{
printf(the status of the stack is \n");
for(i=s.top;i>=0;i--)
{
printf("%d\n",s.stk[i]);
}
}
printf(" ");
}





2. solution

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
int a;
struct node *next;
};
void generate(struct node**);
void display(struct node*);
void stack_reverse(struct node**,struct node**);
void delete(struct node**0;

int main()
{
struct node *head=NULL;
generate(&head);
printf("the sequence of contects in stack\n");
display(head);
printf(""inversing the contents of the stack\n");
if(head!=NULL)
{
stack_reverse(&head,&(head->next));
}
printf("the contents in stack after reversal\n");
display(head);
delete(&head);
return 0;
}
void stack_reverse(struct node **head,struct node **head_next)
{
struct node *temp;
if(*head_next!=next)
{
temp=(*head_next)->next;
(*head_next)->next=(*head);
*head=*head_next;
*head_next=temp;
stack_reverse(head,head_next);
}
}

void display(struct node *head)
{
if(head!=NULL)
{
printf("%d",head->a);
display(head->next);
}
}
void generate(struct node **head)
{
int num,i;
struct node *temp;
printf("enter length of list \n");
scanf("%d",&num);
for(i=num;i>0;i--)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->a=i;
if(*head==NULL)
[
*head=temp;
(*head)->next=NULL;
}
else
{
temp->next=*head;
*head=temp;
}
}
}
void delete(struct node **head)
{
struct node *temp;
while(*head!=NULL)
{
temp=*head;
*head=(*head)->next;
free(temp);
}
}


3. solution


#include <stdio.h>

#include <stdlib.h>

struct node
{
int a;
struct node *next;
};

void generate(struct node **);
void display(struct node *);
void stack_reverse(struct node **);
void delete(struct node **);

int main()
{
struct node *head = NULL;

generate(&head);
printf("\nThe sequence of contents in stack\n");
display(head);
printf("\nInversing the contents of the stack\n");
stack_reverse(&head);
printf("\nThe contents in stack after reversal\n");
display(head);
delete(&head);
return 0;
}

void stack_reverse(struct node **head)
{
struct node *temp, *prev;
 if (*head == NULL)
{
printf("Stack does not exist\n");
}
esle if ((*head)->next == NULL)
{
pintf("Single node stack reversal brings no difference\n");
}
else if ((*head)->next->next == NULL)
{
(*head)->next->next = *head;
*head = (*head)->next;
(*head)->next->next = NULL;
}
else
{
prev = *head;
temp = (*head)->next;
*head = (*head)->next->next;
prev->next = NULL;
while ((*head)->next != NULL)
{
temp->next = prev;
prev = temp;
temp = *head;
*head = (*head)->next;
}
temp->next = prev;
(*head)->next = temp;
}
}

void display(struct node *head)
{
if (head != NULL)
{
printf("%d  ", head->a);
display(head->next);
}
}

void generate(struct node **head)
{
int num, i;
struct node *temp;

printf("Enter length of list: ");
scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}

void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}

No comments:

Post a Comment