PLEASE Follow the direction and watch the jing video given on the bottom! Be sur
ID: 3706410 • Letter: P
Question
PLEASE Follow the direction and watch the jing video given on the bottom! Be sure to make the menu like the video that is given. Using C++ on code blocks for data structures. Also for the high level validation and low level, According to my instructor:
-So, before you call a mutator function, you need to confirm that you are passing it a character that is a capital letter. (Use a while loop to validate, etc...)
-And down inside of the mutator function, you need to make sure you have not received a character that is not a capital letter. If you find it then do an exit_failure.
Prompt:
Using the material in the textbook (NumberList) as a sample, design your own linked list class to hold a series of capital letters. The class should have the following member functions:
append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges the nodes in the list so that their order is reversed), and search (which returns the position of a specific value in the list. Assume the first node is position 0. If the items is not found, then return a -1 which will tell the calling function that that value does not exist)
High level validation
Character data input from the user should be validated for being a capital letter (A, B, C, ....).
Low level validation
1. Mutator functions which are given a node value that is not a capital letter should have an exit_failure.
2. Mutator functions which are told to access a node that doesn't exist should return a -1 so that the calling function can handle the problem. (No exit_failure used in this case b/c the calling function can't know if the node is there or not.)
Create a menu-driven program to demonstrate your linked list capabilities. Make sure that you have proper constructors and destructors (a destructor must delete every node). Make your input and output professional. Break your code down into functions so as to maintain modular design. No global variables.
Demo your program as follows:
append node
append node
append node
append node
append node
print list
insert at position
print list
delete at position
print list
reverse
print list
search list
---------------
Here's a jing from a previous semester showing what the linked list assignment has to do. It uses integers in it's list instead of capital letters, but o/w it's the same.
https://www.screencast.com/t/w4OeB05A
---------------
Submit to me:
1. all of your .cpp and .h files.
2. TWO jings: one that shows me validation and the second that just shows me the demo as requested.
Explanation / Answer
Edit: Divided the program into 3 files: main.cpp, linkedlist.cpp, linkedlist.h
Read user input from the main() function instead of handling it in linkedlist.cpp
Returned -1 when applicable, Exit from the program if data input is not an uppercase letter.
====================================================================================
linkedlist.h
-----------------------------------
//using namespace std;
/*
* Node Declaration
*/
struct node
{
char data;
struct node *next;
};
/*
* Class Declaration
*/
class single_llist
{
public:
node * start;
node * create_node(char);
int insert_pos(char, int);
void append(char);
int delete_pos(int);
int search(char);
void reverse();
void display();
bool isuppercase(char); //check whether entered char is uppercase or not
single_llist() //constructor
{
start = NULL;
}
~single_llist() //destructor
{
// free all the memory space of nodes
node *ptr = start; //ptr for iterator
while (ptr != NULL) { //iterate unitl list is empty
start = start->next;
free(ptr); // free the current node
ptr = start;
}
}
};
=====================================================================
linkedlist.cpp
-----------------------------------------------
/*
* C++ Program to Implement Singly Linked List
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include "linkedlist.h"
using namespace std;
// Check the upper case letter or not
bool single_llist::isuppercase(char data) {
//Upper case letters will have ascii range of 65-90
if (data >= 65 && data <= 90) { //If it is upper case letter.
return true;
}
return false; // letter is not an uppercase one
}
// Creating Node
node *single_llist::create_node(char value)
{
struct node *temp, *s;
if (!isuppercase(value)) //If it is not an uppercase letter
exit(EXIT_FAILURE); // exit from the program.
temp = new(struct node);
if (temp == NULL) // if memory is not allocated
{
cout << "Memory not allocated " << endl;
return temp;
}
else
{
temp->data = value;
temp->next = NULL;
return temp; //return the created node
}
}
/*
* Inserting Node at last
*/
void single_llist::append(char value)
{
struct node *temp, *s;
temp = create_node(value);
s = start;
// If list is empty, make the new node as start.
if (start == NULL)
start = temp;
else { // traverse till end and insert the new node.
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
}
cout << "Element Inserted at last" << endl;
}
/*
* Insertion of node at a given position
*/
int single_llist::insert_pos(char value, int pos)
{
int counter = 0;
struct node *temp, *s, *ptr = NULL;
temp = create_node(value);
int i;
s = start;
if (pos == 0) //If it is first position
{
// Make new node as start of the list
temp->next = start;
start = temp;
return 0;
}
else
{
while (s != NULL) { // traverse till we got the appropriate position
ptr = s;
s = s->next;
counter++;
if (pos == counter) { // if we reach the postion to insert
ptr->next = temp;
temp->next = s;
return 0;
}
}
}
return -1; // Not able to insert. So failure.
}
/*
* Delete element at a given position
*/
int single_llist::delete_pos(int pos)
{
int i, counter = 0;
if (start == NULL)
{
//cout << "List is empty so cant delete" << endl;
return -1;
}
struct node *s, *ptr = NULL;
s = start;
if (pos == 0) //If we have to delete first node
{
start = s->next; // Move the start to next node.
}
else
{
while (s != NULL) // traverse till we find the appropriate position
{
if (pos == counter) { // if we find the position
ptr->next = s->next;
free(s);
return 0;
}
ptr = s; // Holding the previous node at the time of deletion
counter++;
s = s->next;
}
return -1; // Not able to delete any node. Suitable position not found.
}
free(s); // free the node which has to be deleted.
return 0;
}
/*
* Searching an element
*/
int single_llist::search(char value)
{
int pos = -1;
bool flag = false;
if (start == NULL)
{
cout << "List is empty" << endl;
return -1;
}
struct node *s;
s = start;
while (s != NULL) // traverse the list till we reach end or till we get a match
{
pos++;
if (s->data == value) // match found
{
return pos; // return position
}
s = s->next;
}
return -1; // No match found.
}
/*
* Reverse Link List
*/
void single_llist::reverse()
{
struct node *ptr1, *ptr2, *ptr3;
if (start == NULL) // If list is empty nothing to reverse
{
cout << "List is empty" << endl;
return;
}
if (start->next == NULL) // If list has one element, no need to reverse
{
return;
}
ptr1 = start; //1st Node
ptr2 = ptr1->next; //2nd node
ptr3 = ptr2->next; //3rd node
ptr1->next = NULL; // make first node next to null.
ptr2->next = ptr1; // Reverse 1st and 2nd node
while (ptr3 != NULL) // Continue this till the end
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
start = ptr2;
}
/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout << "The List is Empty" << endl;
return;
}
temp = start;
cout << "Elements of list are: " << endl;
while (temp != NULL) // Till the last element of list
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL" << endl;
}
===============================================================
main.cpp
-------------------------------------------
#include<iostream>
#include "linkedlist.h"
#include <string>
using namespace std;
/*
* Main :contains menu
*/
int main()
{
int nodes, position, i, ret;
char choice, element;
string inputstr;
single_llist sl;
while (1)
{
cout << endl << "-------------------------------------------" << endl;
cout << endl << "Operations on character singly linked list" << endl;
cout << endl << "-------------------------------------------" << endl;
cout << "1. Append Node" << endl;
cout << "2. Insert Node at position" << endl;
cout << "3. Delete a Node at Position" << endl;
cout << "4. Print List" << endl;
cout << "5.Reverse List " << endl;
cout << "6.Search Element" << endl;
cout << "7. End Program " << endl;
cout << "Enter your choice : ";
//To avoid infinite loop, we will read as string, and the read first char
getline(cin, inputstr); // read choice as string
choice = inputstr[0]; // read first char
cout << endl;
switch (choice) // We use choice as char
{
case '1':
cout << "Appending Node: " << endl;
cout << "Enter the value to be inserted: ";
cin >> element;
getchar(); // Eat the extra enter character after reading integer input.
sl.append(element);
cout << endl;
break;
case '2':
cout << "Inserting Node at a given position:" << endl;
int pos;
cout << "Enter the value to be inserted: ";
cin >> element;
cout << "Enter the postion at which node to be inserted: ";
cin >> pos;
getchar(); // Eat enter char
ret = sl.insert_pos(element, pos);
if (ret == -1) {
cout << "Position out of bounds" << endl;
}
else
cout << "Element was inserted" << endl;
cout << endl;
break;
case '3':
cout << "Delete a node at particular position " << endl;
cout << "Enter the position of value to be deleted: ";
cin >> pos;
getchar(); // Eat enter char
ret = sl.delete_pos(pos);
if (ret == -1) {
cout << "Position out of bound" << endl;
}
else
cout << "Element Deleted " << endl;
cout << endl;
break;
case '4':
cout << "Print elements of link list" << endl;
sl.display();
cout << endl;
break;
case '5':
cout << "Reverse elements of Link List" << endl;
sl.reverse();
cout << endl;
break;
case '6':
cout << "Search element in List: " << endl;
cout << "Enter the value to be searched: ";
cin >> element;
getchar(); // Eat enter char
ret = sl.search(element);
if (ret == -1)
cout << "Element Not found in list" << endl;
else
cout << "Element Found at position: " << ret << endl;
cout << endl;
break;
case '7':
cout << "Exiting..." << endl;
sl.~single_llist(); //calling distructor to free up the linked list from memory
exit(0);
break;
default:
cout << "Wrong choice. Please enter again." << endl;
}
}
system("pause");
return 0;
}
===============================================================
Sample Output Generated:
-------------------------------------------------------
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 1
Appending Node:
Enter the value to be inserted: H
Element Inserted at last
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 1
Appending Node:
Enter the value to be inserted: E
Element Inserted at last
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 1
Appending Node:
Enter the value to be inserted: L
Element Inserted at last
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 1
Appending Node:
Enter the value to be inserted: L
Element Inserted at last
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 1
Appending Node:
Enter the value to be inserted: O
Element Inserted at last
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 4
Print elements of link list
Elements of list are:
H->E->L->L->O->NULL
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 2
Inserting Node at a given position:
Enter the value to be inserted: O
Enter the postion at which node to be inserted: 4
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 4
Print elements of link list
Elements of list are:
H->E->L->O->L->O->NULL
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 3
Delete a node at particular position
Enter the position of value to be deleted: 5
Element Deleted
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 4
Print elements of link list
Elements of list are:
H->E->L->O->O->NULL
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 5
Reverse elements of Link List
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 4
Print elements of link list
Elements of list are:
O->O->L->E->H->NULL
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice : 6
Search element in List:
Enter the value to be searched: E
Element E is found at position 4
-------------------------------------------
Operations on character singly linked list
-------------------------------------------
1. Appen Node
2. Insert Node at position
3. Delete a Node at Position
4. Print List
5.Reverse List
6.Search Element
7. End Program
Enter your choice :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.