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++ C++ Write a definition for a structure type for records for CDs. The record contains...
C++ C++ Write a definition for a structure type for records for CDs. The record contains price and total number of songs, . Part of the problem is appropriate choices of type and member names.Then declare two variables of this 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.
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”.
C++ 11.7: Customer Accounts Write a program that uses a structure to store the following data...
C++ 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account: Customer name Customer address City State ZIP code Telephone Account balance Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Prompts...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT