Header Ads

You will create a linked list of random numbers and display the last asked elements of the linked list on the console (command prompt).


You are required to make a program using C++ language to implement a singly linked list. You will create a linked list of random numbers and display the last asked elements of the linked list on the console (command prompt). Following are the detailed requirements.

  1. Decide the size of the linked list
    1. Generate a random number between 15 to 20 and use it as the size of the linked list. Every time the program will be executed a different number should be generated.
  2. Fill/create the linked list with random numbers in the range of 1 to 100.
  3. Display the last few elements of the linked list on the console.
    1. The number of the last few elements will be entered by the user.
  4. If the user will enter 5 then display the last five elements of the linked list. If the user will enter 8 then display the last eight elements of the linked list. 

Important Instructions:

You need to fulfill the following requirements while solving the assignment task.

  1. Use only user-defined classes for Node and List, the use of struct is not allowed.
  2. The structure of Node and List classes should be similar to the structure given below. If you will create extra data members then your marks will be deducted.
  3. The user will enter the no. of elements he wants to see, so your application should display entered no. of the last elements. 
  4. An animated gif file is attached to the assignment file. This animation contains the procedure to select the last elements of your choice. Once you have selected the elements of your choice then you can display them easily.


Graphical Representation




Program


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

int skip_till;
int count_no=1;
int no_last_elem_vu;

int printRandoms(int lower, int upper,
int count)
{
int i;
for (i = 0; i < count; i++) {
int num = (rand() %
(upper - lower + 1)) + lower;

return num;
}
}


struct Node {
    int data;
    Node* next;
};

class LinkedList {
  private:
    Node* head;
  public:
    LinkedList(){
      head = NULL;
    }
 
    void push_back(int newElement) {
      Node* newNode = new Node();
      newNode->data = newElement;
      newNode->next = NULL; 
      if(head == NULL) {
        head = newNode;
      } else {
        Node* temp = head;
        while(temp->next != NULL)
          temp = temp->next;
        temp->next = newNode;
      }    
    }

    void PrintList() {
      Node* temp = head;
      if(temp != NULL) {
     
        cout<<endl;
        
        
        while(temp != NULL) {
        if (skip_till<=no_last_elem_vu){
         
        cout<<"Element "<<count_no<<" ";
        count_no=count_no+1;
          cout<<temp->data<<" "<<endl;
          temp = temp->next;
             while (skip_till != 0) { 
             
      skip_till=skip_till-1;}
        } else {
      skip_till=skip_till-1;
      temp = temp->next;}
    }
        
        cout<<endl;
      } 
    }    
};

 
int main() {
  LinkedList MyList;

int lower = 15, upper = 20, count = 1;
int random_length_linked;

srand(time(0));

printRandoms(lower, upper, count);

random_length_linked= printRandoms(lower, upper, count);
skip_till = random_length_linked;

cout << "Randomly generated list size : "<<random_length_linked<<endl;
for (int i = 0; i < random_length_linked; i++) {
int random_fill_linked= printRandoms(1, 100, 1);
cout<<random_fill_linked<<" inserted into linked list"<<endl;
   MyList.push_back(random_fill_linked);
 
}

cout << "Enter no. of last few elements want to see: ";
cin>>no_last_elem_vu;
   
  MyList.PrintList();

return 0;

}



Output





No comments

Powered by Blogger.