0% found this document useful (0 votes)
112 views16 pages

1-Write A Program To Implement A Stack, Show Overflow and Underflow While Performing Push and Pop Operations Respectively

The document contains 5 programs that implement different data structures using C programming language: 1. A stack program that demonstrates push and pop operations and handling of overflow and underflow. 2. A queue program that demonstrates insertion and deletion of elements and handling of overflow. 3. A linked list program that implements creation, insertion, deletion, searching and display of elements. 4. A program that increments the data of each node in a linked list by 10. 5. A circular queue program that demonstrates insertion and deletion of elements.

Uploaded by

Nazia Ajaz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views16 pages

1-Write A Program To Implement A Stack, Show Overflow and Underflow While Performing Push and Pop Operations Respectively

The document contains 5 programs that implement different data structures using C programming language: 1. A stack program that demonstrates push and pop operations and handling of overflow and underflow. 2. A queue program that demonstrates insertion and deletion of elements and handling of overflow. 3. A linked list program that implements creation, insertion, deletion, searching and display of elements. 4. A program that increments the data of each node in a linked list by 10. 5. A circular queue program that demonstrates insertion and deletion of elements.

Uploaded by

Nazia Ajaz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1-Write a program to implement a Stack, show overflow and underflow while performing push and pop operations respectively

#include<stdio.h> #include<conio.h> #include<stdlib.h> void push(int); int pop( ); void display( ); int a[20], top; void main( ) { int ch, data; char ans; top=-1; while(1) { clrscr( ); printf("\n \t Implementing stack using array"); printf("\n\t--------------------------------------"); printf("\n\t 1- Push an element into stack "); printf("\n\t 2- Pop an element from stack "); printf("\n\t 3- Display the stack "); printf("\n\t 4- Exit from output window "); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter element to be pushed into the stack"); scanf("%d", &data); push(data); break; case 2: data= pop( ); if(data != -99) printf("\n Element popped is :%d", data); getch( ); break; case 3: display( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default:

printf("\n Invalid choice ....."); getch( ); break; } } } void push(int x) { if(top == 19) printf("\n\n Stack overflow"); else { top++; a[top] = x; } } int pop( ) { int dummy; if(top == -1) { printf("\n\n Stack underflow"); getch( ); return(-99); } else { dummy= a[top]; top--; return(dummy); } } void display( ) { int i; if(top == -1) printf("\n\n Stack is empty"); else { printf("\n\n Elements in stack :- "); for(i= top; i >= 0; i--) printf("\n %d", a[i]); } getch( ); }

2- Write a program to implement a queue and show the following: insertion and deletion. #include<stdio.h> #include<conio.h> #include<stdlib.h> void insert(int); int del( ); void display( ); int a[20], rear, front; void main() { int ch, data; char ans; front= -1; rear= -1; while(1) { clrscr( ); printf("\n\t 1- Insert an element into queue "); printf("\n\t 2- Delete an element from the queue "); printf("\n\t 3- Display the queue "); printf("\n\t 4- Exit "); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter the element to be inserted intothe simple queue:"); scanf("%d",&data); insert(data); break; case 2: data=del( );

if(data != -99) printf("\n Element deleted is :- %d", data); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 3: display( ); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans=='y' || ans=='Y') exit(0); else break; default: printf("\n Invalid choice ....."); getch( ); break; } } } void insert(int x) { if(rear == 19) printf("\n Simple queue is full"); else { rear++; a[rear] = x; if(front == -1) front++; } } int del( ) { int dummy; if(front == -1) { printf("\n Simple queue is empty"); getch( ); return(-99); } else { dummy = a[front];

if(front == rear) front= rear= -1; else front++; return(dummy); } } void display( ) { int i; if(front == -1) printf("\n Queue is empty"); else { printf("\n Elements in queue :- \n"); for(i= front; i<= rear; i++) printf("\n %d", a[i]); } getch( ); }

