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

Doubly Final

The code implements doubly circular linked lists in C++. It defines a node structure with next and prev pointers and initializes a head node. Functions are provided for insertion and deletion at the beginning and end of the list, as well as inserting and deleting a node at a specified location. The main function contains a menu to call the appropriate functions and display the list. Pointers are used to traverse the list and manage connections between nodes when modifying the structure.

Uploaded by

Aayush Parajuli
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)
47 views16 pages

Doubly Final

The code implements doubly circular linked lists in C++. It defines a node structure with next and prev pointers and initializes a head node. Functions are provided for insertion and deletion at the beginning and end of the list, as well as inserting and deleting a node at a specified location. The main function contains a menu to call the appropriate functions and display the list. Pointers are used to traverse the list and manage connections between nodes when modifying the structure.

Uploaded by

Aayush Parajuli
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/ 16

Code For Operation On Doubly Linear Linked List

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
using namespace std;
struct node
{ int info;
struct node *next,*prev;
} *pnew, *pthis, *ptemp, *pfirst = NULL,*plast=NULL,*ptemp1;
void create();
void insertion_beg();
void insertion_end();
void deletion_beg();
void deletion_end();
void inserting_sp();
void deleting_sp();
void display();

int count=0, loc,choice;


int main()
{ system("cls");
int i;
char ans;
do
{ system("cls");
cout << "1.insertion from beginning" << endl
<< "2.insertion From end" << endl
<< "3.deletion from beginning" << endl
<< "4.deletion from end" << endl
<< "5.display" << endl
<< "6.inserting before specified " << endl
<< "7.Inserting after specied " << endl
<< "8.deleting speciffed " << endl
<< "9.exit."<<endl
<< "Enter your choice" << endl;
cin >> choice;
switch (choice)
{

Aayush Parajuli 780303


case 1:
insertion_beg();
display();
break;
case 2:
insertion_end();
display();
break;
case 3:
deletion_beg();
display();
break;
case 4:
deletion_end();
display();
break;
case 5:
display();
break;
case 6:
inserting_sp();
display();
break;
case 7:
inserting_sp();
display();
break;
case 8:
deleting_sp();
display();
break;
case 9:
exit(0);
}
cout << endl << "Do you want to continue?" << endl;
cin >> ans;
} while (ans != 'n');
getch();
return 0;

Aayush Parajuli 780303


}
void create()
{ int data;
pnew = (struct node *)malloc(sizeof(struct node));
cout << "Enter data" << endl;
cin >>data;
pnew->info=data;
count++;
}
void insertion_beg()
{
create();
if (pfirst == NULL){
pfirst = pnew;
plast=pnew;
pnew->next = NULL;
pnew->prev=NULL;
}
else{
pnew->prev=NULL;
pnew->next = pfirst;
pfirst->prev=pnew;
pfirst = pnew;
}
}
void insertion_end()
{
create();
if (pnew == NULL){
pfirst = pnew;
plast=pnew;
pnew->next = NULL;
pnew->prev=NULL;
}
else{
plast->next=NULL;
plast->next=pnew;
pnew->prev=plast;
plast=pnew;

Aayush Parajuli 780303


}
}
void deletion_beg()
{
if (pfirst == NULL){
cout << "empty" << endl; }
else{
if (pfirst->next == NULL){
cout << " deleted data: " << pfirst->info << endl;
free(pfirst);
pfirst = NULL;
plast=NULL;
count--;
}
else {
ptemp = pfirst->next;
cout << "deleted data: " << pfirst->info << endl;
free(pfirst);
pfirst = ptemp;
pfirst->prev=NULL;
count--;
}
}
}
void deletion_end()
{
if (pfirst == NULL){
cout << "empty" << endl;}
else
{
if (pfirst->next == NULL) {
cout << " deleted data: " << pfirst->info << endl;
free(pfirst);
count--;
pfirst = NULL;
plast=NULL;

}
else

Aayush Parajuli 780303


{
ptemp=plast->prev;
cout << "deleted data:" << plast->info << endl;
free(plast);
count--;
ptemp->next= NULL;
plast=ptemp;
}
}
}
void display()
{
cout << "Data: " << endl;
if (pfirst == NULL) {
cout << "Empty" << endl; }
else{
pthis = pfirst;

while (pthis != plast){


cout << pthis->info << " ";
pthis = pthis->next;}
cout<<plast->info;
}
}
void inserting_sp()
{
cout << "Enter node no" << endl;
cin >> loc;
if(loc>count){
cout << " invalid" << endl;
}
else{
pthis = pfirst;
if(choice==6){
if(loc==1){
insertion_beg();
}
else{
create();

Aayush Parajuli 780303


for (int i = 1; i <=loc-1; i++){
pthis = pthis->next;
}
ptemp=pthis->prev;
ptemp->next=pnew;
pnew->prev=ptemp;
pnew->next=pthis;
pthis->prev=pnew;
}}
if(choice==7){
if(loc==count){
insertion_end();
}
else{
create();
for (int i = 1; i <=loc-1; i++) {
pthis = pthis->next;
}
ptemp=pthis->next;
ptemp->prev=pnew;
pnew->next=ptemp;
pnew->prev=pthis;
pthis->next=pnew;
}}
}
}
void deleting_sp()
{ int loc,i;
cout<<"Enter location to delete."<<endl;
cin>>loc;
if(loc==1){
deletion_beg();
}
else if(loc==count){
deletion_end();
}
else if(loc>count){
cout<<"NO node found."<<endl;
}

Aayush Parajuli 780303


else{
pthis=pfirst;
for(i=1;i<=loc-1;i++){
pthis=pthis->next;
}
ptemp1=pthis->next;
ptemp=pthis->prev;
ptemp->next=ptemp1;
ptemp1->prev=ptemp;
cout<<"Deleted data is: "<<pthis->info<<endl;
free(pthis);
count--;
}
}

