Write a C++ program with three files for each linked list : Implement a singly l
ID: 3855368 • Letter: W
Question
Write a C++ program with three files for each linked list
: Implement a singly linked list ADT to store a collection of doubles.
Make sure to provide an interactive user interface to test these new functions in the main() for grader .
Your ADT will include the following member functions: --- a default constructor --- the "big-3": destructor, copy constructor and overloaded assignment operator
1. a member function pushFront(data) that inserts a node with data at the front of the list
2. a member function pushBack(data) that appends a node with data at the back of the list
3. a member function popFront() that removes first node of the list.
4. a member function popBack() that removes last node of the list.
5. a member function insert(index, val) that inserts a new node with value "val" at a specific position mentioned by the index argument.
6. a member function deleteDuplicates(val) that deletes a node with that number and all its copies from the list, where these copies can be located anywhere in the list.
7. a member function mtoLastElement(M) that returns Mth to the last element of a list such that when M = 0, the last element of the list is returned.
8. a member function size() that returns the size of the list.
9. an overloaded put operator (<<) to print out all the data items stored in a linked list. Note that you are recommended to overload this operator as a friend function of the LinkedList class.
10. a member function reverseList() that reverses a linked list without recreating a temporary copy of this linked list. In other words, your function CAN NOT use the 'new' operator. Here is an example, if a list contains the following data items, 3 -> 5 -> 1 -> 7; this reverse() function will change the list to 7 -> 1 -> 5 -> 3.
Explanation / Answer
Answered #1,2,3,4,5,8 and 10 points from above question
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/*
* Node Declaration
*/
struct node
{
double num;
struct node *next;
}*start,*end;
/*
* Linked list class declaration
*/
class single_llist
{
public:
node* create_node(double);
void pushFront();
void pushBack();
void insert();
void popFront();
void popBack();
int size();
void reverse();
void display();
single_llist() // Default constructor
{
start = NULL;
end = NULL;
}
};
/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<endl;
cout<<"3.Insert node at position"<<endl;
cout<<"4.Delete from front"<<endl;
cout<<"5.Delete from back"<<endl;
cout<<"6.Display size of linked list"<<endl;
cout<<"7.Display Linked List"<<endl;
cout<<"8.Reverse Linked List "<<endl;
cout<<"9.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.pushFront();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.pushBack();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert();
cout<<endl;
break;
case 4:
cout<<"Delete a node from front: "<<endl;
sl.popFront();
break;
case 5:
cout<<"Delete a node from back:"<<endl;
sl.popBack();
cout<<endl;
break;
case 6:
cout<<"Display size of link list"<<endl;
cout<<sl.size();
cout<<endl;
break;
case 7:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 8:
cout<<"Reverse elements of Link List"<<endl;
sl.reverse();
cout<<endl;
break;
case 9:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
/*
* Creating Node
*/
node *single_llist::create_node(double value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->num = value;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void single_llist::pushFront()
{
double value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
end = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/*
* Inserting Node at last
*/
void single_llist::pushBack()
{
double value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp;
temp = create_node(value);
if (start == NULL)
{
start = temp;
end = temp;
start->next = NULL;
}
else
{
end->next = temp;
end = temp;
}
cout<<"Element Inserted at last"<<endl;
}
/*
* Insertion of node at a given position
*/
void single_llist::insert()
{
double value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
/*
* Delete element from front
*/
void single_llist::popFront()
{
struct node *s;
s = start;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}else{
start = start->next;
}
free(s);
cout<<"Element Deleted"<<endl;
}
/*
* Delete element from back
*/
void single_llist::popBack()
{
int i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
struct node *s, *ptr;
s = start;
if (start->next == NULL)
{
free(s);
}
else
{
while (s->next->next != NULL) // Get second last element in list
{
s = s->next;
}
end = s;
s=s->next;
free(s);
}
cout<<"Element Deleted"<<endl;
}
/*
* Reverse Link List
*/
void single_llist::reverse()
{
struct node *ptr1, *ptr2, *ptr3;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (start->next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
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)
{
cout<<temp->num<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
/*
* Get sixe of the linked list
*/
int single_llist::size()
{
int i, counter = 0;
if (start == NULL)
{
return 0;
}
struct node *s;
s = start;
while (s!= NULL) // Get second last element in list
{
counter++;
}
return counter;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.