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...
C++ Write a definition for a structure type for records for books. The record contains ISBN...
C++ Write a definition for a structure type for records for books. The record contains ISBN number, price, and book cover (use H for Hard cover and P for paper cover). Part of the problem is appropriate choices of type and member names. Then declare two variables of the structure. Write a function called GetData use the structure as function argument to ask the user to input the value from the keyboard for structure variables.
Write a program to implement linked list data structure that will have following functions: a. Append...
Write a program to implement linked list data structure that will have following functions: a. Append a node in the list b. Insert a node in the list c. Delete a node from the list d. Display list e. Find maximum value in the list f. Find how many times a value exists in the list. g. Search Portion of the code is give below. You have to write code for the items (e, f, g) Program: #include<stdlib.h> #include<stdio.h> #include<iostream>...
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?
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”.
Using classes and array data structure write methods with algorithms for a software that an airline...
Using classes and array data structure write methods with algorithms for a software that an airline can use to view available/booked seats, management booking, canceling booking and reorder seats. The solution should consist of a minimum of two classes with one class containing the main method and second class for managing a manipulating data. The choice of data structure for this assignment will be static one dimension arrays. in C++
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
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT