Question

In: Computer Science

Problem Description Objective This practical will test your capability of implementing a linked list. Design Think...

Problem Description

Objective

This practical will test your capability of implementing a linked list.

Design

Think of how you are going to solve the problem and test your implementation with the test cases you designed based on the stages below.

Testing Hint: it’s easier if you test things as a small piece of code, rather than building a giant lump of code that doesn’t compile or run correctly. As part of your design, you should also sketch out how you are going to build and test the code.

Problem

Linked lists are dynamic data structures that allow storing a list of items in separate places in the memory. Each item of the list, together with the address of the next item, is placed in one object of type Node (find the description below). The last node of the list should include the last item and a null pointer (NULL or nullptr).

Lists, as abstract data types, can be implemented by different structures, including arrays, vectors and linked lists. Please do not use arrays or vectors for this practical. In this practical, you should write the code for two classes named LinkedList, and Node.

You must include separate header and implementation files for both classes. The class Node should consist of two member variables, an integer data and a pointer to Node next, and 5 functions, a constructor, and getter and setter for the two member variables.

The class LinkedList must have only one member variable: head, which is of type pointer to Node. If the list is empty, head should contain NULL or nullptr. It should also have at least the following member functions. Note that add functions should construct nodes from the heap, and delete functions should manually delete the one that is removed from the list. Don’t forget to write test cases for each function and ensure that the tests pass before progressing to the next function.

  • void addFront(int newItem) : The function inserts a new node, containing the newItem, at the beginning of the list.
  • void addEnd(int newItem) : The function inserts a new node, containing the newItem, at the end of the list.
  • void addAtPosition(int position, int newItem) : The function inserts a new node, containing the newItem, such that it is the position-th member of the list. i.e. we assume the first element of the list is in position 1. If position is larger than the size of the list, the new item is added to the end of the list. If position < 1, the new item is added at the beginning of the list.
  • int search(int item) : The function searches the list for the first instance of the item, and if found, both prints the position of the of the item (followed by a space) and returns the position of the item in the list (positions start from 1). If not found, both prints 0 (followed by a space) and returns 0. Note that the returning type is different from what was explained in the search function in the lecture.
  • void deleteFront() : The function deletes the first element of the list.
  • void deleteEnd() : The function deletes the last element of the list.
  • void deletePosition(int position) : The function deletes the element at the given position of the list. If the position < 1 or it is larger than the size of the list, only print ”outside range”.
  • int getItem(int position) : The function both prints the value of the item (followed by a space) and returns the value of the item at the given position of the list, If beyond the size of the array, both prints std::numeric_limits < int >::max() (followed by a space) and returns std::numeric_limits< int >::max(). You should include <limits> for this. Take a look at
    http://www.cplusplus.com/reference/limits/numeric_limits/ if you need.
  • void printItems() : The function prints the value of the items of the list from head to tail. In case of an empty list, it does not print anything
  • A constructor with no parameters, which makes an empty list.
  • A constructor that takes an array of integers and makes a linked list, containing all the elements of the array, in the same order. As the second parameter, it takes the size of the array.
  • A destructor that manually deletes all the elements that are still in the list.

Note that the printing in the functions search and getItem is for the purpose of easy testing.

Main function

The test script will compile your code using

g++ -o main.out -std=c++11 -O2 -Wall *.cpp

  

It is your responsibility to ensure that your code compiles on the university system. g++ has too many versions, so being able to compile on your laptop does not guarantee that it compiles on the university system. You are encouraged to debug your code on a lab computer (or use SSH).

You are asked to create a main function (main.cpp). It takes in one line of input.
  

int1 int2 ... intn FUNCTIONINITIAL param1 param2

int1, until intn are integers, separated by space, which should be placed in an integer array, and passed to the linked list constructor. For simplicity, we assume that the size of this array never exceeds 100; therefore, you can take an static array with the size of 100.

After the elements of the list, the input consists of an string, denoting a function, followed by its parameters. The string is one of these:

  • AF standing for addFront
  • AE standing for addEnd
  • AP standing for addAtPosition
  • S standing for search
  • DF standing for deleteFront
  • DE standing for deleteEnd
  • DP standing for deletePosition
  • GI standing for getItem

Call the indicated function with its parameter. If the function has only one parameter, then the last integer value from the input is not used. At the end, call the function printItems, to produce the required output.

  

Sample input: 5 2 7 10 AP 3 9

expected output: 5 2 9 7 10

Sample input: 3 4 2 1 DP 3 0

expected output: 3 4 1

Sample input: 45 20 2 10 GI 3 0

expected output: 2 45 20 2 10

Solutions

Expert Solution

Ans for the given problem

/*Simple program to perform linklist Operations*/
#include<iostream>
#include<stdio.h>
#define MAXSIZE 5
#define NULL0

/*Function declarations*/

void create();
void addFront();
void addAtPosition();
void addEnd();
void deleteFront();
void deleteEnd();
void deletePosition();
void display();


struct node
{
int num;
struct node*ptr;
};

typedef struct node NODE;
NODE*start =NULL;

int main()
{
int ch;
char option;
do
{
std::cout<<"Enter the Option\n";
std::cout<<"----------------------------------\n";
std::cout<<"1.Create the Linked list\n";
std::cout<<"2.Insertion at Begining of the Linked list\n";
std::cout<<"3.Insertion at End of the Linked List\n";
std::cout<<"4.Insert at specified Postion\n";
std::cout<<"5.Deletion at first\n";
std::cout<<"6.Deletion at End\n";
std::cout<<"7.Deletion at specified Postion\n";

std::cout<<"-----------------------------------\n";
std::cin>>ch;


switch(ch)
{
case 1: create();
break;
case 2:addFront();
break;
case 3: addEnd();
break;
case 4: addAtPosition();
break;
case 5: deleteFront();
break;
case 6: deleteEnd();
break;
case 7: deletePosition();
default: std::cout<<"Invaild Input";
}
std::cout<<"\nDo you wish to continue(y/n)";
std::cin>>option;

}while(option!='0');

}