OUTPUT

Aayush Parajuli 780303


Aayush Parajuli 780303
DISCUSSION
Above program demonstrates the implementation of a doubly linear linked list using C++. It
provides several operations to manipulate the list, such as insertion at the beginning and end,
deletion from the beginning and end, inserting before and after a specified node, deleting a
specified node, and displaying the contents of the list.
The program uses a structure called "node" to represent each element in the linked list. Each
node contains an integer value along with pointers to the next and previous nodes, allowing
for bidirectional traversal. The main function incorporates a menu-driven loop that enables
users to choose the desired operation. Based on the user's selection, the corresponding
functions are called to perform the required action on the linked list. The program also
utilizes dynamic memory allocation to create new nodes when necessary.

Code For Doubly Circular Linked List


#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
using namespace std;
struct node
{ int info;
struct node *next,*prev;
} *pnew, *pthis, *ptemp, *ptemp1,*head;
void create();
void insertion_beg();
void insertion_end();
void deletion_beg();
void deletion_end();
void inserting_sp();
void deleting_sp();
void display();

int count=0, loc,choice;


int main()
{ system("cls");
head = (struct node *)malloc(sizeof(struct node));
head->info=0;
head->next=head;
head->prev=head;
int i;

Aayush Parajuli 780303


char ans;
do
{ system("cls");
cout << "1.insertion from beginning" << endl
<< "2.insertion From end" << endl
<< "3.deletion from beginning" << endl
<< "4.deletion from end" << endl
<< "5.display" << endl
<< "6.inserting before specified " << endl
<< "7.Inserting after specied " << endl
<< "8.deleting speciffed " << endl
<< "9.exit."<<endl
<< "Enter your choice" << endl;
cin >> choice;
switch (choice)
{
case 1:
insertion_beg();
display();
break;
case 2:
insertion_end();
display();
break;
case 3:
deletion_beg();
display();
break;
case 4:
deletion_end();
display();
break;
case 5:
display();
break;
case 6:
inserting_sp();
display();
break;

Aayush Parajuli 780303


case 7:
inserting_sp();
display();
break;
case 8:
deleting_sp();
display();
break;
case 9:
exit(0);
}
cout << endl << "Do you want to continue?" << endl;
cin >> ans;
} while (ans != 'n');
getch();
return 0;
}
void create()
{ int data;
pnew = (struct node *)malloc(sizeof(struct node));
cout << "Enter data" << endl;
cin >>data;
pnew->info=data;
count++;
}
void insertion_beg()
{
create();
if (head->next== head){
head->next = pnew;
head->prev=pnew;
pnew->next = head;
pnew->prev=head;
}
else{
ptemp=head->next;
head->next=pnew;
pnew->prev = head;
ptemp->prev=pnew;

Aayush Parajuli 780303


pnew->next=ptemp;
}
}
void insertion_end()
{
create();
if (head->next== head){
head->next = pnew;
head->prev=pnew;
pnew->next = head;
pnew->prev=head;
}
else{
ptemp=head->prev;
head->prev=pnew;
pnew->next=head;
pnew->prev=ptemp;
ptemp->next=pnew;
}
}
void deletion_beg()
{
if (head->next==head){
cout << "empty" << endl; }
else{
ptemp = head->next->next;
cout << "deleted data: " <<head->next->info << endl;
free(head->next);
head->next = ptemp;
ptemp->prev=head;
count--;
}
}
void deletion_end()
{
if (head->next==head){
cout << "No node." << endl;}
else
{

Aayush Parajuli 780303


ptemp=head->prev->prev;
cout << "deleted data:" << head->prev->info << endl;
free(head->prev);
ptemp->next=head;
head->prev=ptemp;
count--;
}
}
void display()
{
cout << "Data: " << endl;
if (head->next==head) {
cout << "Empty" << endl; }
else{
pthis = head->next;

while (pthis != head){


cout << pthis->info << " ";
pthis = pthis->next;}
}
}
void inserting_sp()
{
cout << "Enter node no" << endl;
cin >> loc;
if(loc>count||loc<1){
cout << " invalid" << endl;
}
else{
pthis = head->next;
if(choice==6){
if(loc==1){
insertion_beg();
}
else{
create();
for (int i = 1; i <=loc-1; i++){
pthis = pthis->next;
}

Aayush Parajuli 780303


ptemp=pthis->prev;
ptemp->next=pnew;
pnew->prev=ptemp;
pnew->next=pthis;
pthis->prev=pnew;
}}
if(choice==7){
if(loc==count){
insertion_end();
}
else{
create();
for (int i = 1; i <=loc-1; i++) {
pthis = pthis->next;
}
ptemp=pthis->next;
ptemp->prev=pnew;
pnew->next=ptemp;
pnew->prev=pthis;
pthis->next=pnew;
}}
}
}
void deleting_sp()
{ int loc,i;
cout<<"Enter location to delete."<<endl;
cin>>loc;
if(loc==1){
deletion_beg();
}
else if(loc==count){
deletion_end();
}
else if(loc>count){
cout<<"NO node found."<<endl;}
else{
pthis=head->next;
for(i=1;i<=loc-1;i++){
pthis=pthis->next;

Aayush Parajuli 780303


}
ptemp1=pthis->next;
ptemp=pthis->prev;
ptemp->next=ptemp1;
ptemp1->prev=ptemp;
cout<<"Deleted data is: "<<pthis->info<<endl;
free(pthis);
count--;
}
}
OUTPUT

Aayush Parajuli 780303


DISCUSSION
Above program is an implementation of a doubly circular linked list in C++. It uses a
structure called node to represent each node in the list, with three members: info to store the
data, next to point to the next node, and prev to point to the previous node, allowing for
bidirectional traversal. The program offers various operations such as insertion and deletion
at the beginning and end, insertion before and after a specified node, deletion of a specified
node, and displaying the contents of the linked list. It utilizes a head pointer to keep track of
the head of the list.

Aayush Parajuli 780303

You might also like