Question

In: Computer Science

Write in C++: create a Doubly Linked List class that holds a struct with an integer...

Write in C++:

create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.

Solutions

Expert Solution

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. /*
  5.  * Node Declaration
  6.  */
  7. using namespace std;
  8. struct node
  9. {
  10.     int info;
  11.     struct node *next;
  12.     struct node *prev;
  13. }*start;
  14.  
  15. /*
  16.  Class Declaration 
  17.  */
  18. class double_llist
  19. {
  20.     public:
  21.         void create_list(int value);
  22.         void add_begin(int value);
  23.         void add_after(int value, int position);
  24.         void delete_element(int value);
  25.         void search_element(int value);
  26.         void display_dlist();
  27.         void count();
  28.         void reverse();
  29.         double_llist()
  30.         {
  31.             start = NULL;  
  32.         }
  33. };
  34.  
  35. /*
  36.  * Main: Conatins Menu
  37.  */
  38. int main()
  39. {
  40.     int choice, element, position;
  41.     double_llist dl;
  42.     while (1)
  43.     {
  44.         cout<<endl<<"----------------------------"<<endl;
  45.         cout<<endl<<"Operations on Doubly linked list"<<endl;
  46.         cout<<endl<<"----------------------------"<<endl;         
  47.         cout<<"1.Create Node"<<endl;
  48.         cout<<"2.Add at begining"<<endl;
  49.         cout<<"3.Add after position"<<endl;
  50.         cout<<"4.Delete"<<endl;
  51.         cout<<"5.Display"<<endl;
  52.         cout<<"6.Count"<<endl;
  53.         cout<<"7.Reverse"<<endl;
  54.         cout<<"8.Quit"<<endl;
  55.         cout<<"Enter your choice : ";
  56.         cin>>choice;
  57.         switch ( choice )
  58.         {
  59.         case 1:
  60.             cout<<"Enter the element: ";
  61.             cin>>element;
  62.             dl.create_list(element);
  63.             cout<<endl;
  64.             break;
  65.         case 2:
  66.             cout<<"Enter the element: ";
  67.             cin>>element;
  68.             dl.add_begin(element);
  69.             cout<<endl;
  70.             break;
  71.         case 3:
  72.             cout<<"Enter the element: ";
  73.             cin>>element;
  74.             cout<<"Insert Element after postion: ";
  75.             cin>>position;
  76.             dl.add_after(element, position);
  77.             cout<<endl;
  78.             break;
  79.         case 4:
  80.             if (start == NULL)
  81.             {                      
  82.                 cout<<"List empty,nothing to delete"<<endl;   
  83.                 break;
  84.             }
  85.             cout<<"Enter the element for deletion: ";
  86.             cin>>element;
  87.             dl.delete_element(element);
  88.             cout<<endl;
  89.             break;
  90.         case 5:
  91.             dl.display_dlist();
  92.             cout<<endl;
  93.             break;
  94.         case 6:
  95.             dl.count();
  96.             break;    
  97.         case 7:
  98.             if (start == NULL)
  99.             {
  100.                 cout<<"List empty,nothing to reverse"<<endl;
  101.                 break;
  102.             }
  103.             dl.reverse();
  104.             cout<<endl;
  105.             break;
  106.         case 8:
  107.             exit(1);
  108.         default:
  109.             cout<<"Wrong choice"<<endl;
  110.         }
  111.     }
  112.     return 0;
  113. }
  114.  
  115. /*
  116.  * Create Double Link List
  117.  */
  118. void double_llist::create_list(int value)
  119. {
  120.     struct node *s, *temp;
  121.     temp = new(struct node); 
  122.     temp->info = value;
  123.     temp->next = NULL;
  124.     if (start == NULL)
  125.     {
  126.         temp->prev = NULL;
  127.         start = temp;
  128.     }
  129.     else
  130.     {
  131.         s = start;
  132.         while (s->next != NULL)
  133.             s = s->next;
  134.         s->next = temp;
  135.         temp->prev = s;
  136.     }
  137. }
  138.  
  139. /*
  140.  * Insertion at the beginning
  141.  */
  142. void double_llist::add_begin(int value)
  143. {
  144.     if (start == NULL)
  145.     {
  146.         cout<<"First Create the list."<<endl;
  147.         return;
  148.     }
  149.     struct node *temp;
  150.     temp = new(struct node);
  151.     temp->prev = NULL;
  152.     temp->info = value;
  153.     temp->next = start;
  154.     start->prev = temp;
  155.     start = temp;
  156.     cout<<"Element Inserted"<<endl;
  157. }
  158.  
  159. /*
  160.  * Insertion of element at a particular position
  161.  */
  162. void double_llist::add_after(int value, int pos)
  163. {
  164.     if (start == NULL)
  165.     {
  166.         cout<<"First Create the list."<<endl;
  167.         return;
  168.     }
  169.     struct node *tmp, *q;
  170.     int i;
  171.     q = start;
  172.     for (i = 0;i < pos - 1;i++)
  173.     {
  174.         q = q->next;
  175.         if (q == NULL)
  176.         {
  177.             cout<<"There are less than ";
  178.             cout<<pos<<" elements."<<endl;
  179.             return;
  180.         }
  181.     }
  182.     tmp = new(struct node);
  183.     tmp->info = value;
  184.     if (q->next == NULL)
  185.     {
  186.         q->next = tmp;
  187.         tmp->next = NULL;
  188.         tmp->prev = q;      
  189.     }
  190.     else
  191.     {
  192.         tmp->next = q->next;
  193.         tmp->next->prev = tmp;
  194.         q->next = tmp;
  195.         tmp->prev = q;
  196.     }
  197.     cout<<"Element Inserted"<<endl;
  198. }
  199.  
  200. /*
  201.  * Deletion of element from the list
  202.  */
  203. void double_llist::delete_element(int value)
  204. {
  205.     struct node *tmp, *q;
  206.      /*first element deletion*/
  207.     if (start->info == value)
  208.     {
  209.         tmp = start;
  210.         start = start->next;  
  211.         start->prev = NULL;
  212.         cout<<"Element Deleted"<<endl;
  213.         free(tmp);
  214.         return;
  215.     }
  216.     q = start;
  217.     while (q->next->next != NULL)
  218.     {   
  219.         /*Element deleted in between*/
  220.         if (q->next->info == value)  
  221.         {
  222.             tmp = q->next;
  223.             q->next = tmp->next;
  224.             tmp->next->prev = q;
  225.             cout<<"Element Deleted"<<endl;
  226.             free(tmp);
  227.             return;
  228.         }
  229.         q = q->next;
  230.     }
  231.      /*last element deleted*/
  232.     if (q->next->info == value)    
  233.     {   
  234.         tmp = q->next;
  235.         free(tmp);
  236.         q->next = NULL;
  237.         cout<<"Element Deleted"<<endl;
  238.         return;
  239.     }
  240.     cout<<"Element "<<value<<" not found"<<endl;
  241. }
  242.  
  243. /*
  244.  * Display elements of Doubly Link List
  245.  */
  246. void double_llist::display_dlist()
  247. {
  248.     struct node *q;
  249.     if (start == NULL)
  250.     {
  251.         cout<<"List empty,nothing to display"<<endl;
  252.         return;
  253.     }
  254.     q = start;
  255.     cout<<"The Doubly Link List is :"<<endl;
  256.     while (q != NULL)
  257.     {
  258.         cout<<q->info<<" <-> ";
  259.         q = q->next;
  260.     }
  261.     cout<<"NULL"<<endl;
  262. }
  263.  
  264. /*
  265.  * Number of elements in Doubly Link List
  266.  */
  267. void double_llist::count()
  268. {       
  269.     struct node *q = start;
  270.     int cnt = 0;
  271.     while (q != NULL)
  272.     {
  273.         q = q->next;
  274.         cnt++;
  275.     }
  276.     cout<<"Number of elements are: "<<cnt<<endl;
  277. }
  278.  
  279. /*
  280.  * Reverse Doubly Link List
  281.  */
  282. void double_llist::reverse()
  283. {
  284.     struct node *p1, *p2;
  285.     p1 = start;
  286.     p2 = p1->next;
  287.     p1->next = NULL;
  288.     p1->prev = p2;
  289.     while (p2 != NULL)
  290.     {
  291.         p2->prev = p2->next;
  292.         p2->next = p1;
  293.         p1 = p2;
  294.         p2 = p2->prev; 
  295.     }
  296.     start = p1;
  297.     cout<<"List Reversed"<<endl; 
  298. }

Related Solutions

Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class,...
9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 Sortedlist.java import java.util.Scanner; public class SortedList { public static void main (String[] args)...
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT