Question

In: Computer Science

In C++ Write the definition for following methods of List data structure. 5. pushFront – The...

In C++

Write the definition for following methods of List data structure.

5. pushFront – The function appends an element in the list at the front. The operation increases the number of elements in the list by one.

6. pushback - The function appends an element in the list at the end. The operation increases the number of elements in the list by one.

7.popFront - The function returns and then removes an element in the list from the front. The operation decreases the number of elements in the list by one.

8.popBack - - The function returns and then removes an element in the list from the end. The operation decreases the number of elements in the list by one.

Solutions

Expert Solution

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

struct Node
 {
    int val;
    struct Node *next;
    Node(int data)
      {
         val=data;
         next=NULL;
      }
 };

void pushFront(Node *&head, int data)
  {
     if(head==NULL)
       {
        head=new Node(data);
        return ;
       }
     struct Node *temp;
     temp = new Node(data);
     temp->next = head;
     head = temp;
  }

void pushback(Node *&head, int data)
  {
     if(head==NULL)
      {
        head = new Node(data);
        return ;
      }
     struct Node *cur=head;
     while(cur->next!=NULL)
       {
         cur = cur->next;
       }
     cur->next = new Node(data);
  }

void popFront(struct Node *&head)
  {
     if(head==NULL)
        return ;
     head=head->next;

  }

void popBack(struct Node *&head)
  {
     if(head==NULL)
        return ;
     struct Node *cur=head,*pre=NULL;

     while(cur->next!=NULL)
      {
         pre=cur;
         cur=cur->next;
      }
      if(pre)
        pre->next=NULL;
      else
        head=NULL;
  }

void display(struct Node *head)
 {
     if(head==NULL)
     return ;
     while(head!=NULL)
      {
          cout<<head->val<<" ";
          head=head->next;
      }
      cout<<endl;
 }

int main()
 {
   struct Node *head=NULL;

   pushFront(head, 5); // here it append 5  at the beginning in list.
   display(head);      //  5 is printed

   pushback(head, 3); // here it append 3 at end of list
   display(head);     // 5 3 is printed

   popFront(head);   // it pop 5 from the list
   display(head);    // 3 is printed

   popBack(head);    // it pop the element from the end of list
   display(head);    // nothing is printed.
 }

// This code is executable if any problem you will face to understand the code you can comment.

Related Solutions

In C++ Write the definition for following methods of List data structure. 1. setList – The...
In C++ Write the definition for following methods of List data structure. 1. setList – The function set the value of list elements equal to a value passed as the function input. 2. getAt – The function returns an element of the list referred by its position which is given to the function as input. 3. insertAt – The function inserts a given element passed as function input in the list at the specified position also passed as second function...
it should be c++ data structure Write a recursive function that draws the following shape. X...
it should be c++ data structure Write a recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX XXX X The length of the longest row in the shape and the shape's character are function parameters. In above shape the longest row is 9 and the pattern's character is "X”.
5. List the methods for a compensation structure. 6. What other ways are used to determine...
5. List the methods for a compensation structure. 6. What other ways are used to determine wages? 7. List the different alternatives of incentive plans. 8. What are employee’s benefits? Explain. 9. Why it is a concern for companies the cost of benefits? 10. List the benefits required by law? 11. What are the options of voluntary benefits?
Write the following task in C++1) Write the definition of a function numOccurrences thatsearches...
Write the following task in C++1) Write the definition of a function numOccurrences that searches for a character in a character array and returns the number of times it occurs in the array. The function has three formal parameters: a char array array, an int variable arraySize representing the size of the array, and a character variable letter representing the character to be searched for in the array.2) Assume the array numbers is an int array of size 10 and...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
A deque is a data structure consisting of a list of items, on which the following...
A deque is a data structure consisting of a list of items, on which the following operations are possible: • push(x): Insert item x on the front end of the deque. • pop(): Remove the front item from the deque and return it. • inject(x): Insert item x on the rear end of the deque. • eject(): Remove the rear item from the deque and return it. Write routines to support the deque that take O(1) time per operation. In...
In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It...
In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It returns the amount of times the maximum value of data appears in data. If data is empty, it returns 0. For instance, if data = [7, 1, 4, 9, 6, 7, 7, 3, 2, 6, 9, 5, 9], it will return 3 since 9 appears three times
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
C++ - Checks the relevance of a data structure in terms of following the interface specification...
C++ - Checks the relevance of a data structure in terms of following the interface specification for an ADT that represents a linear data structure: Depending on the ADT of linear data structure, you must create the operations CRUD (Create, Read (search), Update, Delete) elements in the data structure. Some operations do not apply for certain data structures Create: Description: Insert an element in the data structure (create) according to the access policy of the structure Input:Data structure and element...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT