Example: A data structure stack example/program using C to create a Stack with user-defined nodes to store records with at least two attributes of employee and then display all the stored records.
#include <stdio.h>
#include <conio.h>
#include <malloc.h> //#include <stdlib.h>
struct emp
{
int empsal;
char empname[50];
struct emp *ptr;
};
struct emp *top, *link, *newnode;
void main()
{
int i, num;
printf("Enter no. of nodes you want to create in a stack\n");
scanf("%d",&num);
printf("Enter %d records of Employee as Employee Salary and Employee name one by one =\n",num);
//first Node creation code
top= (struct emp*)malloc(sizeof(struct emp));
scanf("%d%s",&top->empsal,top->empname);
top->ptr=NULL;
//remaining Node creation code
for(i=1;i<num;i=i+1)
{
newnode= (struct emp*)malloc(sizeof(struct emp));
scanf("%d%s",&newnode->empsal,newnode->empname);
newnode->ptr=NULL;
newnode->ptr=top;
top=newnode;
}
//Display/Print/Traverse code
printf("\nThe Output of Stack are = \n");
link=top;
while(link!=NULL)
{
printf("%d %s\n",link->empsal,link->empname);
link=link->ptr;
}
getch();
}
Output:
Enter no. of nodes you want to create in a stack
5
Enter 5 records of Employee as Employee Salary and Employee name one by one =
50000 A
32000 B
80000 C
32500 D
65000 E
The Output of Stack are =
65000 E
32500 D
80000 C
32000 B
50000 A
Example: A data structure stack example/program using C to insert a new node in an existing Stack [Push Operation] and then display all the stored records.
#include <stdio.h>
#include <conio.h>
#include <malloc.h> //#include <stdlib.h>
struct emp
{
int empsal;
char empname[50];
struct emp *ptr;
};
struct emp *top, *link, *newnode;
void main()
{
int i, num;
printf("Enter no. of nodes you want to create in a stack\n");
scanf("%d",&num);
printf("Enter %d records of Employee as Employee Salary and Employee name one by one =\n",num);
//first Node creation code
top= (struct emp*)malloc(sizeof(struct emp));
scanf("%d%s",&top->empsal,top->empname);
top->ptr=NULL;
//remaining Node creation code
for(i=1;i<num;i=i+1)
{
newnode= (struct emp*)malloc(sizeof(struct emp));
scanf("%d%s",&newnode->empsal,newnode->empname);
newnode->ptr=NULL;
newnode->ptr=top;
top=newnode;
}
//Display/Print/Traverse code
printf("\nThe Output of Stack before insertion are = \n");
link=top;
while(link!=NULL)
{
printf("%d %s\n",link->empsal,link->empname);
link=link->ptr;
}
// creation of single node and insertion in stack
printf("\nEnter new record of Employee as Employee Salary and Employee name =\n");
newnode=(struct emp*)malloc(sizeof(struct emp));
scanf("%d%s",&newnode->empsal,newnode->empname);
newnode->ptr=NULL;
newnode->ptr=top;
top=newnode;
printf("\nThe Output of Stack after insertion are = \n");
link=top;
while(link!=NULL)
{
printf("%d %s\n",link->empsal,link->empname);
link=link->ptr;
}
getch();
}
Output :
Enter no. of nodes you want to create in a stack
3
Enter 3 records of Employee as Employee Salary and Employee name one by one =
2000 A
1000 C
5000 B
The Output of Stack before insertion are =
5000 B
1000 C
2000 A
Enter new record of Employee as Employee Salary and Employee name =
6000 M
The Output of Stack after insertion are =
6000 M
5000 B
1000 C
2000 A
Example: A data structure stack example/program using C to delete a node from an existing Stack [Pop Operation] and then display all the stored records.
#include <stdio.h>
#include <conio.h>
#include <malloc.h> //#include <stdlib.h>
struct emp
{
int empsal;
char empname[50];
struct emp *ptr;
};
struct emp *top, *link, *newnode;
void main()
{
int i, num;
printf("Enter no. of nodes you want to create in a stack\n");
scanf("%d",&num);
printf("Enter %d records of Employee as Employee Salary and Employee name one by one =\n",num);
//first Node creation code
top= (struct emp*)malloc(sizeof(struct emp));
scanf("%d%s",&top->empsal,top->empname);
top->ptr=NULL;
//remaining Node creation code
for(i=1;i<num;i=i+1)
{
newnode= (struct emp*)malloc(sizeof(struct emp));
scanf("%d%s",&newnode->empsal,newnode->empname);
newnode->ptr=NULL;
newnode->ptr=top;
top=newnode;
}
//Display/Print/Traverse code
printf("\nThe Output of Stack records before deletion are = \n");
link=top;
while(link!=NULL)
{
printf("%d %s\n",link->empsal,link->empname);
link=link->ptr;
}
//Deletion code
link=top;
top=top->ptr;
free(link);
printf("\nThe Output of Stack records after deletion are = \n");
link=top;
while(link!=NULL)
{
printf("%d %s\n",link->empsal,link->empname);
link=link->ptr;
}
getch();
}
Output :
Enter no. of nodes you want to create in a stack
5
Enter 5 records of Employee as Employee Salary and Employee name one by one =
2000 A
6000 G
7000 X
9000 N
4000 K
The Output of Stack records before deletion are =
4000 K
9000 N
7000 X
6000 G
2000 A
The Output of Stack records after deletion are =
9000 N
7000 X
6000 G
2000 A
0 Comments