CS301 Assignment 1 Fall 2022 Solution file
#include<iostream>
#include<conio.h>
#include<time.h>
#include<windows.h>
using namespace std;
int size,number,limit;
class Node
{
private:
int marks;
Node* nextNode;
public:
void setMarks(int m)
{
marks = m;
}
int getmarks()
{
return marks;
}
void setNext(Node* a)
{
nextNode = a;
}
Node* getNext()
{
return nextNode;
}
};
class List
{
private:
Node* headNode;
Node* currentNode;
Node* lastCurrentNode;
public:
list()
{
headNode = NULL;
currentNode = NULL;
lastCurrentNode = NULL;
}
int get(Node* ptr)
{
return ptr -> getmarks();
}
void add(int n)
{
Node* newNode = new Node;
newNode -> setMarks(n);
newNode -> setNext(NULL);
if(headNode == NULL )
{
headNode = newNode;
currentNode = newNode;
lastCurrentNode = newNode;
}
else
{
lastCurrentNode -> setNext(newNode);
lastCurrentNode = newNode;
}
cout<<n<<" Inserted into linked list \n";
}
Node* next(Node* ptr)
{
return ptr -> getNext();
}
friend void traverse();
friend List addNode();
}L;
void traverse()
{
int count = 1, a=1;
Node* ptr=L.headNode;
while(ptr != NULL)
{
if(count > size-limit)
{
cout<<"element "<<a++<<" "<<L.get(ptr)<<"\n";
}
ptr = L.next(ptr);
count++;
}
}
List addNode()
{
srand(time(0));
number = rand()%51 + 50;
L.add(number);
}
main()
{
srand(time(0));
size = rand()%6 + 15;
cout<<"randomly generated list size: "<<size<<"\n\n";
for(int i=1; i<=size;i++)
{
Sleep(1000);
addNode();
}
cout<<"\nEnter no. of last elements you want to see: ";
cin>>limit;
cout<<"\n\n";
traverse();
getch();
return 0;
}
#2023#CS301 #CS301Assignment1SolutionSpring2022
#2023#CS301 #CS301Assignment1SolutionSpring2022
#2023#CS301 #CS301Assignment1SolutionSpring2022
#2023#CS301 #CS301Assignment1SolutionSpring2022
0 Comments
Post a Comment