Question

In C++ please Implement a class for doubly linked list. Your doubly linked list should have...

In C++ please

Implement a class for doubly linked list. Your doubly linked list should have following members:

- A head pointer

- A tail pointer

- Function: insert at head

-Function: insert at tail

- Function: delete a node

- Function: search a node

- Function: traverse

- Function: reverse traverse

Test your implementation by calling member functions inside main.

Homework Answers

Answer #1

#include<iostream>
#include<cstdio>
#include<cstdlib>
/*
* Node Declaration
*/
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*head,*tail;//head and tail pointers

/*
Class Declaration
*/
class double_llist
{
public:
void create_list(int value);
void insert_At_head(int value);
void insert_At_tail(int value);
void delete_node(int value);
    int search_node(int value);
void traverse();
void reverse_traverse();;
double_llist()
{
head = tail=NULL;
}
};

/*
* Main: Conatins Menu
*/
int main()
{
int choice, element, position;
double_llist dl;
while (1)
{
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<"Operations on Doubly linked list"<<endl;
cout<<endl<<"----------------------------"<<endl;   
cout<<"1.Create Node"<<endl;
cout<<"2.Add at head"<<endl;
cout<<"3.Add at tail"<<endl;
cout<<"4.Delete"<<endl;
cout<<"5.Traverse"<<endl;
cout<<"6.Reverse_traverse"<<endl;
cout<<"7.Search"<<endl;
cout<<"8.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch ( choice )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.create_list(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element: ";
cin>>element;
dl.insert_At_head(element);
cout<<endl;
break;
case 3:
cout<<"Enter the element: ";
cin>>element;
dl.insert_At_tail(element);
cout<<endl;
break;
case 4:
if (head == NULL)
{
cout<<"List empty,nothing to delete"<<endl;   
break;
}
cout<<"Enter the element for deletion: ";
cin>>element;
dl.delete_node(element);
cout<<endl;
break;
case 5:
dl.traverse();
cout<<endl;
break;
case 6:
dl.reverse_traverse();
break;
case 7:
           cout<<"Enter element to search:";
           cin>>element;
if( dl.search_node(element)==1)cout<<"Found\n";
else cout<<"Not Found\n";
cout<<endl;
break;
case 8:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}

/*
* Create Double Link List
*/
void double_llist::create_list(int value)
{
struct node *s, *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
temp->prev = NULL;
head = temp;
  
tail = head;
}

/*
* Insertion at the beginning
*/
void double_llist::insert_At_head(int value)
{
if (head == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->prev = NULL;
temp->info = value;
temp->next = head;
head->prev = temp;
head = temp;
cout<<"Element Inserted"<<endl;
}

/*
* Insertion of element at a tail
*/
void double_llist::insert_At_tail(int value)
{
if (head == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp = new (struct node);
temp->prev=tail;
temp->next=NULL;
temp->info=value;
tail->next=temp;
tail=temp;
cout<<"Element Inserted"<<endl;
}

/*
* Deletion of element from the list
*/
void double_llist::delete_node(int value)
{
struct node *tmp, *q;
/*first element deletion*/
if (head->info == value)
{
tmp = head;
head = head->next;
head->prev = NULL;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
//if tail node
if(tail->info==value)
{
       tmp=tail;
       tail=tail->prev;
       tail->next=NULL;
       cout<<"Element Deleted"<<endl;
       free(tmp);
       return ;  
   }
q = head;
while (q->next->next != NULL)
{   
/*Element deleted in between*/
if (q->next->info == value)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = q->next;
}
/*last element deleted*/
if (q->next->info == value)
{   
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}
cout<<"Element "<<value<<" not found"<<endl;
}

/*
* Display elements of Doubly Link List
*/
void double_llist::traverse()
{
struct node *q;
if (head == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}
q = head;
cout<<"The Doubly Link List is :"<<endl;
while (q != NULL)
{
cout<<q->info<<" <-> ";
q = q->next;
}
cout<<"NULL"<<endl;
}

/*
* Number of elements in Doubly Link List
*/
void double_llist::reverse_traverse()
{   
struct node *q = tail;
int cnt = 0;
while (q != NULL)
{cout<<q->info<<" ";
q = q->prev;
  
}
cout<<endl;

}
int double_llist::search_node(int value)
{
   struct node *q = tail;
  
while (q != NULL)
{
       if(q->info==value)return 1;//if found
q = q->prev;
  
}
return 0;//if not found
  
}

output


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 1
Enter the element: 2


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 2
Enter the element: 1
Element Inserted


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 3
Enter the element: 3
Element Inserted


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 5
The Doubly Link List is :
1 <-> 2 <-> 3 <-> NULL


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 6
3 2 1

----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 3
Enter the element: 2
Element Inserted


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 5
The Doubly Link List is :
1 <-> 2 <-> 3 <-> 2 <-> NULL


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 4
Enter the element for deletion: 2
Element Deleted


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 5
The Doubly Link List is :
1 <-> 2 <-> 3 <-> NULL


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 5
The Doubly Link List is :
1 <-> 2 <-> 3 <-> NULL


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 2
Enter the element: 1
Element Inserted


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 5
The Doubly Link List is :
1 <-> 1 <-> 2 <-> 3 <-> NULL


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 4
Enter the element for deletion: 2
Element Deleted


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 5
The Doubly Link List is :
1 <-> 1 <-> 3 <-> NULL


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 5
The Doubly Link List is :
1 <-> 1 <-> 3 <-> NULL


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 4
Enter the element for deletion: 1
Element Deleted


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 5
The Doubly Link List is :
1 <-> 3 <-> NULL


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 7
Enter element to search:3
Found


----------------------------

Operations on Doubly linked list

----------------------------
1.Create Node
2.Add at head
3.Add at tail
4.Delete
5.Traverse
6.Reverse_traverse
7.Search
8.Quit
Enter your choice : 8


Process exited with return value 1
Press any key to continue . . .

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head"...
C++ Assumption and Terminology: Forward Linked List FLL has "head" Doubly Linked List DLL has "head" and "tail" Each node in FLL has "next" Each node in DLL has "next" and "prev" Q1: Complete the following constructor code for FLL FLL::FLL() { Q2: Complete the FLL insert-front code (which insert node new at the front of FLL) FLL::insert-front( node * new) { Q3: Complete the DLL insert-after code (which insert node new after node p) DLL::insert-after(node *p, node *new) {
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...
Please answer in C A doubly linked list is a list in which each entry contains...
Please answer in C A doubly linked list is a list in which each entry contains a pointer to the preceding entry in the list as well as a pointer to the next entry in the list. Define the appropriate structure for the doubly linked list. Write a user space program that implements a doubly linked list and prints out the elements of the list. Develop insert_entry() and remove_entry() functions for this list. The link list should store information on...
Using the given SList as a template to implement a doubly linked list with the following...
Using the given SList as a template to implement a doubly linked list with the following functionalities Add first: add an element to the head of the list Add last: add an element to the end of the list Print: print the content of the list starting from the head Find: a private method that will return a reference to a node if it contains the same value of a given argument Insert after: a method that takes a key...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first...
C++ questions QUESTION 1 What kind of linked list begins with a pointer to the first node, and each node contains a pointer to the next node, and the pointer in the last node points back to the first node? A. Circular, singly-linked list. B. Circular, doubly-linked list. C. Singly-linked list. D. Doubly-linked list. E. None of the above.    QUESTION 2 _________ is not an advantage of linked lists when compared to arrays. A. Dynamic memory allocation. B. Efficient...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked...
Q1; Write a method in class SLL called public SLL reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. What is the big-O complexity of your method in terms of the list size n Supplementary Exercise for Programming (Coding) [Singly Linked Lists] Download and unpack (unzip) the file SinglyLinkedList.rar. Compile and execute the class...
(a)   Write a method in class SLL<T> called public SLL<T> reverse() that produces a new linked...
(a)   Write a method in class SLL<T> called public SLL<T> reverse() that produces a new linked list with the contents of the original list reversed. Make sure not to use any methods like addToHead() or addToTail(). In addition, consider any special cases that might arise. (b)   What is the big-O complexity of your method in terms of the list size n. ****** //************************ SLLNode.java ******************************* //           node in a generic singly linked list class public class SLLNode<T> {     public...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT