Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

PROJECT FOUR

COMPUTER SCIENCE – II

EXERCISING A DOUBLY-LINKED LIST


CLASS

Submitted by: SUNDAR BOHORA


Instructor: Dr. Bryant Julstrom
Date due: September 29, 2020
Date submitted: September 28, 2020
Section: 03
INTRODUCTION:

A sequence of elements of same type that is ordered by the elements’ values is called
an ordered list. One of the most popular way to represent an ordered list is in a linked
list. linked list is a type of data structure that is made up of nodes that are created using
self-referential structures. Each of these nodes contain two parts, namely the data and
the reference to the next list node. Only the reference to the first list node is required to
access the whole linked list. This is known as the head. The last node in the list points to
nothing so it stores NULL in that part. Similarly, the sequence of unique elements that
stores them in no particular order is called unordered list. Each element holds a relative
position with respect to the others. Some of the examples of unordered list are; List(),
add(item), remove(item), search(), etc. Doubly linked list is a type of data structure that
is made up of nodes that are created using self referential structures. Each of these nodes
contain three parts, namely the data and the reference to the next list node and the
reference to the previous list node.

In this project we will be writing a class that implements an unordered list abstract
data types using a doubly- linked list with pointers to both its first and last nodes.
Furthermore, we will be specifying a client program that uses a class.

DESCRIPTION:

Firstly, the program will have a class that implements an unordered list ADT which
provides major operations. Default constructor initializes a newly declared list to be
empty, destructor deletes all the nodes in a list, empty() reports if the list that invokes it is
empty, append(entry) appends an entry at the end of the list, remove_last() removes the
last item from the list.
Secondly, for line editing problem or second part,, our program reads characters
one at a time and prints them back to the terminal in order. As per the instruction by the
question, ‘#’ indicates that the program will delete the most recent character. As a result,
the surviving characters are printed in a forward order.

Data Structure:

We have used various data structures in this project like character (char). Character keeps
track of all the character value in the program. When the function is called from the main they
return character value in a program. Apart from this we used if/else statement, while loop,
switch statement, unordered class list, class used were both public and private.

Function and main program:

We used various function and data structure in this project like typedef char Item , class
unorderedList, the class has functions like default constructor, destructor, empty(),
append(entry) and remove_last()whose operations were mentioned in description.

Code:
*/ Sundar Bohora

CSCI 301

Project 04 */

#include<iostream>

using namespace std;

typedef char Item; //Declaring Item as type of char

class unorderedList // unorderedList implements as ADT


{

private:

     // Definition of Node as struct type

     struct Node

     {

          Item data;

          Node *next;

         Node *prev;

     };

     Node *first;      //Declare first as type of Node *

     Node *last;      // Declare last as type of Node *

public:

     unorderedList();      //Declaration of default constructor

     ~unorderedList(); //Declaration of destructor

     bool empty();      //Declaration of empty function

     void append(Item entry);      //Declaration of append function

     void remove_last();      //Declaration of remove_last function

     // Declare the operator << overaload as friend function

     friend ostream& operator<<(ostream &opout, const unorderedList &ult);

};

unorderedList::unorderedList() // Implememtation of default constructor

     first = NULL;      //assign NULL to first

     last = NULL;      //assign NULL to last


}

unorderedList::~unorderedList() // Implementation of destructor

     Node *resultValue;      //Declare resultValue as type of Node *

     for (Node *other = first; other != NULL; other = resultValue)      //Iterate the loop

     {

          resultValue = other->next;

          delete other;

     }

     delete first;

     delete last;

bool unorderedList::empty() // Implementation of empty function which returns bool value

     if (first != NULL)

     {

          return false;

     }

     return true;

//Implementation of append function

//which accepts the parameter entry

// as type of Item

void unorderedList::append(Item entry)

     // check if list is empty or not

     if (empty())

     {
          //Initialize the Node class

          first = new Node();

          //assign entry to first->data

          first->data = entry;

          //assign first to last

          last = first;

          return;

     }

     //Initialize the Node class

     last->next = new Node();

     // assign entry to last->next->data

     last->next->data = entry;

     //assign last to last->next->prev

     last->next->prev = last;

     // assign last->next to last

     last = last->next;

// Implementation of remove_last function

void unorderedList::remove_last()

     // check list is empty or not

     if (empty())

     {

          return;

     }

     // check first is equal to last

     if (first == last)


     {

          first = NULL;

          delete last;

          last = NULL;

          return;

     }

     // assign last to other as type of Node *

     Node *other = last;

     //assign other->prev to last

     last = other->prev;

     //assign NULL to last->next

     last->next = NULL;

     delete other;

//Implementation of operator << with parameters &opout,&ult

ostream& operator<< (ostream &opout, const unorderedList &ult)

     //Iterate the loop

     for (unorderedList::Node *other = ult.first; other != NULL; other = other->next)

     {

          // Display statement same as cout

          opout << other->data;

     }

     //return opout

     return opout;

//Implementation of main function


int main()

     unorderedList unObject;      // Create and object for unorderedList class

     Item ch;

cout << "Enter a line of characters; # => delete the most recent character." << endl;

cout << " ->";  

   // Iterate the loop

     while (ch = cin.get())

     {

          //check ch is equal to '\n' new line character then

          //break the loop

          if (ch == '\n')

          {

              //break the loop

              break;

          }

          // check if ch is equal to # then remove previous character #

          if (ch == '#')

          {

              //call remove_last function with unorderedList classs object

              unObject.remove_last();

          }

          //check if ch is not equal to #

          //then add new character to unorderedList class object

          else if(ch!='#')

          {    //call append function withunorderedList classs object


              unObject.append(ch);

          }        

     }

     cout << unObject << endl;

     return 0;

Test
After compilation and running the program we perform the sample test and many other test and
checks if our program is covering all the requirements or not. And after multiple tests we saved
few tests which are as follow:

Test1

Test 2
Test 3

User Document:
We compiled the program using the command g++.project3.cpp . At first it showed many
compilation error because there were many type error, functions not declared properly and
curly brackets missing at intervals. This might be because this program is pretty lengthy in code
and bit difficult that last project. But, it was fun when it run properly after giving the
command ./a.out. The program showed the information and asked an input from the user.

Summary:
Using necessary guidelines from the instruction of the professor and other resources, we
created a program that implements an unordered list type using a doubly-linked list, and
exercises and tests the class's list implementation. Many functions, variables, loops were used to
make the program effective.

References:
I used various resources for the projects which include classroom notes,
coursebook and few academic websites.
https://www.thecrazyprogrammer.com/2015/09/doubly-linked-list-in-c-and-cpp.html
https://www.geeksforgeeks.org/doubly-linked-list/
https://www.youtube.com/watch?v=m7kEq9J4WVg
https://www.tutorialspoint.com/cplusplus-program-to-implement-doubly-linked-list#:~:text=C%2B%2B
%20Programming%20Server%20Side%20Programming.%20Doubly%20linked%20list,is%20required
%20to%20access%20the%20whole%20linked%20list.
https://www.codesdope.com/blog/article/c-linked-lists-in-c-singly-linked-list/
https://www.tutorialspoint.com/cplusplus-program-to-implement-singly-linked-list
https://www.revisitclass.com/c/create-a-linked-list-in-c-with-examples/
https://www.softwaretestinghelp.com/doubly-linked-list-2/
https://www.includehelp.com/code-snippets/linked-list-implementation-using-cpp-program.aspx

You might also like