CS201 MID TERM SOLVED MCQs || PAST PAPERS || GROUP-1 || INTRODUCTION TO PROGRAMMING || VuTech Visit Website For More Solutions www.vutechofficial.blogspot.com …
Visit Website For More Solutions www.vutechofficial.blogspot.com
KINDLY, DON’T COPY PASTE
Problem:
You are required to make a program using C++ language to implement singly
linked list of random size, populate it with the NUMERIC digits of your VU
ID (according to the size that is already generated randomly). And then find
the middle node of that linked list, handling both even and odd
lengths.
Solution:
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<time.h>
using namespace std;
class Node
{
private:
int object;
Node *nextNode;
public:
void set(int value)
{
object = value;
}
int get()
{
return object;
}
void setNext(Node* ptr)
{
nextNode = ptr;
}
Node* getNext()
{
return nextNode;
}
};
class List
{
public:
Node* headNode;
Node* currentNode;
Node* lastCurrentNode;
List()
{
headNode = NULL;
currentNode = NULL;
lastCurrentNode = NULL;
}
void add(List obj)
{
if(headNode == NULL)
{
headNode = obj.currentNode;
lastCurrentNode =
obj.currentNode;
}
else
{
lastCurrentNode ->
setNext(obj.currentNode);
lastCurrentNode =
obj.currentNode;
}
}
void get(int size)
{
currentNode = headNode;
for(int i = 1; i < size;
i++)
{
next();
}
cout<<"\nThe middle element
is : "<<currentNode ->get();
}
void next()
{
currentNode = currentNode ->
getNext();
}
friend void traverse();
friend List addNodes(int);
friend List findMiddle();
};
void traverse(List obj)
{
obj.currentNode =
obj.headNode;
while(obj.currentNode
!=NULL)
{
cout<<"\tList Element
"<<obj.currentNode -> get()<<"\n";
obj.next();
}
}
List addNodes(int value)
{
List obj;
obj.currentNode = new Node;
obj.currentNode ->
set(value);
obj.currentNode ->
setNext(NULL);
return obj;
}
List findMiddle(int size,List obj)
{
obj.get((size/2)+1);
}
main()
{
int size,id;
List obj;
srand(time(0));
size = 3 +(rand() % 7);
cout<<"Randomly generated
size of the linked list is : "<<size<<"\n";
for(int i = 1; i<=size;
i++)
{
cout<<"Enter NUMERIC
character of your vu id\n";
cin>>id;
obj.add(addNodes(id));
}
cout<<"\n\n";
traverse(obj);
findMiddle(size,obj);
getch();
return 0;
}
KINDLY, DON’T COPY PASTE SUBSCRIBE, SHARE, LIKE AND COMMENTS FOR MORE UPDATES SEND WHATSAPP OR E-MAIL FOR ANY QUERY 0325-6644800 kamranhameedvu@gmail.com Visit Website For More Solutions www.vutechofficial.blogspot.com
We provide Virtual University of Pakistan Study Materials such as Solution of Assignments, GDBs, Mid Term Solved Papers, Final Term Solved Papers, Mid Term Solved MCQs, and Final Term Solved MCQs. We also provide regular Semester Quizzes, Updated Handouts, and Short Questions and Answers. We help you with your research and many other educational-related topics, as far as we know. Furthermore, Share your problem with us and Please feel free to ask any related questions.