Professional Documents
Culture Documents
Assignment 1
Assignment 1
Assignment 1
On
BCA 355
Data Structure Using C ++ Lab
2020-2021(Odd Semester)
1
ASSIGNMENT I [ARRAY AND SEARCHING]
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
Program
#include<iostream>
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;
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;
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];
}
}
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;
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
int top=-1,stack[5];
void insert()
int max=4;
if(top==max)
cout<<"Stack is overflow"<<endl;
else
{ top++;
cin>>stack[top];
void display()
27
if(top==-1)
cout<<"Stack is empty"<<endl;
else
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
cin>>ch;
switch(ch)
28
{
case 1:
insert();
display();
break;
case 2:display();
break;
break;
break;
while(ch!=3);
return 0;
29
OUTPUT:
30
underflow condition using array.
#include<iostream>
int top=4,stack[5]={10,20,30,40,50};
void pop()
if(top==-1)
cout<<"Stack is underflow"<<endl;
else
top--;
void display()
if(top==-1)
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
cin>>ch;
switch(ch)
case 1:
32
pop();
display();
break;
case 2:display();
break;
break;
break;
while(ch!=3);
return 0;
OUTPUT:
1.DELET
2.DISPLAY
3.EXIT
33
Deleted element is : 50
30 40
10 20 30
Deleted element is : 30
20
Deleted element is : 20
Deleted element is : 10
No Element to display
Stack is underflow
No Element to display
34
3. WAP to implement stack by Array.
#include<iostream>
int top=-1,stack[10];
void insert()
int max=9;
if(top==max)
cout<<"Stack is overflow"<<endl;
else
{ top++;
cin>>stack[top];
void pop()
if(top==-1)
cout<<"Stack is underflow"<<endl;
else
35
{
top--;
void display()
if(top==-1)
else
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
cin>>ch;
switch(ch)
case 1:
insert();
break;
case 2:
pop();
37
}
break;
case 3:display();
break;
break;
break;
while(ch!=4);
return 0;
OUTPUT
1.INSERT
2.DELET
3.DISPLAY 4.EXIT
38
Enter your choice : 1
Deleted element is : 76
Deleted element is : 3
45 67 87
int main()
{
int a,arr[100],top=-1,n;
cin>>n;
while(n!=0)
39
top++;
a = n%2;
arr[top]=a;
n=n/2;
while(top!=-1)
cout<<arr[top];
top--;
return 0;
}
OUTPUT:
Enter the element to convert : 25
PROGRAM 5
#include<string.h> using
40
int l=0; string
s;
cin>>s;
cout<<stack[i]; return 0;
OUTPUT:
Enter the string : MANSI
ISNAM
PROGRAM 6
exp;
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<<op1[i];
return 0;
OUTPUT:
PROGRAM 7
cout<<"Queue is full"<<endl;
else
if(front==-1)
front=0;
rear++;
44
cin>>queue[rear];
void pop()
if(front==-1) cout<<"Queue is
empty"<<endl; else
} void display()
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
cin>>ch;
switch(ch)
case 1:
insert();
break;
case 2:
pop();
break;
case 3:display();
break;
break;
break;
}while(ch!=4);
46
return 0;
OUTPUT:
1.INSERT
2.DELETE
3.DISPLAY 4.EXIT
Deleted element is : 49
Deleted element is : 56
QUEUE IS :
76 45 34
76 45 34 98
47
Enter your choice : 4
PROGRAM 8
node *next;
};
Insert()
{ int val;
} else
48
temp->next = NULL; rear
= temp;
void Delete()
temp = front; if
(front == NULL)
return;
"<<front->data<<endl;
free(front);
front = temp;
} else
free(front);
= NULL;
void Display()
49
temp = front; if ((front == NULL) && (rear
== NULL))
cout<<"Queue is empty"<<endl;
return;
(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
case 1: Insert();
break; case 2:
Delete(); break;
case 3: Display();
break;
break;
default:
cout<<"Wrong Choice"<<endl;
break;
} while(ch!=4);
return 0;
OUTPUT:
1.INSERT
2.DELET
3.DISPLAY
4.EXIT
51
Enter the element : 78
90
75
PROGRAM 9
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)))
else
if(front==-1)
front=0;
=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
if(front>rear)
for(int i=front;i<limit;i++)
else if(front<=rear)
for(int i=front;i<=rear;i++)
cout<<queue[i]<<" ";
cout<<endl;
int main()
cout<<"2.DISPLAY"<<endl;
cout<<"3.EXIT"<<endl;
54
do
cin>>ch;
switch(ch)
case 1:push();
break;
case 2:display();
break;
break;
default:cout<<"Wrong choice"<<endl;
break;
}while(ch!=3);
return 0;
OUTPUT:
1.INSERT
2.DISPLAY
3.EXIT
55
Enter the element : 87
67 87 78 98 56
PROGRAM 10:
if(((front==-1)&&(rear==-1))||((front==rear+1)&&(c==d)))
else
if((front==limit)&&(rear>=0))
d=d+1;
front=0;
56
cout<<"Deleted element is : "<<queue[front]<<endl;
front++;
else
front++;
void display()
if(((front==-1)&&(rear==-1))||((front==rear+1)&&(c==d)))
cout<<"Queue is empty"<<endl;
else
if(front>rear)
for(int i=front;i<limit;i++)
else if(front<=rear)
57
for(int i=front;i<=rear;i++)
cout<<queue[i]<<" ";
cout<<endl;
int main()
cout<<"2.DISPLAY"<<endl;
cout<<"3.EXIT"<<endl;
do
cin>>ch;
switch(ch)
case 1:pop();
break;
case 2:display();
break;
break;
default:cout<<"Wrong choice"<<endl;
break;
58
}
}while(ch!=3); return
0;
OUTPUT:
1.DELETE
2.DISPLAY
3.EXIT
10 20 30 40 50
Deleted element is : 10
Deleted element is : 20
30 40 50
LAB ASSIGNMENT 3
PROGRAM 1
59
WAP to create and traverse a linear linked list.
#include <iostream>
struct Node {
int data;
};
new_node->data = new_data;
new_node->next = head;//NULL,100,200
head = new_node;
60
void display() {
ptr = head;
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
{ int data;
Node *next;
};
class List
public:
};
void List::ins()
62
>data=val; temp->next=NULL;
if(head==NULL)
if(place=='e' || place=='E')
if(place=='b' || place=='B')
{ temp->next=head; head=temp; }
} } int
List::disp()
{ if(head==NULL)
temp=head; while(temp!=NULL)
return 0;
int main()
{ List L;
PROGRAM 3:
WAP for linked list with following operation: 1) Deletion (from
beginning, from end and at particular position)
#include<iostream> using
63
{ int data; Node *next;}; class List
public:
};
void List::ins()
temp->next=NULL; if(head==NULL)
if(place=='e' || place=='E')
{rear->next=temp;
int List::del()
{ if(head==NULL)
64
place=='E') { temp=head; while(temp!
=NULL)
{ if(temp->next==rear)
temp->next=NULL;
temp=rear; return 0;
temp=temp->next;
return 0;
} } int
List::disp()
{ if(head==NULL)
while(temp!=NULL)
int main()
{ List L;
L.ins(); L.ins();
65
L.ins(); L.disp(); L.del();
L.disp(); return 0;
PROGRAM 4
public:
};
void List::ins()
temp->next=NULL; if(head==NULL)
66
else cout<<"\nWrong Choice";
} } int
List::disp()
{ if(head==NULL)
while(temp!=NULL)
int List::search()
{ if(head==NULL)
=NULL)
{ i++; if(temp->data==ele)
return 0;
temp=temp->next;
int main()
L.search(); return 0;
67
OUTPUT:
Enter value:10
to search:40
PROGRAM 5
head = newNode;
tail = newNode;
} else {
newNode;
//sortList() will sort nodes of the list in ascending order void sortList() {
68
if(head == NULL) { return; } else {
while(current != NULL) {
index->data = temp;
} index =
index->next;
} current =
current->next;
if(head == NULL) {
return;
while(current != NULL) {
printf("\n");
69
}
int main()
addNode(9);
addNode(7); addNode(2);
addNode(5); addNode(4);
display(); sortList();
return 0;
OUTPUT:
Original list:
9 7 2 5 4 Sorted
list:
24579
program 6
namespace std;
struct node
node *next;
};
70
void create(struct node **);
int main()
int n;
return 0;
p = q = r = *head; p
= p->next->next; q = q-
q->next = r;
71
while (p != NULL)
{ r = q; q
= p; p = p-
>next; q->next
= r;
*head = q;
*temp, *rear;
do
temp->next = NULL;
if (*head == NULL)
*head = temp;
else
rear->next = temp;
72
continue [1/0]: "; cin>>ch; } while (ch != 0);
cout<<"\n";
while (p != NULL)
cout<<p->num;
p = p->next;
cout<<"\n";
((*head) != NULL)
free(temp);
temp = *head;
(*head) = (*head)->next;
OUTPUT:
Enter data into the list
Enter number: 1
73
Enter number: 2
Enter number: 3
Enter number: 4
1 2 3 4 5
5 4 3 2 1
PROGRAM 7
};
struct Node* head = NULL; void insert(int newdata) { struct Node* newnode
74
newnode;
display(); return 0;
OUTPUT:
The doubly linked list is: 9 2 7 1 3
PROGRAM 8
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;
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.";
}
}
}
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; }
}
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
79
Input data for node 3 : 8
node 1 : 2 node 2 : 5
node 3 : 8
node 1 : 2 node 2 : 5
node 3 : 8 node 4 : 9
PROGRAM 9
nextptr;
}*stnode, *ennode;
DlListDeleteFirstNode(); void
DlListDeleteLastNode(); 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";
if(insPlc<1 || insPlc>n)
DlListDeleteAnyNode(insPlc);
a=2; displayDlList(a);
return 0;
void DlListcreation(int n)
81
{
node *fnNode;
if(n >= 1)
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;
i<=n; i++)
if(fnNode != NULL)
cin>>num;
82
ennode = fnNode;
else
break;
} else
int i;
curNode!=NULL; i++)
curNode = curNode->nextptr;
if(pos == 1)
83
DlListDeleteFirstNode();
DlListDeleteLastNode();
= curNode->preptr;
else
void DlListDeleteFirstNode()
== NULL)
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
void DlListDeleteLastNode()
if(ennode == NULL)
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)
int n = 1; if(stnode ==
NULL)
85
cout<<" No data found in the List yet.";
else
tmp = stnode;
if (m==1)
else
while(tmp != NULL)
node
OUTPUT:
Input the number of nodes (3 or more ): 3
86
node 1 : 1 node 2 : 2
node 3 : 3
node 1 : 1
node 2 : 2
PROGRAM 10
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;
last->next = last;
return last;
}
87
return insertInEmpty(last, new_data);
return last;
}
struct Node *insertAtEnd(struct Node *last, int new_data)
{
if (last == NULL)
return insertInEmpty(last, new_data);
return last;
}
struct Node *insertAfter(struct Node *last, int new_data, int after_item)
{
if (last == NULL)
return NULL;
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";
}
last->next=(*head)->next;
free(*head); *head=last->next;
}
89
else
cout<<"The node with data "<< key << " not found in the list"<<endl;
}
// main Program
int main()
{
struct Node *last = NULL;
OUTPUT:
The circular linked list created is as follows:
10==>20==>30==>40==>50==>60==>10
20==>30==>40==>50==>60==>20
90