void create()
{
NODE*newnode;
newnode = (NODE*)malloc(sizeof(NODE));
std::cout<<"Enter the Value :\t";
std::cin>>newnode->num;
newnode->ptr =NULL;
if(start==NULL)
{
start=newnode;
}
else
{
newnode->ptr =start;
start=newnode;
}


display();
return;
}
void addFront()
{
NODE*newnode;
newnode=(NODE*)malloc(sizeof (NODE));
std::cout<<"Enter the Value to be inserted at the Begining of Linked list";
std::cin>>newnode->num;
newnode->ptr=start;
start=newnode;
display();
return;
}
/* function to insert at specified postion*/

void addAtPosition()
{
NODE*newnode, *temp;
int k,pos;
newnode=(NODE*)malloc(sizeof (NODE));
newnode->ptr=NULL;
std::cout<<"Enter the Value to be inserted at Specfic Postion";
std::cin>>newnode->num;
std::cout<<"Enter the position";
std::cin>>pos;
temp=start;
if(start==NULL)
{
start= newnode;
}
else if(pos==1)
{
newnode->ptr=start;
start=newnode;
}
else
{
for(k=0;k<pos-1;k++)
{
temp=temp->ptr;
if(temp==NULL)
{
std::cout<<"Node in the list are less than count";
}
}
newnode->ptr =temp->ptr;
temp->ptr=newnode;
}

display();
return;
}

void addEnd()
{
NODE*newnode,*temp;
newnode=(NODE*)malloc(sizeof (NODE));
if(newnode==NULL)
{
std::cout<<"Overflow\n";
return;
}
else
{
std::cout<<"Enter the Value for the new node";
std::cin>>newnode->num;
}
newnode->ptr=NULL;
if(start==NULL)
{
start=newnode;
}
else
{
temp=start;
while(temp->ptr!=NULL)
{
temp=temp->ptr;
}
temp->ptr=newnode;
}

display();
return;
}

void deleteFront()
{
NODE*temp;
if(start==NULL)
{
std::cout<<"Deletion not possible";
return;
}
else
{
temp=start;
start=start->ptr;

std::cout<<"The Deleted is"<<temp->num;
free(temp);
}
display();
return;

}

void deleteEnd()
{
NODE temp1, temp2;
if (start==NULL)
{
std::cout<<"Deletion not Possible";
return;
}
else
{
temp1=NULL;
temp2=start;
while(temp2->ptr!=NULL)
{
temp1=temp2;
temp2=temp2->ptr;
}
if(temp1!=NULL)
{
temp1->ptr=NULL;
}
else
{
start=NULL;
}

std::cout<<"The Element deleted is"<< temp2->num;
free(temp2);
}
display();
return;
}

void deletePosition()
{
NODE*temp1,*temp2;
int k,pos,item;
if (start==NULL)
{
std::cout<<"Deletion not Possible\n";
return;
}
std::cout<<"Enter the position to be deleted";
std::cin>>pos;
temp1=NULL;
temp2=start;
if(pos==1)
{
temp1=start;
start=start->ptr;
}
else
{
for(k=1;k<pos;k++)
{
temp1=temp2;
temp2=temp2->ptr;
}
temp1->ptr=temp2->ptr;

std::cout<<"The Element deleted is %d", temp2->num;
free(temp2);

}
display();
return;
}

void display()
{
NODE*temp;
temp=start;
std::cout<<"\nCurrent Linked List:\t";
while(temp->ptr!=NULL)
{
std::cout<<temp->num<<"\t";
temp=temp->ptr;
}
std::cout<<temp->num<<"\t";
return;

}

Output:

****************************Thank You***************************


Related Solutions

Problem Description Objective This practical will test your knowledge on sorting and searching algorithms. In this...
Problem Description Objective This practical will test your knowledge on sorting and searching algorithms. In this practical, you will be implementing a number of algorithms. Each of these algorithms will be constructed in a different class. You should make sure that you know the complexity of the algorithms you implement. Design Think of how you are going to solve the problem and test your implementation with the test cases you designed based on the stages below. Testing Hint: it’s easier...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list for employees, the data stored is ·An employee number (a positive integer) ·A yearly salary (a float). ·Number of dependents (a short positive integer) The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h)...
C++ What Should This Program Do? Linked List Class Design your own linked list class (List.h) to hold a series of strings. The linked list node should be implemented as a struct. The class should have member functions for appending, inserting, and deleting nodes. You should also have a display function that will traverse the list & display each node’s value. Don’t forget to add a destructor that destroys the list. DRIVER – driver.cpp Write a driver program (driver.cpp) that...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
The aim of this lab session is to make you familiar with implementing a linked list....
The aim of this lab session is to make you familiar with implementing a linked list. In this lab you will complete the partially implemented LinkedIntegerList. Specifically, you will implement the following methods. • public boolean remove(int value) • public int removeAt(int index) • public boolean set(int index, int value) • public int indexOf(int value) Task-1 Add a new class named Lab3Tester class to test the current implementation of the linked list. (Note: you may reuse the code from SimpleIntegerListTester...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT