0% found this document useful (0 votes)
16 views4 pages

Doubly Link List

This document defines a C++ doubly linked list class with methods for adding and removing nodes from the front and end of the list. It includes a node class to represent each element with data and next/previous node pointers. The doubly_list class implements functions to insert and delete nodes from the front/end of the list and display the list. The main function provides a menu to test the different list operations.

Uploaded by

rida.maryam1215
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)
16 views4 pages

Doubly Link List

This document defines a C++ doubly linked list class with methods for adding and removing nodes from the front and end of the list. It includes a node class to represent each element with data and next/previous node pointers. The doubly_list class implements functions to insert and delete nodes from the front/end of the list and display the list. The main function provides a menu to test the different list operations.

Uploaded by

rida.maryam1215
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/ 4

#include <iostream>

using namespace std;

class node {
int data;
node* next;
node* pre;
public:

node()
{
data = 0;
next = NULL;
pre = NULL;
}
int getdata()
{
return data;
}
void setdata(int x)
{
data = x;
}
node* getnext()
{
return next;
}
void setnext(node *n)
{
next = n;
}
node* getprev()
{
return pre;
}
void setprev(node *n)
{
pre = n;
}

};

class doubly_list
{
node* head, *tail;
public:
doubly_list()
{
head = tail = NULL;
}
void insert_at_Front()
{
node* newnode = new node;
int d;
cout << "Enter a number" << endl;
cin >> d;
newnode->setdata(d);
newnode->setnext(NULL);
newnode->setprev(NULL);
if (head == NULL)
{
head = newnode;
tail = newnode;
cout << "Node Added at front" << endl;
}
else
{
newnode->setnext(head);
newnode->setprev(NULL);
head = newnode;
}
}
void insert_at_End() {
node* newnode = new node;
int d;
cout << "Enter a number" << endl;
cin >> d;
newnode->setdata(d);
newnode->setnext(NULL);
newnode->setprev(NULL);

if (head == NULL) {
head = newnode;
tail = newnode;
cout << "Node added at End" << endl;
}
else {

newnode->setprev(tail);
tail->setnext(newnode);
tail = newnode;
cout << "Node added at End" << endl;
}
}
void deleteFirstNode()
{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
node* newnode = new node;
newnode = head;
head = newnode->getnext();
newnode->setnext(NULL);
head->setprev(NULL);
}
}
void deleteEndNode()
{
if (head == NULL)
{
cout << "List is empty" << endl;
}
else
{
node* newnode = head;
while (newnode->getnext() != tail)
{
newnode = newnode->getnext();
}
newnode->setnext(NULL);
tail->setprev(NULL);
tail = newnode;
}
}
void search()
{
//previous lectures........
}
void display()
{
node* temp = head;
while (temp != NULL)
{
cout << "Data=" << temp->getdata() << endl;
temp = temp->getnext();
}
}
};

int main()
{
int ch = 0;
doubly_list dll;

while (ch != 7)
{
cout << "Plz enter your choice:" << endl;
cout << "1. Add Node at Front" << endl;
cout << "2. Add Node at End" << endl;
cout << "3. Delete First Node" << endl;
cout << "4. Delete End Node" << endl;
cout << "5. Search" << endl;
cout << "6. Display" << endl;
cout << "7. Exit" << endl;
cin >> ch;
switch (ch)
{
case 1:
dll.insert_at_Front();
break;
case 2:
dll.insert_at_End();
break;
case 3:
dll.deleteFirstNode();
break;
case 4:
dll.deleteEndNode();
break;
case 5:
dll.search();
break;
case 6:
dll.display();
break;

}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy