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

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.
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method displays inches in feet and inches. For example, 67 inches is 5 feet 7 inches.
Create a C++ program that follows the specifications below: *Define a struct with 4 or more...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more members. *Application must have at least one user-defined function *Declare an array of your struct using a size of 10 or more *Load the date for each element in your array from a text file *Display the data in your array in the terminal *provide brief comments for each line of code
Write a program that reads an integer, a list of words, and a character.
13.14 LAB: Contains the characterWrite a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character.Ex: If the input is:4 hello zoo sleep drizzle zthen the output is:zoo drizzleIn c++ 
Create a mathematical proof to prove the following: Given an integer n, and a list of...
Create a mathematical proof to prove the following: Given an integer n, and a list of integers such that the numbers in the list sum up to n. Prove that the product of a list of numbers is maximized when all the numbers in that list are 3's, except for one of the numbers being either a 2 or 4, depending on the remainder of n when divided by 3.
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Write a class named RetailItem that holds data about an item in retail store.
Python 3Your program will have 2 classes:A) RetailItem ClassWrite a class named RetailItem that holds data about an item in retail store.Attributes: The class should store following data in attributes:>item_Name> PriceMethods:> RetailItem class’s __init__ method should accept an argument for each attribute.> RetailItem class should also have accessor and mutator methods for each attributeB) MainMenu ClassAttributes: The class should store following data in attributes:> List of RetailItem Objects: InventoryMethods:> createInventory(): method to create three RetailItem Objects store in list Inventory...
Related with the below statements, write the missing words: A variation of linked list, named as...
Related with the below statements, write the missing words: A variation of linked list, named as Doubly Linked List contains a link element called _______,_______. The heap sort has an average-case complexity of _________. The algorithms like merge sort, quick sort and binary search are based on __________ and _____________ algorithm. In stack terminology, the insertion operation is defined to be ___________. The asymptotic notation O(1) defines the complexity of type ___________. Looking for the next cell in the array,...
C++ Design a class named TermPaper that holds an author's name, the subject of the paper,...
C++ Design a class named TermPaper that holds an author's name, the subject of the paper, and an assigned letter grade. Include methods to set the values for each data field and display the values for each data field. Create the class diagram and write the pseudocode that defines the class. Pseudocode help please
Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT