Assignment 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 90

LAB ASSIGNMENT REPORT

On

BCA 355
Data Structure Using C ++ Lab

COLLEGE OF COMPUTING SCIENCES AND


INFORMATION
TECHNOLOGY
TMU, MORADABAD

2020-2021(Odd Semester)

Submitted To: Submitted By:

MR. AKSHAY KUMAR SIDDHU SIR NAME: - Omendra Singh

COURSE: - BCA “C”

ENROLLMENT NO.: - TCA1901149

YEAR/SEM: - 2ND YEAR/3RD SEM.

1
ASSIGNMENT I [ARRAY AND SEARCHING]

S.NO PROGRAM NAME PAGE DATE SIGN REMARK


NO
1. WAP for traversing the elements of array

2. WAP to insert an element into an array.

3. WAP to find out the largest element of an array of size


n.

4. WAP to delete a specific element from an


array.
5 WAP to find out location of
element in an array using
linear search.
6. WAP to find out location of an
element in an array using binary
search.

7. WAP to calculate the reverse of an array.

8 WAP to find out the occurrence of maximum


element in an array.
9. WAP a program to obtain transpose of a matrix.

10. WAP a program to check a given string is palindrome or


not .

2
II (STACKS and QUEUES)
S PROGRAM NAME PAGE DATE SIGN REMARKS
N
1. WAP to perform push operation on
the stack. Check the overflow
condition using array
2 WAP to perform pop operation from
the stack . Check the underflow
condition using array.
3. WAP to implement stack by Array.
4. WAP to perform decimal to binary
conversion using a stack.
5. WAP to reverse a string using stack.
6. WAP to evaluate postfix expression
using a stack.
7. WAP to implement queue using array
and perform operations: a) traverse
b) insert c) delete
8. WAP to implement Queue using
linked list and perform operations:a)
Create b) traverse c) insert d) delete
9. WAP to insert an element in circular
queue.
10 WAP to delete an element from
circular queue.

3
ASSIGNMENT III (LINKED LIST)
SN PROGRAM NAME DATE PAGE SIG REMARKS
N
1 WAP to create and traverse a linear
linked list
2 WAP for making a linear linked list
with following operation: (1) Insertion
(at beginning, at End, at particular
position)
3 WAP for linked list with following
operation: 1) Deletion (from
beginning, from end and at particular
position)
4 WAP to search an element from linear
linked list.
5. WAP to sort the elements of a linear
linked list.
6. WAP to reverse the elements of a
linear linked list
7. WAP to create and traverse a Doubly
Linked List
8 WAP to insert an element ( at
beginning, at end and at particular
position) in Doubly Linked List
9 WAP to delete an element ( from
beginning, from end and from
particular position) in Doubly Linked
List.
10 WAP to perform following operations
:a) create b) traverse c) insert an
element d) delete an element , on
Circular linked List.

4
LAB ASSIGNMENT 1

1. WAP for traversing the elements of array.

Program
#include<iostream>

using namespace std;

int main()

int arr[5]={1,2,3,4,5};

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

cout<<arr[i]<<" ";

return 0;

Output

5
1. WAP to insert an element into an array.

PROGRAM
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the size of the array :- ";
cin>>n;
int arr[n];
cout<<"Enter the values of array :- ";
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
return 0;
}

6
OUTPUT

7
2. WAP to find out the largest element of an array of size n.
PROGRAM
#include<iostream>
using namespace std;

int main()
{
int n,max=0;
cout<<"Enter the size of the array :- ";
cin>>n;
int arr[n];
cout<<"Enter the values of array :- ";
for(int i=0;i<n;i++)
cin>>arr[i];
for(int i=0;i<n;i++){
if(max<arr[i])
max=arr[i];
}

8
cout<<"The greatest element of the array is "<<max;
return 0;
}

OUTPUT

9
3. WAP to delete a specific element from an array.
PROGRAM
#include<iostream>
using namespace std;
int main()
{
int n,del,con=1;
cout<<"Enter the size of the array\n";
cin>>n;
cout<<"Enter the element of an array\n";
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"Enter the element which you want to delete\n";
cin>>del;
for(int i=0;i<n;i++)
{
if(arr[i]==del)

10
{
for(int j=i;j<n;j++)
{
arr[j]=arr[j+1];
con=0;
}
}
}
if(con==0){
cout<<"The values are\n";
for(int i=0;i<n-1;i++)
cout<<arr[i]<<" ";
}
else
cout<<"The value is not present in the array";
return 0;
}

11
OUTPUT

12
4. WAP to find out location of element in an array using linear search.
PROGRAM
#include<iostream>
using namespace std;

int main()
{
int n,sech,loc;
cout<<"Enter the size of the array:- ";
cin>>n;
cout<<"Enter the element of an array:- ";
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"Enter the element which you want to search:- ";
cin>>sech;
for(int i=0;i<n;i++){

13
if(arr[i]==sech)
loc=i;}
cout<<"The location of the element is arr["<<loc<<"]";
return 0;

OUTPUT

14
5. WAP to find out location of an element in an array using binary search.
PROGRAM
#include<iostream>
using namespace std;
int main()
{
int c,lb,ub,avg,n,search;
cout<<"Enter number of elements\n";
cin>>n;
int array[n];
cout<<"Enter "<<n<<" integers\n";
for (c = 0; c < n; c++)
cin>>array[c];
cout<<"Enter value to find\n";
cin>>search;
lb = 0;
ub = n - 1;

15
avg = (lb+ub)/2;

while (lb <= ub) {


if (array[avg] < search)
lb = avg + 1;
else if (array[avg] == search) {
cout<<search<<" found at location "<<avg+1<<"\n";
break;
}
else
ub = avg - 1;

avg = (lb + ub)/2;


}
if (lb > ub)
cout<<"Not found! "<<search<<" isn't present in the list.\n";
return 0;
}
OUTPUT

16
6. WAP to calculate the reverse of an array.
PROGRAM
#include<iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter the no. of element";
cin>>n;
int arr[n];
cout<<"Enter "<<n<<" Integer\n";
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<"Before reverse\n";
for(int i=0;i<n;i++)
cout<<arr[i];
cout<<"\n\nAfter reverse\n";
for(int i=n-1;i>=0;i--)

17
cout<<arr[i];
return 0;
}

OUTPUT

18
7. WAP to merge two sorted arrays into a single sorted array.
PROGRAM
#include<iostream>
using namespace std;
int main()
{
int i=0,j=0,x,y,z;
cout<<"Enter the first array size:-";
cin>>x;
cout<<"Enter the Second array size:-";
cin>>y;
z=x+y;
int a[x],b[y],c[z];
for(int l=0;l<x;l++)
{
cout << "Enter the "<<l<<" no. out of "<<x-1<<" ";
cin>>a[l];
}
for(int l=0;l<y;l++)

19
{
cout << "Enter the "<<l<<" no. out of "<<y-1<<" ";
cin>>b[l];
}
for(int k=0;k<z;k++){
if(i <= x && j <= y)
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
}
else
{
if(j<y){
c[k]=b[j];
j++;
}

20
else{
c[k]=a[i];
i++;
}
}
}
for(int k=0;k<z;k++){
cout << "\n" << c[k];
}
return 0;
}
OUTPUT

21
8. WAP to find out the occurrence of maximum element in an array.

PROGRAM
#include <iostream>
using namespace std;

int largest(int arr[], int n)


{
int i;
int max = arr[0];  
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];

return max;
}
int main()
{
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Largest in given array is " << largest(arr, n);
return 0;
}

22
9. WAP a program to obtain transpose of a matrix.
#include <iostream>
using namespace std;
int main() {
int a[10][10], transpose[10][10], row, column, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> row >> column;
cout << "\nEnter elements of matrix: " << endl;
// Storing matrix elements
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}

// Printing the a matrix


cout << "\nEntered Matrix: " << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << " " << a[i][j];

23
if (j == column - 1)
cout << endl << endl;
}}
10.WAP a program to check a given string is palindrome or not .
#include <iostream>
using namespace std;
int main()
{
char s[1000];
int i,n,c=0;

cout<<"Enter the string : ";


cin>>s;
n=strlen(s);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
cout<<"string is palindrome";
else
cout<<"string is not palindrome";
return 0;
}

24
11.WAP to calculate the length of string without using library functions.
#include <iostream>
using namespace std;
int main()
{
char str[100],i;
cout<<"Enter a string: \n";
cin>>str;
for(i=0; str[i]!='\0'; ++i);
cout<<"\nLength of input string: "<<i;
return 0;
}

25
12.WAP to print the sum of diagonal elements of N*N square matrix.
#include<iostream>
using namespace std;
int main()
{
int a[5][5],d1sum=0,d2sum=0,m,i,j;
cout<<"Enter size of the square matrix(max 5):";
cin>>m;
cout<<"\nEnter the Matrix row wise:\n";
for(i=0;i<m;i++)
for(j=0;j<m;++j)
cin>>a[i][j];
for(i=0;i<m;++i)
for(j=0;j<m;++j){
if(i==j)
d1sum+=a[i][j];
if(i+j==(m-1))
d2sum+=a[i][j];}
cout<<"\nSum of 1st diagonal is "<<d1sum;
cout<<"\nSum of 2nd diagonal is "<<d2sum;
return 0;
}

26
LAB ASSIGNMENT 2

1. WAP to perform push operation on the stack. Check the overflow


condition using array.
#include<iostream>

using namespace std;

int top=-1,stack[5];

void insert()

int max=4;

if(top==max)

cout<<"Stack is overflow"<<endl;

else

{ top++;

cout<<"Enter element : ";

cin>>stack[top];

void display()

27
if(top==-1)

cout<<"Stack is empty"<<endl;

else

cout<<"Stack elements are : "<<endl;

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

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

cout<<endl;

int main()

int ch;

cout<<"1.INSERT"<<endl;

cout<<"2.DISPLAY"<<endl;

cout<<"3.EXIT"<<endl;

do

cout<<"Enter your choice : ";

cin>>ch;

switch(ch)

28
{

case 1:

insert();

display();

break;

case 2:display();

break;

case 3:cout<<"HAVE A NICE DAY"<<endl;

break;

default:cout<<"Wrong input "<<endl;

break;

while(ch!=3);

return 0;

29
OUTPUT:

2. WAP to perform pop operation from the stack. Check the

30
underflow condition using array.
#include<iostream>

using namespace std;

int top=4,stack[5]={10,20,30,40,50};

void pop()

if(top==-1)

cout<<"Stack is underflow"<<endl;

else

cout<<"Deleted element is : "<<stack[top]<<endl;;

top--;

void display()

if(top==-1)

cout<<"No Element to display"<<endl;

else

31
cout<<"Stack elements are : "<<endl;

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

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

cout<<endl;

int main()

int ch;

cout<<"1.DELET"<<endl;

cout<<"2.DISPLAY"<<endl;

cout<<"3.EXIT"<<endl;

do

cout<<"Enter your choice : ";

cin>>ch;

switch(ch)

case 1:

32
pop();

display();

break;

case 2:display();

break;

case 3:cout<<"HAVE A NICE DAY!!!"<<endl;

break;

default:cout<<"Wrong input "<<endl;

break;

while(ch!=3);

return 0;

OUTPUT:
1.DELET

2.DISPLAY

3.EXIT

Enter your choice : 1

33
Deleted element is : 50

Stack elements are : 10 20

30 40

Enter your choice : 1


Deleted element is : 40

Stack elements are :

10 20 30

Enter your choice : 1

Deleted element is : 30

Stack elements are : 10

20

Enter your choice : 1

Deleted element is : 20

Stack elements are : 10

Enter your choice : 1

Deleted element is : 10

No Element to display

Enter your choice : 1

Stack is underflow

No Element to display

Enter your choice : 3

HAVE A NICE DAY!!!

34
3. WAP to implement stack by Array.
#include<iostream>

using namespace std;

int top=-1,stack[10];

void insert()

int max=9;

if(top==max)

cout<<"Stack is overflow"<<endl;

else

{ top++;

cout<<"Enter the element : ";

cin>>stack[top];

void pop()

if(top==-1)

cout<<"Stack is underflow"<<endl;

else

35
{

cout<<"Deleted element is : "<<stack[top]<<endl;;

top--;

void display()

if(top==-1)

cout<<"No elements to display"<<endl;

else

cout<<"Stack elements are : "<<endl;

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

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

cout<<endl;

int main()

36
{

int ch;

cout<<"1.INSERT"<<endl;

cout<<"2.DELET"<<endl;

cout<<"3.DISPLAY"<<endl;

cout<<"4.EXIT"<<endl;

do

cout<<"Enter your choice : ";

cin>>ch;

switch(ch)

case 1:

insert();

break;

case 2:

pop();

37
}

break;

case 3:display();

break;

case 4:cout<<"HAVE A NICE DAY!!!"<<endl;

break;

default:cout<<"Wrong input "<<endl;

break;

while(ch!=4);

return 0;

OUTPUT
1.INSERT

2.DELET

3.DISPLAY 4.EXIT

Enter your choice : 1

Enter the element : 45

Enter your choice : 1

Enter the element : 67

Enter your choice : 1

Enter the element : 87

38
Enter your choice : 1

Enter the element : 3

Enter your choice : 1

Enter the element : 76

Enter your choice : 3

Stack elements are :

45 67 87 3 76 Enter your choice : 2

Deleted element is : 76

Enter your choice : 2

Deleted element is : 3

Enter your choice : 3

Stack elements are :

45 67 87

Enter your choice : 4

HAVE A NICE DAY!!!

4. WAP to perform decimal to binary conversion using a stack.


#include<iostream>

using namespace std;

int main()

{
int a,arr[100],top=-1,n;

cout<<"Enter the element to convert : ";

cin>>n;

while(n!=0)

39
top++;

a = n%2;

arr[top]=a;

n=n/2;

cout<<"Equivalent Binary no. : ";

while(top!=-1)

cout<<arr[top];

top--;

return 0;
}

OUTPUT:
Enter the element to convert : 25

Equivalent Binary no. : 11001

PROGRAM 5

WAP to reverse a string using stack.


#include<iostream>

#include<string.h> using

namespace std; int main()

40
int l=0; string

s;

cout<<"Enter the string : ";

cin>>s;

l=s.length(); char stack[l]; for(int

i=0;i<l;i++) stack[i]=s[i]; cout<<"String in

reverse order"<<endl; for(int i=l-1;i>=0;i--)

cout<<stack[i]; return 0;

OUTPUT:
Enter the string : MANSI

String in reverse order

ISNAM

PROGRAM 6

WAP to evaluate postfix expression using a stack.


#include <iostream>

#include <string> using

namespace std; int main()

int temp, l,top1=0,top2=0; string

exp;

char op1[100],op2[100]; cout<<"Enter the

expression : "<<endl; cin>>exp; l = exp.length();

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

41
if(exp[i]=='(')

temp = top2;

else if(exp[i]==')')

int t = top2;

for(int j=t;j>temp;j--)

top1++;

op1[top1]=op2[top2];

top2--;

top2 = top2 - 1;

else if((exp[i]=='+')||(exp[i]=='-'))

while(((op2[top2]=='+')||(op2[top2]=='-'))&&(top2>=0))

top1++;

op1[top1] = op2[top2];

top2--;

top2++;

op2[top2]=exp[i];

42
else if((exp[i]=='*')||(exp[i]=='/'))

while(((op2[top2]=='+')||(op2[top2]=='-
')||(op2[top2]=='*')||(op2[top2]=='/'))&&(top2>0))

top1++;

op1[top1]=op2[top2];

top2--;

top2++;

op2[top2]=exp[i];

else

top1++;

op1[top1]=exp[i];

if(i==(l-1))

while(top2>=0)

top1++;

op1[top1]=op2[top2];

top2--;

43
}

cout<<"Postfix expression is : "<<endl; for(int i=0;i<top1;i++)

cout<<op1[i];

return 0;

OUTPUT:

PROGRAM 7

WAP to implement queue using array and perform operations: a)


traverse b) insert c) delete. #include<iostream>

using namespace std; int rear=-

1,front=-1,queue[10]; void insert()

int max=9; if(rear==max)

cout<<"Queue is full"<<endl;

else

if(front==-1)

front=0;

rear++;

cout<<"Enter the element : ";

44
cin>>queue[rear];

void pop()

if(front==-1) cout<<"Queue is

empty"<<endl; else

cout<<"Deleted element is : "<<queue[front]<<endl;; front++;

} void display()

int max=9; if(front==max+1)

cout<<"No elements to display"<<endl;

else

cout<<"QUEUE IS : "<<endl;

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

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

cout<<endl;

int main()

{ int ch;

45
cout<<"1.INSERT"<<endl; cout<<"2.DELETE"<<endl;

cout<<"3.DISPLAY"<<endl; cout<<"4.EXIT"<<endl; do

cout<<"Enter your choice : ";

cin>>ch;

switch(ch)

case 1:

insert();

break;

case 2:

pop();

break;

case 3:display();

break;

case 4:cout<<"HAVE A NICE DAY!!!"<<endl;

break;

default:cout<<"Wrong input "<<endl;

break;

}while(ch!=4);

46
return 0;

OUTPUT:
1.INSERT

2.DELETE

3.DISPLAY 4.EXIT

Enter your choice : 1

Enter the element : 49

Enter your choice : 1

Enter the element : 56

Enter your choice : 1

Enter the element : 76

Enter your choice : 1

Enter the element : 45

Enter your choice : 1

Enter the element : 34

Enter your choice : 3 QUEUE IS :

49 56 76 45 34 Enter your choice : 2

Deleted element is : 49

Enter your choice : 2

Deleted element is : 56

Enter your choice : 3

QUEUE IS :

76 45 34

Enter your choice : 1

Enter the element : 98

Enter your choice : 3 QUEUE IS :

76 45 34 98

47
Enter your choice : 4

HAVE A NICE DAY!!!

PROGRAM 8

WAP to implement Queue using linked list and perform operations:


a) Create b) traverse c) insert d) delete

#include <stdlib.h> #include

<iostream> using namespace

std; struct node

int data; struct

node *next;

};

struct node* front = NULL;

struct node* rear = NULL;

struct node* temp; void

Insert()

{ int val;

cout<<"Enter the element : "<<endl;

cin>>val; if (rear == NULL)

rear = (struct node *)malloc(sizeof(struct node));

rear->next = NULL; rear->data = val; front = rear;

} else

temp=(struct node *)malloc(sizeof(struct node));

rear->next = temp; temp->data = val;

48
temp->next = NULL; rear

= temp;

void Delete()

temp = front; if

(front == NULL)

cout<<" Queue is Empty"<<endl;

return;

else if (temp->next != NULL)

temp = temp->next; cout<<"Element deleted from queue is :

"<<front->data<<endl;

free(front);

front = temp;

} else

cout<<"Element deleted from queue is : "<<front->data<<endl;

free(front);

front = NULL; rear

= NULL;

void Display()

49
temp = front; if ((front == NULL) && (rear

== NULL))

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

return;

cout<<"Queue elements are: "; while

(temp != NULL)

cout<<temp->data<<" ";

temp = temp->next;

cout<<endl;

int main()

{ int

ch;

cout<<"1

.INSERT"

<<endl;

cout<<"2

.DELET"<

<endl;

cout<<"3

.DISPLAY

"<<endl;

cout<<"4

.EXIT"<<

50
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<<"HAVE A NICE DAY!!!"<<endl;

break;

default:

cout<<"Wrong Choice"<<endl;

break;

} while(ch!=4);

return 0;

OUTPUT:
1.INSERT

2.DELET

3.DISPLAY

4.EXIT

Enter your choice : 1

Enter the element : 65

Enter your choice : 1

51
Enter the element : 78

Enter your choice : 1

Enter the element : 76

Enter your choice : 1

Enter the element :

90

Enter your choice :

Enter the element :

75

Enter your choice :

Queue elements are: 65 78 76 90 75

Enter your choice :

Element deleted from queue is : 65

Enter your choice :

Element deleted from queue is : 78

Enter your choice :

Queue elements are: 76 90 75

Enter your choice :

HAVE A NICE DAY!!!

PROGRAM 9

WAP to insert an element in circular queue.


#include<iostream> using

52
namespace std; int rear=-

1,front=-

1,queue[5],c=0,limit=5; void

push()

if(((front==0)&&(rear==limit-1))||((front==rear+1)&&(c>0)))

cout<<"Queue is overflow "<<endl;

else

if(front==-1)

front=0;

cout<<"Enter the element : "; if((rear==limit-1)&&(front!

=0))

rear=0;

cin>>queue[rear];

c=c+1;

else

rear++;

cin>>queue[rear];

53
void display()

if(((front==-1)&&(rear==-1))||((front==rear+1)&&(c==0)))

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

else

cout<<"Queue elements are : "<<endl;

if(front>rear)

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

cout<<queue[i]<<" "; for(int i=0;i<=rear;i++)

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

else if(front<=rear)

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

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

cout<<endl;

int main()

int ch; cout<<"1.INSERT"<<endl;

cout<<"2.DISPLAY"<<endl;

cout<<"3.EXIT"<<endl;

54
do

cout<<"Enter your choice : ";

cin>>ch;

switch(ch)

case 1:push();

break;

case 2:display();

break;

case 3:cout<<"HAVE A NICE DAY!!!"<<endl;

break;

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

break;

}while(ch!=3);

return 0;

OUTPUT:
1.INSERT

2.DISPLAY

3.EXIT

Enter your choice : 1

Enter the element : 67

Enter your choice : 1

55
Enter the element : 87

Enter your choice : 1

Enter the element : 78

Enter your choice : 1

Enter the element : 98

Enter your choice : 1

Enter the element : 56

Enter your choice : 2

Queue elements are :

67 87 78 98 56

Enter your choice : 3

HAVE A NICE DAY!!!

PROGRAM 10:

WAP to delete an element from circular queue.


#include<iostream>

using namespace std; int

rear=4,front=0,queue[5]={10,20,30,40,50},c=0,d=0,limit=5; void pop()

if(((front==-1)&&(rear==-1))||((front==rear+1)&&(c==d)))

cout<<"Queue is underflow "<<endl;

else

if((front==limit)&&(rear>=0))

d=d+1;

front=0;

56
cout<<"Deleted element is : "<<queue[front]<<endl;

front++;

else

cout<<"Deleted element is : "<<queue[front]<<endl;

front++;

void display()

if(((front==-1)&&(rear==-1))||((front==rear+1)&&(c==d)))

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

else

cout<<"Queue is: "<<endl;

if(front>rear)

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

cout<<queue[i]<<" "; for(int i=0;i<=rear;i++)

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

else if(front<=rear)

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

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

cout<<endl;

int main()

int ch; cout<<"1.DELETE"<<endl;

cout<<"2.DISPLAY"<<endl;

cout<<"3.EXIT"<<endl;

do

cout<<"Enter your choice : ";

cin>>ch;

switch(ch)

case 1:pop();

break;

case 2:display();

break;

case 3:cout<<"HAVE A NICE DAY!!!"<<endl;

break;

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

break;

58
}

}while(ch!=3); return

0;

OUTPUT:
1.DELETE

2.DISPLAY

3.EXIT

Enter your choice : 2 Queue is:

10 20 30 40 50

Enter your choice : 1

Deleted element is : 10

Enter your choice : 1

Deleted element is : 20

Enter your choice : 2 Queue is:

30 40 50

Enter your choice : 3

HAVE A NICE DAY!!!

LAB ASSIGNMENT 3
PROGRAM 1

59
WAP to create and traverse a linear linked list.
#include <iostream>

using namespace std;

struct Node {

int data;

struct Node *next;

};

struct Node* head = NULL;

void insert(int new_data) {

struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = head;//NULL,100,200

head = new_node;

60
void display() {

struct Node* ptr;

ptr = head;

while (ptr != NULL) {

cout<< ptr->data <<” “;

ptr = ptr->next;

int main() {

insert(3);

insert(1);

insert(7);

insert(2);

insert(9);

61
cout<<“The linked list is: “;

display();

return 0;

OUTPUT:
The linked list is: 9 2 7 1 3

PROGRAM 2:
WAP for making a linear linked list with following operation: (1)
Insertion (at beginning, at End, at particular position).
#include<iostream> using

namespace std; struct Node

{ int data;

Node *next;

};

class List

{ Node *head=NULL; Node *rear=NULL; Node *temp;

public:

void ins(); intdisp();

};

void List::ins()

{ Node *temp=new Node;intval; cout<<"\

nEnter Value:"; cin>>val; temp-

62
>data=val; temp->next=NULL;

if(head==NULL)

{ head=temp; rear=temp; } else { char place;

cout<<"\nEnter place (B OR E):"; cin>>place;

if(place=='e' || place=='E')

{rear->next=temp; rear=temp; } else

if(place=='b' || place=='B')

{ temp->next=head; head=temp; }

} } int

List::disp()

{ if(head==NULL)

{ cout<<"\nList Underflow"; return 0; }

temp=head; while(temp!=NULL)

{ cout<<temp->data<<" "; temp=temp->next; }

return 0;

int main()

{ List L;

L.ins();L.ins(); L.ins();L.disp(); return 0;

PROGRAM 3:
WAP for linked list with following operation: 1) Deletion (from
beginning, from end and at particular position)
#include<iostream> using

namespace std; struct Node

63
{ int data; Node *next;}; class List

{ Node *head=NULL; Node *rear=NULL; Node *temp;

public:

void ins(); int del();intdisp();

};

void List::ins()

{ Node *temp=new Node; intval; cout<<"\

nEnter Value:"; cin>>val; temp->data=val;

temp->next=NULL; if(head==NULL)

{ head=temp; rear=temp; } else { char place;

cout<<"\nEnter place (B OR E):"; cin>>place;

if(place=='e' || place=='E')

{rear->next=temp;

rear=temp; //FOR INSERTION AT END

else if(place=='b' || place=='B') { temp->next=head;

head=temp; //FOR INSERTION AT THE BEGINNING

else cout<<"\nWrong Choice";

int List::del()

{ if(head==NULL)

{ cout<<"\nList Underflow"; return 0; } else

{ char place; cout<<"\nEnter place to delete (B

OR E):"; cin>>place; if(place=='e' ||

64
place=='E') { temp=head; while(temp!

=NULL)

{ if(temp->next==rear)

{ cout<<rear->data<<" deleted from the List\n"; delete rear;

temp->next=NULL;

temp=rear; return 0;

temp=temp->next;

else if(place=='b' || place=='B') { temp=head;

head=head->next; cout<<temp->data<<" deleted

from the List\n"; delete temp;

return 0;

else cout<<"\nWrong Choice";

} } int

List::disp()

{ if(head==NULL)

{ cout<<"\nList Underflow"; return 0; }

temp=head; cout<<"\nList Status:-\n";

while(temp!=NULL)

{ cout<<temp->data<<" "; temp=temp->next; } return 0;

int main()

{ List L;

L.ins(); L.ins();

65
L.ins(); L.disp(); L.del();

L.disp(); return 0;

PROGRAM 4

WAP to search an element from linear linked list.


#include<iostream> using

namespace std; struct

Node { int data; Node

*next;}; class List

{ Node *head=NULL; Node *rear=NULL; Node *temp;

public:

void ins(); intdisp();int search();

};

void List::ins()

{ Node *temp=new Node; intval; cout<<"\

nEnter Value:"; cin>>val; temp->data=val;

temp->next=NULL; if(head==NULL)

{ head=temp; rear=temp; } else { char place;

cout<<"\nEnter place (B OR E):"; cin>>place;

if(place=='e' || place=='E') {rear->next=temp;

rear=temp; //FOR INSERTION AT END

else if(place=='b' || place=='B') { temp->next=head;

head=temp; //FOR INSERTION AT THE BEGINNING

66
else cout<<"\nWrong Choice";

} } int

List::disp()

{ if(head==NULL)

{ cout<<"\nList Underflow"; return 0; }

temp=head; cout<<"\nList Status:-\n";

while(temp!=NULL)

{ cout<<temp->data<<" "; temp=temp->next; } return 0;

int List::search()

{ if(head==NULL)

{ cout<<"\nList Underflow"; return 0; }

intele,i=0,flg=0; cout<<"\nEnter element to

search:"; cin>>ele; temp=head; while(temp!

=NULL)

{ i++; if(temp->data==ele)

{ cout<<endl<<ele<<" found at position "<<i; flg++;

return 0;

temp=temp->next;

if(flg==0) cout<<"\nElement not found"; return 0;

int main()

{ List L; L.ins(); L.ins(); L.ins();

L.search(); return 0;

67
OUTPUT:

Enter value:10

Enter value:20 Enter element

to search:40

Element not found.

PROGRAM 5

WAP to sort the elements of a linear linked list.


#include <iostream> using namespace std;

struct node{ int data; struct node

*next; }; struct node *head, *tail = NULL;

//addNode() will add a new node to the list

void addNode(int data) {

struct node newNode = (struct node)malloc(sizeof(struct node));

newNode->data = data; newNode->next = NULL; if(head == NULL) {

head = newNode;

tail = newNode;

} else {

tail->next = newNode; tail =

newNode;

//sortList() will sort nodes of the list in ascending order void sortList() {

//Node current will point to head struct node

*current = head, *index = NULL; int temp;

68
if(head == NULL) { return; } else {

while(current != NULL) {

//Node index will point to node next to current

index = current->next; while(index != NULL) {

if(current->data > index->data) { temp = current-

>data; current->data = index->data;

index->data = temp;

} index =

index->next;

} current =

current->next;

display() void display() {

struct node *current = head;

if(head == NULL) {

printf("List is empty \n");

return;

while(current != NULL) {

//Prints each node by incrementing pointer

cout<<current->data; current = current->next;

printf("\n");

69
}

int main()

addNode(9);

addNode(7); addNode(2);

addNode(5); addNode(4);

cout<<"Original list: ";

display(); sortList();

cout<<"Sorted list: “; display();

return 0;

OUTPUT:
Original list:
9 7 2 5 4 Sorted
list:
24579

program 6

WAP to reverse the elements of a linear linked list.


#include <iostream> using

namespace std;

struct node

{ int num; struct

node *next;

};

70
void create(struct node **);

void reverse(struct node **);

void release(struct node **);

void display(struct node *);

int main()

struct node *p = NULL;

int n;

cout<<"Enter data into the list\n"; create(&p);

cout<<"Displaying the nodes in the list:\n";

display(p); printf("Reversing the list...\n");

reverse(&p); printf("Displaying the reversed

list:\n"); display(p); release(&p);

return 0;

void reverse(struct node **head)

{ struct node *p, *q, *r;

p = q = r = *head; p

= p->next->next; q = q-

>next; r->next = NULL;

q->next = r;

71
while (p != NULL)

{ r = q; q

= p; p = p-

>next; q->next

= r;

*head = q;

void create(struct node **head)

{ int c, ch; struct node

*temp, *rear;

do

cout<<"Enter number: "; cin>>c; temp = (struct

node *)malloc(sizeof(struct node)); temp->num = c;

temp->next = NULL;

if (*head == NULL)

*head = temp;

else

rear->next = temp;

rear = temp; cout<<"Do you wish to

72
continue [1/0]: "; cin>>ch; } while (ch != 0);

cout<<"\n";

void display(struct node *p)

while (p != NULL)

cout<<p->num;

p = p->next;

cout<<"\n";

void release(struct node **head)

struct node *temp = *head;

*head = (*head)->next; while

((*head) != NULL)

free(temp);

temp = *head;

(*head) = (*head)->next;

OUTPUT:
Enter data into the list

Enter number: 1

Do you wish to continue [1/0]: 1

73
Enter number: 2

Do you wish to continue [1/0]: 1

Enter number: 3

Do you wish to continue [1/0]: 1

Enter number: 4

Do you wish to continue [1/0]: 1 Enter number: 5

Do you wish to continue [1/0]: 0

Displaying the nodes in the list:

1 2 3 4 5

Reversing the list...

Displaying the reversed list:

5 4 3 2 1

PROGRAM 7

WAP to create and traverse a Doubly Linked List.


#include <iostream>

using namespace std;

struct Node { int data;

struct Node *prev;

struct Node *next;

};

struct Node* head = NULL; void insert(int newdata) { struct Node* newnode

= (struct Node*) malloc(sizeof(struct Node)); newnode->data = newdata;

newnode->prev = NULL; newnode->next = head;

if(head != NULL) head-

>prev = newnode ; head =

74
newnode;

void display() { struct Node*

ptr; ptr = head; while(ptr !

= NULL) { cout<< ptr->data

<<" "; ptr = ptr->next;

int main() { insert(3); insert(1);

insert(7); insert(2); insert(9);

cout<<"The doubly linked list is: ";

display(); return 0;

OUTPUT:
The doubly linked list is: 9 2 7 1 3

PROGRAM 8

WAP to insert an element (at beginning, at end and at particular


position) in Doubly Linked List
#include <stdio.h>
#include <stdlib.h>

struct node { int num;


struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;

void DlListcreation(int n);

75
void DlLinsertNodeAtBeginning(int num); void
DlLinsertNodeAtEnd(int num); void
DlLinsertNodeAtAny(int num, int pos);
void displayDlList(int a);

int main()
{
int n,num1,a,insPlc;
stnode = NULL;
ennode = NULL;
cout<<"\n\n Doubly Linked List : Insert new node at any position in a doubly linked list :\n";
cout<<"-----------------------------------------------------------------------------------\n";
cout<<" Input the number of nodes : "; cin>>n;
DlListcreation(n); a=1;
displayDlList(a);
cout<<" Input the position to insert a new node : "<<n+1;

cin>>insPlc;
cout<<" Input data for the position : "<< insPlc;
cin>>num1;
DlLinsertNodeAtAny(num1,insPlc);
a=2; displayDlList(a);
return 0;
}

void DlListcreation(int n)
{ int i, num;
struct node *fnNode;

if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)
{
cout<<" Input data for node 1 : "; cin>>num;

stnode->num = num; stnode-


>preptr = NULL; stnode->nextptr =
NULL; ennode = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)

76
{
cout<<" Input data for node "<< i;
cin>>num; fnNode->num = num;
fnNode->preptr = ennode; fnNode-
>nextptr = NULL; ennode->nextptr =
fnNode; ennode =
fnNode; } else {
cout<<" Memory can not be allocated.";
break;
}
}
} else
{
cout<<" Memory can not be allocated.";
}
}
}

void DlLinsertNodeAtAny(int num, int pos)


{
int i;
struct node * newnode, *tmp;
if(ennode == NULL)
{
cout<<" No data found in the list!\n";
}
else
{
tmp = stnode;
i=1;
while(i<pos-1 && tmp!=NULL)
{
tmp = tmp->nextptr;
i++;
}
if(pos == 1)
{
DlLinsertNodeAtBeginning(num);
}
else if(tmp == ennode)
{
DlLinsertNodeAtEnd(num);
}

77
else if(tmp!=NULL)
{
newnode = (struct node *)malloc(sizeof(struct node)); newnode-
>num = num;
//next address of new node is linking with the next address of temp node
newnode->nextptr = tmp->nextptr; newnode->preptr = tmp;
if(tmp->nextptr != NULL)
{
tmp->nextptr->preptr = newnode;
}
tmp->nextptr = newnode;
}
else
{
cout<<" The position you entered, is invalid.\n;
}
}
}
void DlLinsertNodeAtBeginning(int num)
{
struct node * newnode;
if(stnode == NULL)
{
cout<<" No data found in the list!\n";
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num; newnode->nextptr =
stnode; node newnode->preptr = NULL
stnode->preptr = newnode;
stnode = newnode; }
}

void DlLinsertNodeAtEnd(int num)


{
struct node * newnode;

if(ennode == NULL)
{
cout<<" No data found in the list!\n";
}

78
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num; newnode->nextptr =
NULL; newnode->preptr = ennode;
ennode->nextptr = newnode;
ennode = newnode;
}
}

void displayDlList(int m)
{
struct node * tmp;
int n = 1; if(stnode
== NULL)
{
cout<<" No data found in the List yet.";
}
else
{
tmp = stnode;
if (m==1)
{
cout<<"\n Data entered in the list are :\n";
}
else
{
cout<<"\n After insertion the new list are :\n";
}
while(tmp != NULL)
{
cout<<" node "<<n<< tmp->num;
n++;
tmp = tmp->nextptr; // current pointer moves to the next node
}
}}

OUTPUT:
Input the number of nodes : 3

Input data for node 1 : 2

Input data for node 2 : 5

79
Input data for node 3 : 8

Data entered in the list are :

node 1 : 2 node 2 : 5

node 3 : 8

Input the position ( 1 to 4 ) to insert a new node : 4

Input data for the position 4 : 9

After insertion the new list are :

node 1 : 2 node 2 : 5

node 3 : 8 node 4 : 9

PROGRAM 9

WAP to delete an element (from beginning, from end and from


particular position) in Doubly Linked List.
#include <iostream>

#include <stdlib.h> using

namespace std; struct node

{ int num; struct node *

preptr; struct node *

nextptr;

}*stnode, *ennode;

void DlListcreation(int n); void

DlListDeleteFirstNode(); void

DlListDeleteLastNode(); void

DlListDeleteAnyNode(int pos); void

80
displayDlList(int a);

int main()

int n,num1,a,insPlc;

stnode = NULL;

ennode = NULL;

cout<<"\n\n Doubly Linked List : Delete node from any position of a doubly linked list
:\n"; cout<<"----------------------------------------------------------------------------------\n";

cout<<" Input the number of nodes (3 or more ): "; cin>>n;

DlListcreation(n); a=1; displayDlList(a); cout<<"

Input the position to delete a node : "<<n; cin>>insPlc;

if(insPlc<1 || insPlc>n)

cout<<"\n Invalid position. Try again.\n ";

if(insPlc>=1 && insPlc<=n)

DlListDeleteAnyNode(insPlc);

a=2; displayDlList(a);

return 0;

void DlListcreation(int n)

81
{

int i, num; struct

node *fnNode;

if(n >= 1)

stnode = (struct node *)malloc(sizeof(struct node));

if(stnode != NULL)

cout<<" Input data for node 1 : "; // assigning data in the first node

cin>>num;

stnode->num = num;

stnode->preptr = NULL;

stnode->nextptr = NULL;

ennode = stnode; for(i=2;

i<=n; i++)

fnNode = (struct node *)malloc(sizeof(struct node));

if(fnNode != NULL)

cout<<" Input data for node %d : "<< i;

cin>>num;

fnNode->num = num; fnNode-

>preptr = ennode; fnNode->nextptr =

NULL; ennode->nextptr = fnNode;

82
ennode = fnNode;

else

cout<<" Memory can not be allocated.";

break;

} else

cout<<" Memory can not be allocated.";

void DlListDeleteAnyNode(int pos)

struct node *curNode;

int i;

curNode = stnode; for(i=1; i<pos &&

curNode!=NULL; i++)

curNode = curNode->nextptr;

if(pos == 1)

83
DlListDeleteFirstNode();

else if(curNode == ennode)

DlListDeleteLastNode();

else if(curNode != NULL)

curNode->preptr->nextptr = curNode->nextptr; curNode->nextptr->preptr

= curNode->preptr;

free(curNode); //Delete the n node

else

cout<<" The given position is invalid!\n";

void DlListDeleteFirstNode()

struct node * NodeToDel; if(stnode

== NULL)

cout<<" Delete is not possible. No data in the list.\n";

else

84
NodeToDel = stnode; stnode = stnode->nextptr; // move the next address of starting

node to 2 node stnode->preptr = NULL; // set previous address of staring node is NULL

free(NodeToDel); // delete the first node from memory

void DlListDeleteLastNode()

struct node * NodeToDel;

if(ennode == NULL)

cout<<" Delete is not possible. No data in the list.\n";

else

NodeToDel = ennode;

ennode = ennode->preptr; // move the previous address of the last node to 2nd last node
ennode->nextptr = NULL; // set the next address of last node to NULL
free(NodeToDel); // delete the last node

void displayDlList(int m)

struct node * tmp;

int n = 1; if(stnode ==

NULL)

85
cout<<" No data found in the List yet.";

else

tmp = stnode;

if (m==1)

cout<<"\n Data entered in the list are :\n";

else

cout<<"\n After deletion the new list are :\n";

while(tmp != NULL)

cout<<" node "<< n<< tmp->num);

n++; tmp = tmp->nextptr; // current pointer moves to the next

node

OUTPUT:
Input the number of nodes (3 or more ): 3

Input data for node 1 : 1

Input data for node 2 : 2

Input data for node 3 : 3

Data entered in the list are :

86
node 1 : 1 node 2 : 2

node 3 : 3

Input the position ( 1 to 3 ) to delete a node : 3

After deletion the new list are :

node 1 : 1

node 2 : 2

PROGRAM 10

WAP to perform following operations: a) create b) traverse c) insert


an element d) delete an element, on Circular linked List.
#include<iostream>
using namespace std;

struct Node
{ int data;
struct Node *next;
};
//insert a new node in an empty list
struct Node *insertInEmpty(struct Node *last, int new_data)
{
// if last is not null then list is not empty, so return if
(last != NULL) return last;

struct Node *temp = new Node;

temp -> data = new_data;


last = temp;

last->next = last;

return last;
}

struct Node *insertAtBegin(struct Node *last, int new_data)


{
if (last == NULL)

87
return insertInEmpty(last, new_data);

struct Node *temp = new Node;

temp -> data = new_data; temp ->


next = last -> next;
last -> next = temp;

return last;
}
struct Node *insertAtEnd(struct Node *last, int new_data)
{
if (last == NULL)
return insertInEmpty(last, new_data);

struct Node *temp = new Node;

temp -> data = new_data;


temp -> next = last -> next; last
-> next = temp; last = temp;

return last;
}
struct Node *insertAfter(struct Node *last, int new_data, int after_item)
{
if (last == NULL)
return NULL;

struct Node *temp, *p;


p = last -> next; do
{
if (p ->data == after_item)
{
temp = new Node; temp -
> data = new_data; temp ->
next = p -> next;
p -> next = temp;

if (p == last)
last = temp; return
last;
}
p = p -> next;
} while(p != last -> next);

88
cout << "The node with data "<<after_item << " is not present in the list." << endl; return last;

}
void traverseList(struct Node *last) { struct
Node *p;

if (last == NULL) {
cout << "Circular linked List is empty." << endl; return;
}
p = last -> next; // Point to the first Node in the list.

do {
cout << p -> data << "==>"; p
= p -> next; } while(p != last-
>next); if(p == last->next)
cout<<p->data;
cout<<"\n\n";
}

void deleteNode(Node** head, int key)


{
if (*head == NULL)
return;

if((*head)->data==key && (*head)->next==*head) { free(*head);


*head=NULL;
}
Node *last=*head,*d; if((*head)->data==key) {
while(last->next!=*head) last=last->next;

last->next=(*head)->next;
free(*head); *head=last->next;
}

// end of list is reached or node to be deleted not there in the list


while(last->next!=*head&&last->next->data!=key) { last=last->next;
}
// node to be deleted is found, so free the memory and display the list if(last-
>next->data==key) { d=last->next; last->next=d->next;
cout<<"The node with data "<<key<<" deleted from the list"<<endl;
free(d); cout<<endl;
cout<<"Circular linked list after deleting "<<key<<" is as follows:"<<endl; traverseList(last);
}

89
else
cout<<"The node with data "<< key << " not found in the list"<<endl;
}

// main Program
int main()
{
struct Node *last = NULL;

last = insertInEmpty(last, 30); last


= insertAtBegin(last, 20); last =
insertAtBegin(last, 10); last =
insertAtEnd(last, 40); last =
insertAtEnd(last, 60); last =
insertAfter(last, 50,40 );
cout<<"The circular linked list created is as follows:"<<endl;
traverseList(last); deleteNode(&last,10);
return 0;
}

OUTPUT:
The circular linked list created is as follows:

10==>20==>30==>40==>50==>60==>10

The node with data 10 is deleted from the list

Circular linked list after deleting 10 is as follows:

20==>30==>40==>50==>60==>20

90

You might also like