0% found this document useful (0 votes)
89 views

Data Structure Programs-Final Rishabh Jain

The document describes programs to implement stack, queue, and linked list data structures in C++. The stack program demonstrates push and pop operations, checking for overflow and underflow. The queue program shows enqueue and dequeue operations, with checks for overflow and underflow. The linked list program inserts nodes at the beginning and allows deleting the first node, creating and linking nodes dynamically. Output examples are provided for each data structure.

Uploaded by

Harshit Tiwari
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Data Structure Programs-Final Rishabh Jain

The document describes programs to implement stack, queue, and linked list data structures in C++. The stack program demonstrates push and pop operations, checking for overflow and underflow. The queue program shows enqueue and dequeue operations, with checks for overflow and underflow. The linked list program inserts nodes at the beginning and allows deleting the first node, creating and linking nodes dynamically. Output examples are provided for each data structure.

Uploaded by

Harshit Tiwari
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 48

PROGRAM -1

Write a Program to Implement STACK. -: INSERTION :-

#include<iostream.h> #include<conio.h> #include<process.h> int push(int [ ],int &,int); void display(int [ ],int); const int size=50; void main( ) { int stack[size],item,top=-1,res; char ch=y; clrscr(); while(ch==y||ch==Y) { cout<< \n Enter ITEM for insertion:; cin>>item; res=push(stack,top,item); if(res == -1) { cout<<OVERFLOW!!!Aborting!! \n; exit(1); }
Rishabh Jain (0114IT071070)

cout<< \n The stack now is: \n; Display(stack,top); cout<< \n Want to insert more elements?(y/n)...; cin>>ch; } getch(); } int push(int stack[ ],int &top,int ele) { if(top == size-1) return -1; else { top++; stack[top]=ele; } return 0; } void display(int stack[],int top) { cout<<stack[top]<< <<endl; for(int i=top-1;i>=0;i--) cout<<stack[ i ]<<endl; }
Rishabh Jain (0114IT071070)

The output produced by above program is shown below. Symbol signifies top. Enter ITEM for insertion: 7 The stack now is: 7 Want to insert more elements? (y/n)... y Enter ITEM for insertion: 9 The stack now is: 9 7 Want to insert more elements? (y/n)... y Enter ITEM for insertion: 9 The stack now is: 5 9 7 Want to insert more elements? (y/n)... n The above program calls function push() for pushing element into stack-array. This function firstly tests for STACK-FULL condition and if it is full, it returns -1, otherwise, it successfully inserts element into stack and returns 0.

Rishabh Jain (0114IT071070)

-: DELETION :#include<iostream.h> #include<conio.h> #include<process.h> int pop(int [ ],int &); int push(int [ ],int &,int); void display(int [ ],int); const int size=50; void main( ) { int stack[size],item, top= -1,res; char ch=y; clrscr(); while(ch==y||ch==Y) { cout<< \n Enter ITEM for insertion:; cin>>item; res=push(stack ,top, item); if(res== -1) { cout<<OVERFLOW!!!Aborting!! \n; exit(1); } cout<< \n The stack now is: \n; display(stack, top); cout<< \n Want to insert more elements?(y/n)...;
Rishabh Jain (0114IT071070)

cin>>ch; } cout<< \n Now deletion of elements begins... \n; ch=y; while(ch==y||ch==Y) { res=pop(stack[], top); if(res== -1) { cout<<UNDERFLOW!!!Aborting!! \n; exit(1); } else { cout<< \n Element deleted is:<<res<<endl; cout<< \n The stack now is: \n; display(stack, top); } cout<< \n Want to delete more elements? (y/n)...; cin>>ch; } getch();} int push(int stack[ ], int &top, int ele) { if(top==size-1) return -1; else
6
Rishabh Jain (0114IT071070)

{ Top++; stack[top]=ele; } return 0; } int pop(int stack[], int &top) { int ret; if(top== -1) return -1; else { ret=stack[top]; top--; } return ret; } void display(int stack[],int top) { if(top== -1) return; cout<<stack[top]<< <<endl; for(int i=top-1;i>=0;i--) cout<<stack[ i ]<<endl; }
Rishabh Jain (0114IT071070)

The output produced by above program is shown below. Symbol signifies top. The stack now is: 9 5 8 7 Want to delete more elements? (y/n)... y Element deleted is: 9 The stack now is: 5 8 7 Want to delete more elements? (y/n)... y Element deleted is: 5 The stack now is: 8 7 Want to insert more elements? (y/n)... n

Rishabh Jain (0114IT071070)

PROGRAM- 2
8

Write a Program to Implement QUEUE? -: INSERTION :#include<iostream.h> #include<conio.h> #include<process.h> int insert_in_Q(int [ ],int); void display(int [ ], int, int); const int size=50; int queue[size], front= -1, rear= -1; void main() { int item, res; char ch=y; clrscr(); while(ch==y||ch==Y) { cout<< \n Enter ITEM for insertion:; cin>>item; res=insert_in_Q(queue, item); if(res == -1) { cout<<OVERFLOW!!!Aborting!! \n; exit(1); }
Rishabh Jain (0114IT071070)

cout<< \n Now the Queue(Front.........to.........Rear) is: \n;


9

display(queue, front, rear); cout<<\n Want to insert more elements? (y/n)...; cin>>ch; } getch(); } int insert_in_Q(int queue[ ], int ele) { if(rear==size-1) return -1; else if(rear== -1) { front=rear=0; queue[rear]=ele; } else { rear++; queue[rear]=ele; } return 0; } void display(int queue[ ],int top) {
Rishabh Jain (0114IT071070)

if(front == -1)
10

return; for(int i=top-1;i>=0;i--) cout<<queue[i]<< \t; cout<<queue[rear]<<endl; }

Rishabh Jain (0114IT071070)

11

The output produced by above program is shown below.

Enter ITEM for insertion: 3 Now the Queue (Front.......to.......Rear) is: 3 Want to insert more elements? (y/n)... y Enter ITEM for insertion: 6 Now the Queue (Front.......to.......Rear) is: 3 6

Want to insert more elements? (y/n)... y Enter ITEM for insertion: 8 Now the Queue (Front.......to.......Rear) is: 3 6 8

Want to insert more elements? (y/n)... n

12

Rishabh Jain (0114IT071070)

-: DELETION :#include<iostream.h> #include<conio.h> #include<process.h> int remove(int [ ], int); int insert(int [ ],int); void display(int [ ], int, int); const int size=50; int queue[size], front= -1, rear= -1; void main( ) { int item, res; char ch=y; clrscr( ); while(ch==y||ch==Y) { cout<< \n Enter ITEM for insertion:; cin>>item; res=insert(queue, item); if(res== -1) { cout<<OVERFLOW!!!Aborting!! \n;
13

exit(1); } cout<< \n Now the Queue(Front.........to.........Rear) is: \n; display(queue, front, rear);
Rishabh Jain (0114IT071070)

cout<<\n Want to insert more elements? (y/n)...; cin>>ch; } cout<<\n Now deletion of elements begins... \n; ch=y; while(ch==y||ch==Y) { res=remove(queue); if(res == -1) { cout<<UNDERFLOW!!!Aborting!! \n; exit(1); } else { cout<<\n Element deleted is:<<res<<endl; cout<< \n Now the Queue(Front.........to.........Rear) is: \n; display(queue, front, rear); } cout<<\n Want to insert more elements? (y/n)...; cin>>ch; }
14

} getch(); } int insert_in_Q(int queue[ ], int ele) { if(rear==size-1) return -1; else if(rear== -1) { front=rear=0; queue[rear]=ele; } else { rear++; queue[rear]=ele; } return 0; } int remove(int queue[ ]) { int ret; if(front== -1) return -1; else {
15
Rishabh Jain (0114IT071070)

ret=queue[front]; if(front==rear) front=rear=-1; else front++; } return ret; } void display(int stack[],int top) { if(front == -1) return; for(int i=top-1;i>=0;i--) cout<<queue[i]<< \t; cout<<queue[rear]<<endl; }
Rishabh Jain (0114IT071070)

16

Rishabh Jain (0114IT071070)

The Output Produced By Above Program Is Shown Below.

Now the Queue (Front.......to.......Rear) is: 7 9 4 2

Want to delete more elements? (y/n)... y Element deleted is: 7 Now the Queue (Front.......to.......Rear) is: 9 4 2

Want to delete more elements? (y/n)... y Element deleted is: 9 Now the Queue (Front.......to.......Rear) is: 4 2

Want to delete more elements? (y/n)... n

17

Rishabh Jain (0114IT071070)

PROGRAM- 3

Write a Program to Implement LINKED LIST.

-: INSERTION :#include<iostream.h> #include<conio.h> #include<process.h> struct node { int info; Node *next; } *start, *newptr, *save, *ptr; Node *create_new_node(int); void insert_beg(node *); void display(node *); void main() { start=NULL; int inf; char ch=y; while(ch==y||ch==Y) {
18

clrscr(); cout<<\n Enter INFOrmation for the new node...; cin>>inf;


Rishabh Jain (0114IT071070)

cout<<\n Creating New Node!! Press Enter to continue...; newptr=create_new_node(inf); if(newptr!=NULL) { cout<<\n\n New Node Created Successfully. Press ENTER to continue...; getch(); } else { cout<<\n Cannot create new node!!! Aborting!! \n; getch(); exit(1); } cout<<\n\n Now inserting this node in the beginning of list... \n; cout<<Press ENTER to continue... \n; getch(); insert_beg(newptr); cout<<\n Now the list is:; display(start); cout<<\n Press Y to enter more nodes, N to exit... \n; cin>>ch; }
19

getch(); } node *create_new_node(int n)


Rishabh Jain (0114IT071070)

{ ptr=new node; ptr-> info=n; ptr->next=NULL; return ptr; } void insert_beg(node *np) { if(start==NULL) start=np; else { save=start; start=np; np->next=save; } } void display(node *np) { while(np!=NULL) { cout<<np->info<<->;
20

np=np->next; } cout<<!!!\n; }
Rishabh Jain (0114IT071070)

The Output Produced By Above Program Is.

Enter Information for the new node.... 7 Creating New Node!! Press Enter to continue.... New Node Created Successfully. Press Enter to continue... Now inserting this node in the beginning of list... Press Enter to continue... Now the list is: 7 ->!!! Press Y to enter more nodes, N to exit... Enter INFOrmation for the new node.... 9 Creating New Node!! Press Enter to continue... New Node Created Successfully. Press Enter to continue.... Now inserting this node in the beginning of list... Press Enter to continue... Now the list is: 9 -> 7 -> !! Press Y to enter more nodes, N to exit...n

21

Rishabh Jain (0114IT071070)

-: DELETION :#include<iostream.h> #include<conio.h> #include<process.h> struct node { int info; node *next; } *start, *newptr, *save, *ptr, *rear; node *create_new_node(int); void insert(node *); void display(node *); void delnode(); void main() { start=rear=NULL; int inf; char ch=y; while(ch==y||ch==Y) { cout<<\n Enter INFOrmation for the new node...; cin>>inf; cout<<\n Creating New Node!! Press Enter to continue...;
22

newptr=create_new_node(inf); if(newptr==NULL) {
Rishabh Jain (0114IT071070)

cout<<\n Cannot create new node!!! Aborting!! \n; getch(); Exit(1); } insert(newptr); cout<<\n Press Y to enter more nodes, N to exit... \n; cin>>ch; do { Cout<<\n Now the list is:; Display(start); Getch(); Cout<<Want to delete first node? (y/n)...; Cin>>ch; If(ch==y||ch==Y) Delnode(); }while(ch==y||ch==Y); }

node *create_new_node(int n) { ptr=new node; ptr-> info=n;


23

ptr->next=NULL; return ptr; }


Rishabh Jain (0114IT071070)

void insert(node *np) { if(start==NULL) start=np; rear=np; else { rear->next=np; rear=np; } } void delnode() { if(start==NULL) cout<<UNDERFLOW!!! \n; else { ptr=start; start=start->next; delete ptr; } }
24

void display(node *np) { while(np!=NULL)


Rishabh Jain (0114IT071070)

{ cout<<np->info<<->; np=np->next; } cout<<!!!\n; }

25

Rishabh Jain (0114IT071070)

The output produced by above program is.

The list now is: 6 -> 7 -> 1 -> 4 -> 9 -> 3 -> 5 -> 2 -> !!! Want to delete first node? (y/n)...y The list now is: 7 -> 1 -> 4 -> 9 -> 3 -> 5 -> 2 -> !!! Want to delete first node? (y/n)...y The list now is: 1 -> 4 -> 9 -> 3 -> 5 -> 2 -> !!! Want to delete first node? (y/n)...n

26

Rishabh Jain (0114IT071070)

PROGRAM- 4

Write a Program to Implement BUBBLE SORT.

#include<iostream.h> #include<conio.h> void bubblesort(int [ ],int); void main() { int AR[50],N; clrscr(); cout<<\n How many elements do U want to create array ? (max. 50...); cin>>N; cout<<\n Enter array elements....\n; for(int i=0;i<N;i++) { cin>>AR[i]; Bubblesort(AR,N); cout<<\n\n The sorted array is as shown below....\n; for(i=0;i<N;i++) cout<<AR[i]<< ; cout<<endl; } getch();
27

}
Rishabh Jain (0114IT071070)

void bubblesort(int AR[ ], int size) { int tmp, ctr=0; for(int i=0;i<size;i++) { for(int j=0;j<(size-1)-i;j++) { if(AR[ j ]>AR[j+1]) { tmp=AR[j]; AR[j]=AR[j+1]; AR[j+1]=tmp; } cout<<Array after iteration - <<++ctr<< is: \n; for(int k=0;k<size;k++) cout<<AR[k]<< ; cout<<endl; } } }

28

Rishabh Jain (0114IT071070)

The Output Produced By Above Program Is.

How many elements do U want to create array with? (max. 50)....4 Enter Array elements.... 7382 Array after iteration -1 is: 3782 Array after iteration -2 is: 3782 Array after iteration -3 is: 3728 Array after iteration -4 is: 3728 Array after iteration -5 is: 3278 Array after iteration -6 is: 2378 The Sorted array is as shown below... 2378

Rishabh Jain (0114IT071070)

29

PROGRAM- 5

Write a Program to Implement INSERTION SORT. #include<iostream.h> #include<conio.h> #include<limits.h> void INSsort(int [ ],int); void main() { int AR[50],N; clrscr(); cout<<\n How many elements do U want to create array ? (max. 50...); cin>>N; cout<<\n Enter array elements....\n; for(int i=0;i<N;i++) { cin>>AR[i]; INSsort(AR,N); cout<<\n\n The sorted array is as shown below....\n; for(i=0;i<N;i++) cout<<AR[i]<< ; cout<<endl; } getch();
Rishabh Jain (0114IT071070)

30

void INSsort(int AR[], int size) { int tmp,j; AR[0]=int_min; for(int i=1;i<=size;i++) { tmp=AR[i]; j=i-1; while(tmp<AR[j]) { AR[j+1]=AR[j]; j--; } AR[j+1]=tmp; cout<<Array after pass - <<i<< is: \n; for(int k=0;k<=size;k++) cout<<AR[k]<< ; cout<<endl; } }

Rishabh Jain (0114IT071070)

31

