TO ENTER THE STRINGS IN STACK USING POINTERS

General Programming Languages


/ * TO ENTER THE STRINGS IN STACK USING POINTERS */

/*HEADER FILES****/

#include<stdio.h> /*TO USE scanf() & printf()*/
include<conio.h>/*TO USE getch()*/
include<stdlib.h>/*TO USE exit()*/

/*USING OF STRUCTURE*/
typedef struct node /
* CHANGES NAME OF EXISTING DATA TYPES * /
{
char info[50];
struct node *next;

/*USE OF SELF REFRENTIAL STRUCTURE*
TO POINT TO NEXT NODE*/
}stack;
/
*THIS CHANGES NAME OF STRUCTURE*/ stack *s; / * FUNCTION PROTOTYPES*/
void push(stack *ptr,char info);
void pop(stack *ptr);
void display();

/*STARTING OF main()*/
void main()
{
/*VARIABLE DECLARATION SO AS TO USE THEM LATER IN main()*/
int choice;
char charachter,info;
/
*THIS CLEARS THE SCREEN*/
clrscr();
/*USING OF do-while so that atleast one time loop is executed*/ do { textcolor(91); / * THIS CLEARS THE SCREEN*/
clrscr();
printf(“\nEnter only positive integer values”);

/ * * DISPLAY THE MENU***/
printf(“\nThe operations of stack are:-“);
printf(“\n\t======================================================”);
printf(“\n\n\t\t\t\t MENU\n\n”);
printf(“\n\t======================================================”);
printf(“\n\t\t\t\t1.PUSH”);
printf(“\n\t\t\t\t2.POP”);
printf(“\n\t\t\t\t3.EXIT”);

printf(“\nEnter the choice:- “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
printf(“\n\n================PUSH======================\n”);
/ * CALLING OF push() TO INSERT STRING * /

push(s,info);

break;

case 2: printf(“\n\n=================POP======================\n”);

/ * CALLING OF pop() TO REMOVE STRING**/
pop(s);
break;
case 3:
printf(“\n\n=================EXIT=======================\n”);
exit(0);
break;
default:
printf(“\n\nSorry wrong case”);
}

/ * THIS ASKS WHETHER YOU WANT TO CONTINUE OR NOT * /
printf(“\n\nDo you want to continue”);
printf(“\nIf yes,press Y or y”);
fflush(stdin);/THIS CLEARS THE BUFFER/
scanf(“%c”,&charachter);
}while((charachter==’y’)||(charachter==’Y’));
printf(“\nTHANKS”);
getch();
}
/ * END OF main()***/

/ * FUNCTION DEFINITION OF push() * /

void push(stack *ptr,char info)

{ ptr=(stack *)malloc(sizeof (stack));/ * THIS CREATES THE NODE/ printf(“\nEnter the string-: “); / * THIS CLEARS THE BUFFER**/ fflush(stdin); gets(ptr->info);/ * THIS INPUTS THE STRING * / / * THIS POINTS TO THE STARTING OF STACK * /
ptr->next=s;
s=ptr;

/ * THIS FURTHER CALLS THE display() to show the stack*/
printf(“\n\n——————————————————-\n”);
display();
printf(“\n——————————————————-“);
}

/ * FUNCTION DEFINITION OF pop()***/
void pop(stack *ptr)
{

if(s==NULL)
{
printf(“\n————————————————-“);
printf(“\n\tUNDERFLOW”);
printf(“\n\tStack is empty”);
printf(“\n————————————————–“);
}
else
/ * THIS EXECUTES IF STACK IS NOT EMPTY * /
{
s=s->next;/
* THIS POINTS TO THE NEXT OF NODE*/

 /*********THIS FURTHER CALLS display() to show the stack********/
 printf("\n\n------------------------------------------------------\n");
 display();

  if(s==NULL)
  {
 printf("\n\tAfter deletion ,Stack is empty");
  }
 printf("\n-------------------------------------------------------");

}

}

/ * FUNCTION DEFINITION OF display()*/
void display(stack *ptr)
{
ptr=s;
while(ptr!=NULL)
{ printf(“\t\t\t”);
puts(ptr->info);/THIS PRINTS INFORMATION OF PTR/ ptr=ptr->next;

/ * THIS LINKS PTR TO NEXT OF PTR*/
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.