CS301 Assignment 1 Solution and Discussion


  • Cyberian's Gold

    Assignment No. 01
    Semester Fall 2019
    CS301- Data Structures Total Marks: 20

    Due Date : 18-Nov-2019
    Instructions
    Please read the following instructions carefully before solving & submitting assignment:
    It should be clear that your assignment will not get any credit (zero marks) if:
    o The assignment is submitted after due date.
    o The submitted code does NOT compile.
    o The submitted assignment is other than .CPP file.
    o The submitted assignment does NOT open or file is corrupted.
    o The assignment is copied (from other student or ditto copy from handouts or internet).
    Uploading instructions
    For clarity and simplicity, You are required to Upload/Submit only ONE .CPP file.

    Note: Use ONLY Dev-C++ IDE.
    Objective
    The objective of this assignment is

    o To make you familiar with implementing the Doubly Linked List data structure in C++ programming language.

    For any query about the assignment, contact at [email protected]

    GOOD LUCK

    Marks: 20
    Problem Statement:

    You have to implement a Double Linked List in C++ language in which you have to create a structure and two classes as given below:

    1. A structure named as “StudentDetail” by using struct keyword.
      StudentDetail structure should consist of following data members of type “string”.
      name
      vuid

    2. A node class named as “Node” by using class keyword.
      Node class will have following data members and member functions:
      cda47932-36ec-4210-ab0a-b3bee8bce8dc-image.png

       (“X” denotes StudentDetail variable means the student details and “Y” denotes the data value of the node).
      
    3. A doubly linked list class named as “DoublyLinkedList” by using class keyword.
      DoublyLinkedList class will have following data members and member functions:
      a46ffa50-421f-4ab2-9d20-67bfac975435-image.png

    Detailed description of member functions of DoublyLinkList class:

    1. addAtBegining(X): To add the node at the beginning of the doubly linked list class.
      4da1ea58-b161-48f7-98b0-9684d220156b-image.png

    2. addAtEnd(X): To add the node at the End of the doubly linked list class.
      08b97a59-7a5e-4312-9d83-5cdb81890a8f-image.png

    3. delNode(): To delete a node pointed by current pointer in the doubly linked list.
      59d9f4dd-919a-4db9-991a-950c4d6bb829-image.png

    4. display():To display all student details in doubly linked list.

            (“X” denote Node to add in the doubly linked list).
      

    In the main ( ) function you have to do the following:

    1. Create two objects of type StudentDetail at the Beginning of the doubly linked list class using addATBegining (X) method of DoublyLinkedList and use display() method to display doubly linked list.
      Note that first object should contain your own VU ID and your own name otherwise you will get zero marks.

    2. Create one objects of type StudentDetail at the End of the doubly linked list class using addAtEnd (X) method of DoublyLinkedList and use display() method to display doubly linked list.

    3. Delete current node of the doubly linked list and use display() method to display doubly linked list.

    4. Display doubly linked list after calling each functions.

    Detailed Output Screenshot:
    fcd2ba41-d1ae-4c73-9648-8180c609cba1-image.png

    Lectures Covered: This assignment covers Lecture # 1-8.
    Deadline: Your assignment must be uploaded/submitted at or before 18-Nov-2019.


  • Cyberian's Gold

    100 % Solved:

    // Include header files
    #include<iostream>
    #include<string.h>
    #include<conio.h>
    #include<stdlib.h>
    using namespace std;
    
    // Create Structure of StudentDetail
    struct StudentDetail{
    	string name;
    	string vuid;
    };
    
    // Create a Node class
    
    class Node{
    
    // Data Members of Node class
    private:
    	StudentDetail newStd;
    	Node *prev;
    	Node *next;
    
    // Member Functions of Node class
    public:
    	
    	// Set and get data
    	void set(StudentDetail newStd){
    		this->newStd = newStd;
    	}
    	
    	StudentDetail get(){
    		return newStd;
    	}
    	
    	// Set and get next node pointer
    	void setNext(Node *nextPtr){
    		next = nextPtr;
    	}
    	
    	Node* getNext(){
    		return next;
    	}
    	
    	// set and get previous node pointer
    	void setPrev(Node *prevPtr){
    		prev = prevPtr;
    	}
    	
    	Node* getPrev(){
    		return prev;
    	}
    };
    // Create a DoublyLinkedList class 
    
    class DoublyLinkedList{
    // Data Members of DoublyLinkedList class
    private:
    	
    	Node *headPtr;
    	Node *curPtr;
    	int size;
    // Member Functions of DoublyLinkedList class
    public:
    	
    	// Constructor of DoublyLinkedList class
    	DoublyLinkedList(){
    		headPtr=new Node;
    		headPtr->setNext(NULL);
    		headPtr->setPrev(NULL);
    		curPtr=NULL;
    		size = 0;
    	}
    	// Add node at the begining of DoublyLinkedList class
    	
    	void addAtBegining(Node *newNode){
    		
    		if(headPtr->getNext()==NULL){
    			newNode->setNext(headPtr->getNext());
    			headPtr->setNext(newNode);
    			newNode->setPrev(headPtr);
    			curPtr = newNode; 
    		}
    		else{
    			
    			newNode->setNext(headPtr->getNext());
    			Node *tempNode = headPtr->getNext();
    			tempNode->setPrev(newNode);
    			headPtr->setNext(newNode);
    			newNode->setPrev(headPtr);
    		}
    		size++;
    	}
    	
    	// Add node at the end of DoublyLinkedList Class
    	void addAtEnd(Node *newNode){
    		
    		if(curPtr==NULL){
    			newNode->setNext(headPtr->getNext());
    			headPtr->setNext(newNode);
    			newNode->setPrev(headPtr);
    			curPtr = newNode;
    		}
    		else{
    			newNode->setNext(curPtr->getNext());
    			curPtr->setNext(newNode);
    			newNode->setPrev(curPtr);
    			curPtr = newNode;
    		}
    		
    		size++;
    	}
    	// Deleting the current node
    	void delNode(){
    		Node *tempCur = curPtr;
    		
    		if(curPtr==NULL){
    			cout<< "List is Empty";
    		}
    		else{
    			
    			tempCur = curPtr->getPrev();
    			tempCur->setNext(curPtr->getNext());
    			delete curPtr;
    			curPtr = tempCur;
    			size--;
    		}
    		
    		
    	}
    	
    	// Display method for DoublyLinkedList nodes
    	
    	void display(){
    		
    		Node *tempCur = headPtr->getNext();
    		
    		for(int i=0; i<size; i++){
    			cout<<tempCur->get().name <<" "<<tempCur->get().vuid<< "--->";
    			tempCur = tempCur->getNext();
    		}
    	}
    	
    	
    };
    
    int main(){
    	// Initializing Student data in structure variables
    	StudentDetail  r1={"BC123457896","Ali"};
    	StudentDetail  r2={"BC123458967","Basit"};
        StudentDetail  r3={"BC123458963","Agha"};
    
    	
    	// Create new nodes 
    	Node *newNode[3] ;
    	newNode[0] = new Node();
    	newNode[1] = new Node();
    	newNode[2] = new Node();
    
    	newNode[0]->set(r1);
    	newNode[1]->set(r2);
    	newNode[2]->set(r3);
    
    
    	// Create an object of DoublyLinkedList and calling the required methods
    	DoublyLinkedList *dll = new DoublyLinkedList();
    	cout<<"\n";
    	cout<<"  Add Your VUid and Name at First Position\n";
    	cout<<"  -----------------------------------------------\n"<<endl;
    	dll->addAtBegining(newNode[0]);  // adding 1st node at start
    	cout<<"  ";
    	dll->display();
    	cout << endl ;
    	
    	cout<<"\n\n\n  Insertion at Beginning in Doubly Linked List\n";
    	cout<<"  -----------------------------------------------\n"<<endl;
    	dll->addAtBegining(newNode[1]); // adding 2nd node at start 
    	cout<<"  ";
    	dll->display();
    	cout << endl  ;
    
    	cout<<"\n\n\n  Insertion at End in Doubly Linked List\n";
    	cout<<"  -----------------------------------------------"<<endl;
    	cout<<"\n";
    	dll->addAtEnd(newNode[2]); // adding 3rd node at end
    	cout<<"  ";
    	dll->display();
    	cout << endl  ;
    	
    	// Deleting current (last) node at the end
    	cout<<"\n\n\n  Deltion of current node (Last Node)\n";
    	cout<<"  -----------------------------------------------\n"<<endl;
    	dll->delNode();
    	cout<<"  ";
    	dll->display();
    
    	cout<<"\n\n\n\n";
    	system("pause");
    	return 1;
    }
    
    

  • Cyberian's Gold

    .CPP File Code

    using namespace std;
     
    #include <stdlib.h>
    #include <iostream>
     
    struct StudentDetail{
         
        string name;
        string vuid;
         
         
    };
     
    //class Node* head;
     
     
    class Node{
         
        struct StudentDetail newStd;
        class Node* next;
        class Node* prev;
         
        void Set(string name,string vuid)
        {
            newStd.name=name;
            newStd.vuid=vuid;
             
             
        }
         
        Node Get()
        {
                 
        }
         
        void setNext(string name,string vuid){
             
            if(next==NULL)
            {
                 
            }
            else
            {
                class Node* newNode= new Node();
                 
                    newStd.name=name;
                    newStd.vuid=vuid;
                    newNode->next= newNode;
             
                 
                 
        }   }
         
        string getNext()
        {
            next;
             
        }
         
            void setPrev(string name,string vuid){
             
            if(next==NULL)
            {
                 
            }
            else
            {
                class Node* newNode= new Node();
                 
                    newStd.name=name;
                    newStd.vuid=vuid;
                    newNode->next= newNode;
             
                 
                 
        }   }
         
            string getPrev()
        {
            prev;
             
        }
         
         
    };
     
     
     
    class DoublyLinkedList{
        public:
        struct StudentDetail newStd;
    class DoublyLinkedList* headPtr;
    class DoublyLinkedList* curPtr;
    class DoublyLinkedList* nextPtr;
    int size;
    //dfdf
     
    class DoublyLinkedList* headDlinkList=NULL;
     
    void addAtBegining(string vuid, string name)
    {
    class DoublyLinkedList* dNode= new DoublyLinkedList();
     
        dNode->newStd.vuid=vuid;
        dNode->newStd.name=name;
        dNode->nextPtr=headDlinkList;
        headDlinkList= dNode;
        dNode->curPtr=dNode;
         
         
         
        }
         
    void addAtEnd(string vuid, string name)
    {
     
        class DoublyLinkedList* dNode= new DoublyLinkedList();
     
        dNode->newStd.vuid=vuid;
        dNode->newStd.name=name;
        dNode->nextPtr=headDlinkList;
        headDlinkList= dNode;
            dNode->curPtr=dNode;
         
        }   
         
         
     void delNode()
     {
        class DoublyLinkedList* temp1=curPtr;
        class DoublyLinkedList* temp2= temp1;
        temp1->nextPtr= temp2->nextPtr;
         
        free(temp2);
         
         
         
         
         
     }
         
         
         
         
        void print()
        {
         
       class DoublyLinkedList* temp= headDlinkList;
        
       while(temp!=NULL)
       {
     cout<<temp->newStd.vuid<<"      "<<temp->newStd.name<<endl;  
        temp= temp->nextPtr;
             
    }
             
        }
     
     
     
     
         
    };
     
     
     
     
    int main()
    {
        string vuid,name;
         
    cout<<"Add your vuID and Name at First Position "<<endl;
    cout<<"_ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _"<<endl;
     
        DoublyLinkedList dlist1;
     
         
        cin>>vuid;
        cin>>name;
         
        dlist1.addAtBegining(vuid,name);
        dlist1.print();
         
        cout<<"Insertion At Beginning in doubly Link List "<<endl;
        cout<<"_ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _"<<endl;
            cin>>vuid;
        cin>>name;
        dlist1.addAtBegining(vuid,name);
    dlist1.print(); 
     
            cout<<"Insertion At End in doubly Link List "<<endl;
        cout<<"_ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _"<<endl;
        cin>>vuid;
        cin>>name;
            dlist1.addAtEnd(vuid,name);
    dlist1.print(); 
            cout<<"Deletion of Current Node (Last Node) "<<endl;
        cout<<"_ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _"<<endl;
        dlist1.delNode();
        dlist1.print();
         
    }
    

    Download .cpp File


  • Cyberian's Gold

    100 % Solved:

    // Include header files
    #include<iostream>
    #include<string.h>
    #include<conio.h>
    #include<stdlib.h>
    using namespace std;
    
    // Create Structure of StudentDetail
    struct StudentDetail{
    	string name;
    	string vuid;
    };
    
    // Create a Node class
    
    class Node{
    
    // Data Members of Node class
    private:
    	StudentDetail newStd;
    	Node *prev;
    	Node *next;
    
    // Member Functions of Node class
    public:
    	
    	// Set and get data
    	void set(StudentDetail newStd){
    		this->newStd = newStd;
    	}
    	
    	StudentDetail get(){
    		return newStd;
    	}
    	
    	// Set and get next node pointer
    	void setNext(Node *nextPtr){
    		next = nextPtr;
    	}
    	
    	Node* getNext(){
    		return next;
    	}
    	
    	// set and get previous node pointer
    	void setPrev(Node *prevPtr){
    		prev = prevPtr;
    	}
    	
    	Node* getPrev(){
    		return prev;
    	}
    };
    // Create a DoublyLinkedList class 
    
    class DoublyLinkedList{
    // Data Members of DoublyLinkedList class
    private:
    	
    	Node *headPtr;
    	Node *curPtr;
    	int size;
    // Member Functions of DoublyLinkedList class
    public:
    	
    	// Constructor of DoublyLinkedList class
    	DoublyLinkedList(){
    		headPtr=new Node;
    		headPtr->setNext(NULL);
    		headPtr->setPrev(NULL);
    		curPtr=NULL;
    		size = 0;
    	}
    	// Add node at the begining of DoublyLinkedList class
    	
    	void addAtBegining(Node *newNode){
    		
    		if(headPtr->getNext()==NULL){
    			newNode->setNext(headPtr->getNext());
    			headPtr->setNext(newNode);
    			newNode->setPrev(headPtr);
    			curPtr = newNode; 
    		}
    		else{
    			
    			newNode->setNext(headPtr->getNext());
    			Node *tempNode = headPtr->getNext();
    			tempNode->setPrev(newNode);
    			headPtr->setNext(newNode);
    			newNode->setPrev(headPtr);
    		}
    		size++;
    	}
    	
    	// Add node at the end of DoublyLinkedList Class
    	void addAtEnd(Node *newNode){
    		
    		if(curPtr==NULL){
    			newNode->setNext(headPtr->getNext());
    			headPtr->setNext(newNode);
    			newNode->setPrev(headPtr);
    			curPtr = newNode;
    		}
    		else{
    			newNode->setNext(curPtr->getNext());
    			curPtr->setNext(newNode);
    			newNode->setPrev(curPtr);
    			curPtr = newNode;
    		}
    		
    		size++;
    	}
    	// Deleting the current node
    	void delNode(){
    		Node *tempCur = curPtr;
    		
    		if(curPtr==NULL){
    			cout<< "List is Empty";
    		}
    		else{
    			
    			tempCur = curPtr->getPrev();
    			tempCur->setNext(curPtr->getNext());
    			delete curPtr;
    			curPtr = tempCur;
    			size--;
    		}
    		
    		
    	}
    	
    	// Display method for DoublyLinkedList nodes
    	
    	void display(){
    		
    		Node *tempCur = headPtr->getNext();
    		
    		for(int i=0; i<size; i++){
    			cout<<tempCur->get().name <<" "<<tempCur->get().vuid<< "--->";
    			tempCur = tempCur->getNext();
    		}
    	}
    	
    	
    };
    
    int main(){
    	// Initializing Student data in structure variables
    	StudentDetail  r1={"BC123457896","Ali"};
    	StudentDetail  r2={"BC123458967","Basit"};
        StudentDetail  r3={"BC123458963","Agha"};
    
    	
    	// Create new nodes 
    	Node *newNode[3] ;
    	newNode[0] = new Node();
    	newNode[1] = new Node();
    	newNode[2] = new Node();
    
    	newNode[0]->set(r1);
    	newNode[1]->set(r2);
    	newNode[2]->set(r3);
    
    
    	// Create an object of DoublyLinkedList and calling the required methods
    	DoublyLinkedList *dll = new DoublyLinkedList();
    	cout<<"\n";
    	cout<<"  Add Your VUid and Name at First Position\n";
    	cout<<"  -----------------------------------------------\n"<<endl;
    	dll->addAtBegining(newNode[0]);  // adding 1st node at start
    	cout<<"  ";
    	dll->display();
    	cout << endl ;
    	
    	cout<<"\n\n\n  Insertion at Beginning in Doubly Linked List\n";
    	cout<<"  -----------------------------------------------\n"<<endl;
    	dll->addAtBegining(newNode[1]); // adding 2nd node at start 
    	cout<<"  ";
    	dll->display();
    	cout << endl  ;
    
    	cout<<"\n\n\n  Insertion at End in Doubly Linked List\n";
    	cout<<"  -----------------------------------------------"<<endl;
    	cout<<"\n";
    	dll->addAtEnd(newNode[2]); // adding 3rd node at end
    	cout<<"  ";
    	dll->display();
    	cout << endl  ;
    	
    	// Deleting current (last) node at the end
    	cout<<"\n\n\n  Deltion of current node (Last Node)\n";
    	cout<<"  -----------------------------------------------\n"<<endl;
    	dll->delNode();
    	cout<<"  ";
    	dll->display();
    
    	cout<<"\n\n\n\n";
    	system("pause");
    	return 1;
    }
    
    


Quiz 100% Result Quiz 100% Result
| |