The Output Produced by Above Program Is.

How many elements do U want to create array with? (max. 50).....7 Enter array elements.... 9613427 Array after pass - 1 is: -32768 9 6 1 3 4 2 7 Array after pass - 2 is: -32768 6 9 1 3 4 2 7 Array after pass - 3 is: -32768 1 6 9 3 4 2 7 Array after pass - 4 is: -32768 1369427

Array after pass - 5 is: -32768 1346927

Array after pass - 6 is: -32768 1234697

Array after pass - 7 is: -32768 1234679

The Sorted array is as shown below.... -32768 1234679

Rishabh Jain (0114IT071070)

PROGRAM- 6

32

Write a Progrm to Implement SELECTION SORT. #include<iostream.h> #include<conio.h> void selsort(int [], int); void main() { int AR[50], N; clrscr(); cout<<\n How many elements do U want to create array ? (max. 50...); cin>>N; cout<<\n Enter array elements....\n; for(int i=0;i<N;i++) { cin>>AR[i]; Selsort(AR,N); cout<<\n\n The sorted array is as shown below....\n; for(i=0;i<N;i++) cout<<AR[i]<< ; cout<<endl; } getch(); } void selsort(int AR[], int size)
Rishabh Jain (0114IT071070)

{ int small, pos, tmp;


33

for(int i=0;j<size;i++) { small=AR[i]; pos=i; for(int j=i+1;j<size;j++) { if(AR[j]<small) { small=AR[j]; pos=j; } } tmp=AR[i]; AR[i]=AR[pos]; AR[pos]=tmp; cout<<\n Array after pass - <<i+1<< is: \n; for(j=0;j<size;j++) cout<<AR[j]<< ; } }

Rishabh Jain (0114IT071070)

The output produced by above program is.

How many elements do U want to create array with? (max. 50).....7


34

Enter array elements.... 6825134 Array after pass - 1 is: 1825634 Array after pass - 2 is: 1285634 Array after pass - 3 is: 1235684 Array after pass - 4 is: 1234685 Array after pass - 5 is: 1234586 Array after pass - 6 is: 1234568 The Sorted array is as shown below..... 1 2 3 4 5 6 8

Rishabh Jain (0114IT071070)

PROGRAM- 7

Write a Program to Implement MERGE SORT. #include<iostream.h>


35

#include<conio.h> void merge(int [], int, int [], int, int []); void main() { int A[50], B[50], C[50], MN=0,M, N; clrscr(); cout<<\n How many elements do U want to create array ? (max. 50...); cin>>M; cout<<\n Enter first Arrays element [ascending]...\n; for(int i=0;i<M;i++) { cin>>A[i]; } cout<<\n How many elements do U want to create array ? (max. 50...); cin>>N; MN=M+N; cout<<\n Enter first Arrays element [ascending]...\n; for(int i=0;i<N;i++) { cin>>B[i]; } Merge(A,M,B,N,C); cout<<\n\n The Sorted array is as shown below....\n; for(i=0;i<MN;i++) cout<<C[ i ]<< ;
36
Rishabh Jain (0114IT071070)

cout<<endl; getch(); } void merge(int A[], int M, int B[], int N, int C[]) { int a, b, c; for(a=0,b=N-1,c=0;a<M && b>=0;) { if(A[a]<=B[b]) C[c++]=A[a++]; else C[c++]=B[b--]; } if(a<M) { while(a<M) C[c++]=A[a++]; } Else { while(b>=0) C[c++]=B[b--];

Rishabh Jain (0114IT071070)

The Output Produced by Above Program Is

37

How many elements do U want to create array ? (max. 50)....5 Enter first Arrays element [ascending]... 2 5 8 9 12 How many elements do U want to create array ? (max. 50)....5 Enter first Arrays element [descending]... 16 12 10 8 7 3 1 The Sorted array is as shown below... 1 2 3 5 7 8 8 9 10 12 12 16

Rishabh Jain (0114IT071070)

38

PROGRAM- 8

Write a program to implement QUICK SORT.

#include<iostream.h> #include<conio.h> int a[100],n,i,l,h;

void main() { clrscr(); void input(void); input(); getch(); } void input(void) {

void quicksort(int a[],int l,int h); void output(int a[],int n); cout<<"How elements in the Array :"; cin>>n; cout<<"Enter the Elements : \n"; for(i=0;i<=n-1;i++) { cin>>a[i]; }
Rishabh Jain (0114IT071070)

39

i=0;h=n-1; quicksort(a,l,h); cout<<"Sorted Array : \n"; output(a,n); } void quicksort(int a[],int l,int h) { int temp,key,low,high; low=l; high=h; key=a[(low+high)/2]; do { while(key>a[low]) { low++; } while(key<a[high]) { high--; } if(low<=high) { temp=a[low]; a[low++]=a[high]; a[high--]=temp;
Rishabh Jain (0114IT071070)

40

} } while(low<=high); if(l<high) quicksort(a,l,high); if(low<h) quicksort(a,low,h); } void output(int a[],int n) { for(i=0;i<=n-1;i++) { cout<<a[i]<<" "; } }

Rishabh Jain (0114IT071070)

41

Output generated by above program is :

Enter no. of many elements in the array: 5 Enter elements: 4 3 6 2 5 Sorted array : 23456

Rishabh Jain (0114IT071070)

42

PROGRAM- 9

Write a Program to Implement LINEAR SEARCH.

#include<iostream.h> #include<conio.h> int Lsearch(int [],int,int); void main() { int AR[50],ITEM,N,index; clrscr(); cout<<\nEnter desired array size (max. 50); cin>>N; cout<<\nEnter Array elements\n; for(int i=0;i<N;i++) { cin>>AR[i]; } cout<<\nEnter Elements to be searched for; cin>>ITEM; Index=Lsearch(AR,N,ITEM); If((Index==-1) cout<<\nSorry!! Given element could not b found.\n; else cout<<\nElement found at index :<< index<<,Position :<<index+1<<endl;
Rishabh Jain (0114IT071070)

43

Int Lsearch(int AR[ ],int size,int item) { For(int i=0;i<size;i++) { If(AR[ i ]==o) return I; } return -1; }

Rishabh Jain (0114IT071070)

44

The output produced by the program is.

Enter desired array size(max. 50)7 Enter Array elements 1 5 2 8 6 10 7 Enter Elements to be searched for6 Elements found at index : 4,Position :5

Rishabh Jain (0114IT071070)

PROGRAM- 10

45

Write a Program to Implement BINARY SEARCH. #include<iostream.h> #include<conio.h> Int Bsearch(int [ ],int,int); void main() { int AR[50],ITEM,N,index; clrscr(); cout<<Enter desired array size (max.50); cin>>N; cout<<\nEnter Array elements(must be stored in Asc order)\n; for(int i=0;I,n;i++) { cin>>AR[i]; } cout<<\nEnter Elements to be searched for ; cin>>ITEM; index=Bsearch(AR,N,ITEM); if ()index==-1) cout<<\nSorry!! Given Element could not be found.\n; else cout<<\nElements found at index:<<index<<, Position; <<index+1 <<endl; }
Rishabh Jain (0114IT071070)

int Bsearch(int AR[ ],int size,int item)


46

{ int beg,last,mid; beg=o; last=size-1; while(beg<=last) { mid=(beg+last)/2; if(item==AR[mid]) return mid; else if (item>AR[mid]) beg=mid+1; else last=mid-1; } return -1; }

Rishabh Jain (0114IT071070)

The Output Produced by The Above Program is.

47

Enter desired array size (max.50)7 Enter Array elements (must be sorted in Asc order) 2 5 7 8 9 10 15 Enter Element to be searched for8 Element found at index :3, Position : 4

Rishabh Jain (0114IT071070)

48

You might also like