CS301 Assignment Solution Spring 2020 | VU Assignments Solution
CS301
Assignment Solution Spring 2020
If U Found Any
Mistake Then Correct Your Self
Lab 1
Lab Title: Learn to implement
linked list data structure
Objectives: Get the knowledge of implementing
linked list data structure using C++ programming language.
Tool: Dev C++
Description:
Write the C++ code to implement the List class. You need to write only
add( ). remove( ), and find( ) functions of the list class.
SOlution of this assignment is following code you just put this code in the dev c+ and save that file.
SOlution of this assignment is following code you just put this code in the dev c+ and save that file.
class Node {
                private:
                                //
Data part and pointer part of a node
        int number;
        Node
*nextNode;
    public:
                //To
get and set data of node
        int get()
{return number;};
        void set(int
number) {this->number = number;};
        //To get
address of next node in the linked list
        Node
*getNext() {return nextNode;};
        void
setNext(Node *nextNode){this->nextNode = nextNode;};      
};
list.cpp file:
#include "Node.cpp"
class List{
    private:
        int size;
        Node*
headNode;
        Node*
currentNode;
        Node*
lastCurrentNode;
    public:
List(){
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;   
lastCurrentNode = NULL ;
               size =
0;
        }
        void add(int
addNumber){
            Node
*newNode = new Node();
newNode->set(addNumber);
            if
(currentNode != NULL){
newNode->setNext(currentNode->getNext());
currentNode->setNext(newNode);
lastCurrentNode = currentNode;
currentNode = newNode;
            }
else{
newNode->setNext(NULL);
headNode->setNext(newNode);
lastCurrentNode = headNode ;
currentNode = newNode ;
            }
               size ++
;
        }
        void remove(){
                //store
locations of cur and lastcur pointers
                Node
*tempCurNode = currentNode;
                Node
*templstCurNode = lastCurrentNode;
                //Move
pointers to start of the list
                currentNode
= headNode->getNext();
                lastCurrentNode
= headNode;
            if
(currentNode != NULL &¤tNode != headNode){
                lastCurrentNode->setNext(currentNode->getNext());
                delete
currentNode;
                size--;
                                                }
                                                //Re-assign
the positions again to current node and lastcurrentnode
                                                currentNode
= tempCurNode;
                                                lastCurrentNode
= templstCurNode;
        }
        void find(List
list, int x){
                                                Node*
savedCurrentNode = list.currentNode;
                                                list.currentNode
= list.headNode;
                                                int
f = 0 ;
                                                while(
list.next()){
                                                                if
(x == list.get()){
                                                                                cout<<"\n
Element " << x << " is found \n" ;
                                                                                f
= 1 ;
                                                                }
                                                }
                                                if
(f !=1)
                                                                cout<<
"\nElement is not found in the list\n";
                                                list.currentNode
= savedCurrentNode;
                                }
        bool next(){
            if
(currentNode==NULL) return false;
lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
            if
(currentNode==NULL || size==0)
            return
false;
            else
                return
true;
        }
        int get(){
            if
(currentNode != NULL)
            return
currentNode->get();
        }
        void start() {
                lastCurrentNode
= headNode;
                currentNode
= headNode;
                                }       
};
link_list.cpp:
#include<iostream>
using namespace std;
#include "list.cpp"
main(){
                //Created
a list object.
                List
list;
                int x ;
                //Adding
values to the list
                cout<<
"Added numbers to the list: \n";
                list.add(5);
                list.add(13);
                list.add(4);
                list.add(8);
                list.add(24);
                list.add(48);
                list.add(12);
                //Calling
the start method of the list 
                list.start();
                //Printing
all the element of the list
                while(list.next())
                                cout<<"List
Element:"<<list.get()<<endl;
                cout<<"_____________________\n";
                cout<<
"Removed First three elements from the list after that\n";           
                list.remove();
                list.remove();
                list.remove();
                list.start();
                while(list.next())
                                cout<<"List
Element:"<<list.get()<<endl;
                cout<<"_____________________\n";
                cout<<
"Enter the number to be searched: " ;
                cin>>x
;
                list.start();
                list.find(list,
x );
                system("pause");
}
Output images are:
Output images are:




