3- Write a program to implement Linear Linked List and show the following operations: creation, display, insertion, deletion and searching.
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct gssll { int info; struct gssll*link; }*head=NULL; void create( ); void ins(); void del( ); void search( ); void display( ); void main( ) { int ch; char ans; while(1) { clrscr( ); printf("\n\n Implementing Simply Singly Linked List"); printf("\n----------------------------------------------- "); printf("\n\n\t 1- Creation of linked list"); printf("\n\t 2- Insertion of element "); printf("\n\t 3- Deletion of element "); printf("\n\t 4- Searching of element in the list"); printf("\n\t 5- Display the elements in the list"); printf("\n\t 6- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: create( ); break; case 2: ins( ); break; case 3: del( ); break; case 4: search( ); break; case 5: display( ); break; case 6: printf("\n You have chosen to exit"); printf("\n Are you sure you want exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans);

if(ans=='y'||ans=='Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } } } void create( ) { char ans; struct gssll *ptr, *ptr1; ptr1=(struct gssll*)malloc(sizeof(struct gssll)); if(!ptr1) printf("\n Memory is full. "); else { ptr1->link = NULL; printf("\n Enter information :- "); scanf("%d",&ptr1->info); if(head == NULL) head = ptr1; else { ptr=head; while(ptr->link != NULL) ptr= ptr->link; ptr->link= ptr1; } printf("\n Do you have more information? "); printf("\n If yes press 'Y' :"); fflush(stdin); scanf("%c",&ans); if(ans== 'Y' || ans== 'y') create( ); } } void ins( ) { struct gssll *ptr, *ptr1; ptr1=(struct gssll*)malloc(sizeof(struct gssll)); if(!ptr1) printf("\n Memory is full."); else ptr1->link = NULL; printf("\n Enter the information : "); scanf("%d",&ptr1->info); ptr = head;

while(ptr->link != NULL) ptr = ptr->link; ptr->link = ptr1; } } void del( ) { struct gssll *ptr, *ptr1; if(head == NULL) printf("\n Linked list is empty."); else { ptr= head; while(ptr->link->link != NULL) ptr= ptr->link; ptr1= ptr->link; ptr->link = NULL; printf("\n Element deleted was : %d",ptr1->info); printf("\n Press any key."); fflush(stdin); getch( ); free(ptr1); } } void display( ) { int count=1; struct gssll *ptr; ptr= head; if(ptr == NULL) printf("\n Linked list is empty."); else { printf("\n Elements in list are : \n"); while(ptr != NULL) { printf("\n\n Node : %d Element : %d",count,ptr->info); count++; ptr= ptr->link; } } printf("\n Press any key."); fflush(stdin); getch( ); } void search( ) { int ser=0,pos=1,flag=0; struct gssll *ptr; printf("\n Enter the element to be searched : "); scanf("%d",&ser); ptr = head;

while(ptr != NULL) { if(ptr->info == ser) { flag=1; break; } pos++; ptr=ptr->link; } if(flag== 1) printf("\n Element found at %d position ",pos); else printf("\n Element not found. \n\n SEARCH UNSUCCESSFUL !!!"); getch( ); }

4- Write a program to increment the data part of every node in a linked list by 10. #include<stdio.h> #include<conio.h> #include<stdlib.h> #define NULL 0 struct gssll { int info; struct gssll*link; }*head=NULL; void create( ); void display( ); void main( ) { int ch; char ans; while(1) { clrscr( ); printf("\n\n Implementing Grounded Simply Singly Linked List"); printf("\n----------------------------------------------- "); printf("\n\n\t 1- Creation of linked list"); printf("\n\t 2- Display the elements in the list"); printf("\n\t 3- Exit from output window"); fflush(stdin); printf("\n Enter your choice :- "); scanf("%d",&ch); switch(ch) { case 1: create( ); break; case 2: display( ); break; case 3: printf("\n You have chosen to exit"); printf("\n Are you sure you want exit?"); printf("\n If yes press y."); fflush(stdin); scanf("%c",&ans); if(ans=='y'||ans=='Y') exit(0); else break; default: printf("\n\n Invalid choice..."); printf("\n Press any key to continue"); fflush(stdin); getch( ); break; } }

} void create( ) { char ans; struct gssll *ptr, *ptr1; ptr1=(struct gssll*)malloc(sizeof(struct gssll)); if(!ptr1) printf("\n Memory is full. "); else { ptr1->link = NULL; printf("\n Enter information :- "); scanf("%d",&ptr1->info); if(head == NULL) head = ptr1; else { ptr=head; while(ptr->link != NULL) ptr= ptr->link; ptr->link= ptr1; } printf("\n Do you have more information? "); printf("\n If yes press 'Y' :"); fflush(stdin); scanf("%c",&ans); if(ans== 'Y' || ans== 'y') create( ); } } void display( ) { int count=1,temp; struct gssll *ptr; ptr= head; if(ptr == NULL) printf("\n Linked list is empty."); else { printf("\n Elements in list are : \n"); while(ptr != NULL) { printf("\n\n Node : %d Element : %d",count,ptr->info+10); count++; ptr= ptr->link; } } printf("\n Press any key."); fflush(stdin); getch( ); }

5- Write a program to implement a circular queue and show the following: insertion and deletion.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void insert(int); int del( ); void display( ); int a[20], rear, front; void main( ) { int ch,data; char ans; front= -1; rear= -1; while(1) {

clrscr( ); printf("\n\t 1- Insert an element into queue "); printf("\n\t 2- Delete an element from the queue "); printf("\n\t 3- Display the queue "); printf("\n\t 4- Exit from output window "); fflush(stdin); printf("\n\n Enter your choice :- "); scanf("%d", &ch); switch(ch) { case 1: printf("\n Enter element inserted into queue"); fflush(stdin); scanf("%d", &data); insert(data); break; case 2: data=del( ); if(data != -99) printf("\n Element deleted is :- %d", data); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 3: display( ); printf("\n Press any key to continue "); fflush(stdin); getch( ); break; case 4: printf("\n You have chosen to exit"); printf("\n You want to exit? If yes press y"); fflush(stdin); scanf("%c", &ans); if(ans == 'y' || ans == 'Y') exit(0); else break; default: printf("\n Invalid choice ....."); getch( ); break; } } } void insert(int x) { if(((rear == 19) && (front == 0)) || (rear == front-1)) printf("\n Circular queue is full"); else if(rear== -1) {

front++; rear++; } else if(rear==19) rear= 0; else rear++; a[rear]=x; } int del( ) { int dummy; if(front== -1) { printf("\n Circular queue is empty"); getch( ); return(-99); } else { dummy=a[front]; if(front==19) front=0; else if(front==rear) front=rear= -1; else front++; return(dummy); } } void display( ) { int i; if(front==-1) printf("\n Circular queue is empty"); else { printf("\n Elements in queue :- \n"); if(front<= rear) { for(i= front; i<= rear; i++) printf("\n %d", a[i]); } else { for(i= front; i<= 19; i++) printf("%d \n", a[i]); for(i= 0; i<= rear; i++) printf("%d \n", a[i]); } } getch( );

6- Write a program to perform the following operations on two given matrices: Addition and Multiplication
.#include<stdio.h>

#include<conio.h> void main() { int i,j,k,a[2][2],b[2][2],c[2][2]; clrscr(); printf("enter the elementof 1st matrix\n\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&a[i][j]); } } printf("following is the 1st matrix\n\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d ",a[i][j]); } printf("\n"); } printf("enter the element of 2nd martix\n\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&b[i][j]); } } printf("following is the 2nd matrix\n\n");

for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d ",b[i][j]); } printf("\n"); } printf(" sum is following==>\n\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=a[i][j]+b[i][j]; printf("\%d ",c[i][j]); } printf("\n"); } printf("multiplication of followin=====>\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { c[i][j]=0; for(k=0;k<2;k++) { c[i][j]+=a[i][k]*b[k][j]; } printf("%d ",c[i][j]); } printf("\n"); } getch(); }

You might also like