0% found this document useful (0 votes)
34 views26 pages

Ds Lab 1-7

The document contains multiple C++ code implementations for various data structures and algorithms including List, Linked List, Stack, Queue, Priority Queue, Bubble Sort, and Insertion Sort. Each section provides a main function that allows users to interact with the respective data structure or sorting algorithm through a menu-driven interface. The code covers basic operations such as insertion, deletion, display, and sorting of elements.

Uploaded by

gnana.svga
Copyright
© © All Rights Reserved
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)
34 views26 pages

Ds Lab 1-7

The document contains multiple C++ code implementations for various data structures and algorithms including List, Linked List, Stack, Queue, Priority Queue, Bubble Sort, and Insertion Sort. Each section provides a main function that allows users to interact with the respective data structure or sorting algorithm through a menu-driven interface. The code covers basic operations such as insertion, deletion, display, and sorting of elements.

Uploaded by

gnana.svga
Copyright
© © All Rights Reserved
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/ 26

1) List

#include<iostream>

using namespace std;

void create();

void insert();

void deletion();

void search();

void display();

int a,b[20],n,d,e,f,i;

int main() {

int c;

cout<<"\n Main Menu";

cout<<"\n 1.Create \n 2.Delete \n 3.Search \n 4.insert \n 5.Display \n


6.Exit";

do {

cout<<"\n enter your choice:";

cin>>c;

switch(c) {

case 1: create(); break;


case 2: deletion(); break;

case 3: search(); break;

case 4: insert(); break;

case 5: display(); break;

case 6: exit(0 ); break;

default:

cout<<"The given number is not between 1-5\n"; } }

while(c<=6);

return 0; }

void create() {

cout<<"\n Enter the number of elements you want to create: ";

cin>>n;

cout<<"\nenter the elements\n";

for(i=0;i<n;i++) {

cin>>b[i]; } }

void deletion() {

cout<<"Enter the number u want to delete \n";

cin>>d;

for(i=0;i<n;i++) {

if(b[i]==d) {
b[i]=0;

cout<<d<<" deleted"; } }

void search()

cout<<"Enter the number \n";

cin>>e;

for(i=0;i<n;i++)

if(b[i]==e)

cout<<"Value found the position\n"<<i+1;

void insert()

cout<<"\nenter how many number u want to insert: ";

cin>>f;

cout<<"\nEnter the elements\n";


for(i=0;i<f;i++)

cin>>b[n++];

void display()

for(i=0;i<n;i++)

cout<<"\n"<<b[i];

}
2)Linked List

#include<iostream>

using namespace std;

struct Node{

int data;

int next;

};

Node Array[100];

int head=-1;

int avail=0;

void initialise_list()

for(int i=0;i<=98;i++)

Array[i].next=i+1;

Array[99].next=-1; //indicating the end of the linked list.

}
int getnode()

int NodeReturn=avail;

avail=Array[avail].next;

return NodeReturn;

void freeNode(int nodeDeleted){

Array[nodeDeleted].next=avail;

avail=nodeDeleted;

void insertBeginning(int data){

int newNode=getnode();

Array[newNode].data=data;

Array[newNode].next=head;

head=newNode;

void insertEnd(int data)

int newNode=getnode();
int temp=head;

while(Array[temp].next!=-1)

temp=Array[temp].next;

Array[newNode].data=data;

Array[newNode].next=-1;

Array[temp].next=newNode;

void display() {

int temp=head;

while(temp!=-1) {

cout<<Array[temp].data<<"->";

temp=Array[temp].next; }

cout<<"-1"<<endl; }

int main() {

initialise_list();

int x,y,z;

for(;;) {

cout<<"1. Insert At The Beginning"<<endl;


cout<<"2. Insert At The End"<<endl;

cout<<"3. Display"<<endl;

cout<<"4.Exit"<<endl;

cout<<"Enter Your choice"<<endl;

cin>>z;

switch(z) {

case 1:

cout<<"Enter the number you want to enter"<<endl;

cin>>x;

insertBeginning(x);

break;

case 2:

cout<<"Enter the number you want to enter"<<endl;

cin>>y;

insertEnd(y);

break;

case 3:

cout<<"The linked list contains the following element in


order"<<endl;

display();
break;

case 4:

exit(0);

default:

cout<<"The entered choice is not correct"<<endl;

}
3)Stack

#include <iostream>

using namespace std;

int stack[100], n=100, top=-1;

void push(int val) {

if(top>=n-1)

cout<<"Stack Overflow"<<endl;

else {

top++;

stack[top]=val;

void pop() {

if(top<=-1)

cout<<"Stack Underflow"<<endl;

else {

cout<<"The popped element is "<< stack[top] <<endl;

top--;
}

void display() {

if(top>=0) {

cout<<"Stack elements are:";

for(int i=top; i>=0; i--)

cout<<stack[i]<<"";

cout<<endl;

} else

cout<<"Stack is empty";

int main() {

int ch, val;

cout<<"1) Push in stack"<<endl;

cout<<"2) Pop from stack"<<endl;

cout<<"3) Display stack"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter choice: "<<endl;

cin>>ch;
switch(ch) {

case 1: {

cout<<"Enter value to be pushed:"<<endl;

cin>>val;

push(val);

break;

case 2: {

pop();

break;

case 3: {

display();

break;

case 4: {

cout<<"Exit"<<endl;

break;

default: {
cout<<"Invalid Choice"<<endl;

}while(ch!=4);

return 0;

}
4)Queue

#include <iostream>

using namespace std;

int queue[100], n = 100, front = - 1, rear = - 1;

void Insert() {

int val;

if (rear == n - 1)

cout<<"Queue Overflow"<<endl;

else {

if (front == - 1)

front = 0;

cout<<"Insert the element in queue : "<<endl;

cin>>val;

rear++;

queue[rear] = val;

void Delete() {
if (front == - 1 || front > rear) {

cout<<"Queue Underflow ";

return ;

} else {

cout<<"Element deleted from queue is : "<< queue[front] <<endl;

front++;;

void Display() {

if (front == - 1)

cout<<"Queue is empty"<<endl;

else {

cout<<"Queue elements are : ";

for (int i = front; i <= rear; i++)

cout<<queue[i]<<"";

cout<<endl;

int main() {

int ch;
cout<<"1) Insert element to queue"<<endl;

cout<<"2) Delete element from queue"<<endl;

cout<<"3) Display all the elements of queue"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter your choice : "<<endl;

cin>>ch;

switch (ch) {

case 1: Insert();

break;

case 2: Delete();

break;

case 3: Display();

break;

case 4: cout<<"Exit"<<endl;

break;

default: cout<<"Invalid choice"<<endl;

} while(ch!=4);

return 0;}
5)Priority Queue

#include<iostream>

using namespace std;

#define N 20

int Q[N], Pr[N];

int r = -1, f = -1;

void enqueue (int data, int p)

int i;

if ((f == 0) && (r == N - 1))

cout <<"Queue is full";

else

if (f == -1)

f = r = 0;

Q[r] = data;

Pr[r] = p;

}
else if (r == N - 1)

for (i = f; i <= r; i++)

Q[i - f] = Q[i];

Pr[i - f] = Pr[i];

r = r - f;

f = 0;

for (i = r; i > f; i--)

if (p > Pr[i])

Q[i + 1] = Q[i];

Pr[i + 1] = Pr[i];

else

break;

Q[i + 1] = data;

Pr[i + 1] = p;

r++;
}

else

for (i = r; i >= f; i--)

if (p > Pr[i])

Q[i + 1] = Q[i];

Pr[i + 1] = Pr[i];

else

break;

Q[i + 1] = data;

Pr[i + 1] = p;

r++;

}
}

void display () {

int i;

for (i = f; i <= r; i++)

cout <<"Element ="<< Q[i] <<" Priority = "<< Pr[i] << endl;

void dequeue () {

if (f == -1) {

cout <<"Queue is Empty"; }

else {

cout <<"deleted Element ="<< Q[f] << endl;

cout <<"Its Priority = "<< Pr[f] << endl;

if (f == r)

f = r = -1;

else

f++; }}

int main () {

int opt, n, i, data, p;


cout <<"Enter Your Choice:-"<< endl;

do {

cout <<

"1 for Insert the Data in Queue\n2 for show the Data in Queue \
n3 for Delete the data from the Queue\n0 for Exit"

<< endl;

cin >> opt;

switch (opt) {

case 1:

cout <<"Enter the number of data"<< endl;

cin >> n;

cout <<"Enter your data and Priority of data"<< endl;

i = 0;

while (i < n) {

cin >> data;

cin >> p;

enqueue (data, p);

i++; }

break;

case 2:
display ();

break;

case 3:

dequeue ();

break;

case 0:

break;

default:

cout <<"Incorrect Choice"<< endl;

} }

while (opt != 0);

return 0; }
6)Bubble Sort

#include <bits/stdc++.h>

using namespace std;

void bubbleSort(vector<int>&v, int n) {

int i, j;

for (i = 0; i < n - 1; i++)

for (j = 0; j < n - i - 1; j++)

if (v[j] > v[j + 1])

swap(v[j], v[j + 1]); }

void printArray(vector<int>&v, int n) {

int i;

for (i = 0; i < n; i++)

cout << v[i] <<"";

cout << endl; }

int main() {

int n;

cin>>n;

vector<int> v(n);

for(int i=0;i<n;i++){
cin>>v[i];

bubbleSort(v, n);

cout <<"Sorted array: \n";

printArray(v, n);

return 0;}
7)Insertion sort

#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
int key, j;
for (int i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
cout << arr[i] <<"";
}
cout << endl;
}

int main() {
int n;
cout <<"Enter the number of elements: ";
cin >> n;
int* arr = new int[n];
cout <<"Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout <<"Original array: ";
printArray(arr, n);
insertionSort(arr, n);

cout <<"Sorted array: ";


printArray(arr, n);
delete[] arr;
return 0;}

You might also like