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

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,...
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]
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
a) Write C code using a struct to hold student information: integer id integer number of...
a) Write C code using a struct to hold student information: integer id integer number of hours taken integer number of hours passed double gpa b) create a variable of the type in #25 and initialize it with some data.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT