c++ only
Program 2: Linked List Class
For this problem, let us take the linked list we wrote in a
functional manner in a previous assignment and convert it into a
Linked List class. For extra practice with pointers we’ll expand
its functionality and make it a doubly linked list with the ability
to traverse in both directions.
Since the list is doubly linked, each node will have the following
structure:
struct Node { int number; Node * nextNode; Node * prevNode;
};
In addition, we’ll maintain both a head node and a tail node as
variables within the class. Your class, therefore, should have at
least two private attributes (data members): (a) a Node variable
that serves as the head node; (b) a Node variable that serves as
the tail node. (This list can be expanded later as we add
functionality to the class.)
Description of the class
At the minimum, your class should have the following public member
functions:
1. A default constructor.
This constructor will:
a. Initialize the head node and the tail node. b. Set the previous
node pointer of the head node to nullptr and the nextNode pointer
to point to the tail node. c. Set the previous node pointer of the
tail node to point to the head node and the next node pointer to
nullptr.
2. Node * addNodeToEnd (int value);
This function will add a node to the end of the list, set the
node’s value, and return a pointer to the new node added. 3. Node *
addNodeToBeginning (int value);
This function will add a node to the beginning of the list, set the
node’s value, and return a pointer to the new node added.
CS1337 – Computer Science I page: 6
4. void printListForward ();
This member function will traverse the list from the first node to
the last node and print out the values using the formatting
requirements we’ve maintained throughout the semester (e.g., 10
numbers per line, 5 byte field, etc).
5. void printListBackward ();
This member function will traverse the list from the last node to
the first node and print out the values using the formatting
requirements we’ve maintained throughout the semester (e.g., 10
numbers per line, etc).
6. void sort ();
This member function will sort the numbers in the list by using one
of the sorting algorithms we’ve implemented before. (I suggest
using Bubble Sort, as it works with adjacent elements.) Note that
it is not necessary to swap actual Nodes; the sort can be performed
by simply swapping values between nodes.
7. int & operator [ ] (const int &);
Implement the overloaded subscript operator on your list. This
operator should return a reference to the data value of the
specified node. For example, list[5] would return a reference to
the data item of the sixth element in the list. Demonstrate the use
of your operator by taking an element, say the 5th, changing its
value, and then reprinting the list to show that the value was
indeed changed. (e.g., list[5] = 100;) You should have some code
that displays an error message if the index supplied is out of
bounds.
8. A destructor for the list.
This member function will traverse the list and delete each node in
turn.
General Tasking
Once the LinkedList class is built, have your main() function
perform the following tasks:
1. Instantiate a LinkedList object. 2. Read the data from the same
file we used in the previous linked list problem, “Perm 50.txt”,
into the list. 3. Print the list in forward order by using the
printListForward() member function. 4. Sort the list by calling the
sort() member function. 5. Print the list in forward order again,
but after it has been sorted.
CS1337 – Computer Science I page: 7
6. Print the list in reverse order using the printListBackward()
member function. 7. Demonstrate the use of the overloaded subscript
operator by using it to change a value in the list, then reprinting
the list the list in the forward direction one last time.
Make all reports neat and professional in appearance.
Some Comments:
This is a very basic doubly linked list data structure; much more
functionality can be added to a list like this. To name just a few
items, we could also have:
1. the ability to add nodes to the list at arbitrary locations
within the list. 2. the ability to remove nodes at arbitrary
locations from the list. 3. a counter that counts the number of
elements in the list. 4. an iterator that can traverse the
list.
All of this functionality and more is typical of linked list data
structures and would make our list more robust and useful.
Nevertheless, it should be clear that once the list is built, it is
far easier to use and maintain as a class than as a series of
functions that are disassociated from the data they are working
on.
#include<iostream>
using namespace std;
// create a node
struct Node{
public:
int info;
Node *next;
Node *prev;
Node(int el,Node *n=0,Node *p=0){
info=el;
next=n;
prev=p;
}
};
class List{
public:
Node *head;
Node *tail;
List(){
head=NULL;
tail=NULL;
}
~List(){
if(head==NULL){
return;
}
else{
while(head!=NULL){
Node *h=head;
head=head->next;
delete(h);
}
}
}
void insertAtHead();
void insertAtTail();
void insertAtAnyplace();
void deleteFromTail();
void deleteFromHead();
void deleteFromAnyplace();
void displayforward();
void displaybackward();
void SortList();
};
void List::SortList(){
if(head==NULL)
return;
Node *temp=head;
Node *pred=head;
while(temp->next!=NULL){
while(pred->next!=NULL){
if(pred->info > pred->next->info)
{
int tem=pred->next->info;
pred->next->info=pred->info;
pred->info=tem;
}
pred=pred->next;
}
temp=temp->next;
}
displayforward();
}
void List ::displayforward(){
Node *temp=head;
while(temp!=NULL){
cout << temp->info<< "-> ";
temp=temp->next;
}
}
void List ::displaybackward(){
Node *temp=tail;
while(temp!=NULL){
cout << temp->info<< "-> ";
temp=temp->prev;
}
}
void List::deleteFromAnyplace(){
int el;
cout << "ENTER THE ELEMENT DO U WANT TO DELETE "<<
endl;
cin >>el;
if(head==NULL)
cout << "THE LIST IS EMPTY"<< endl;
else if(head==tail){
head=tail=NULL;
}
else if(head->info==el){
head=head->next;
head->prev=NULL;
}
else if(tail->info==el){
tail=tail->prev;
tail->next=NULL;
}
else{
Node *temp=head;
while((temp!=NULL)&&(el!=temp->info)){
if(el==temp->info){
temp->prev->next=temp->next;
return;
}
temp=temp->next;
}
if(temp==NULL)
cout <<"ELEMENT IS NOT EXIST"<< endl;
}
}
void List :: insertAtTail(){
int info;
cout << "ENTER THE ELEMENT DO U WANT TO INSERT "<<
endl;
cin >>info;
if(head==0)
tail=head=new Node(info);
else{
tail->next=new Node(info,NULL,tail);
tail=tail->next;
}
}
void List ::deleteFromHead(){
if(head==NULL)
cout << "OOPS LIST IS ALREADY EMPTY "<< endl;
else if(head==tail){
head=tail=NULL;
}
else{
int el=head->info;
head=head->next;
head->prev=NULL;
cout << "DELETED ELEMENT IS "<< endl;
cout << el<< endl;
}
}
void List::deleteFromTail(){
if(tail==NULL)
cout << "OOPS LIST IS ALREADY EMPTY "<< endl;
else if(tail==head){
head=tail=NULL;
}
else{
int el=tail->info;
tail=tail->prev;
tail->next=NULL;
cout << "THE DELETED ELEMENT IS "<<el<<
endl;
}
}
void List::insertAtAnyplace() {
int info;
int ele;
cout << "ENTER THE ELEMENT DO U WANT TO INSERT " <<
endl;
cin >> info;
cout << "ENTER THE ELEMENT AFTER WHICH DO U WANT TO INSERT
"<< endl;
cin >>ele;
if(head==NULL){
cout <<" CAN INSERT ELEMENT BECAUSE GIVEN LIST IS EMPTY "
<<endl;
return ;
}
else{
Node *temp=head;
Node *newNode;
while(temp!=NULL){
if(ele==temp->info){
temp=new Node(info,temp->next,temp->prev);
return ;
}
temp=temp->next;
}
}
cout << "THE GIVEN ELEMENT IS NOT EXIST SO CAN NOT INSERT
"<< endl;
}
void List :: insertAtHead(){
int info;
cout << "ENTER THE ELEMENT DO U WANT TO INSERT "<<
endl;
cin >> info;
if(tail==NULL){
head=new Node(info);
tail=head;
}
else if(head==tail){
head=new Node(info,head,NULL);
tail=head->next;
}
else{
head=new Node(info,head,NULL);
}
}
int main(){
List object;
int choice;
char ch;
do{
cout << "MENU"<< endl;
cout << "1.INSERT ELEMENT AT HEAD"<<endl;
cout << "2.INSERT ELEMENT AT TAIL"<< endl;
cout << "3.INSERT AT ANY GIVEN NODE"<< endl;
cout << "4.DELETE FROM HEAD "<< endl;
cout << "5.DELETE FROM TAIL"<< endl;
cout << "6.DELETE FROM ANY GIVEN LOCATION"<<
endl;
cout << "7.DISPLAY THE LIST IN FORWARD DIRECTION
"<<endl;
cout << "8.DISPLAY THE LIST IN BACKWARD DIRECTION
"<<endl;
cout << "9.Sort "<<endl;
cout << "ENTER UR CHOICE"<< endl;
cin >> choice;
switch(choice){
case 1: object.insertAtHead();
object.displayforward();
break;
case 2: object.insertAtTail();
object.displayforward();
break;
case 3: object.insertAtAnyplace();
object.displayforward();
break;
case 4: object.deleteFromHead();
object.displayforward();
break;
case 5: object.deleteFromTail();
object.displayforward();
break;
case 6: object.deleteFromAnyplace();
object.displayforward();
break;
case 7: object.displayforward();
break;
case 8: object.displaybackward();
break;
// SortList the list
case 9: object.SortList();
break;
bydefault: cout << "PLZ ENTER THE CORRECT CHOICE"<<
endl;
}
cout <<endl<< "IF U WANT TO PERFORM MORE OPERATION THAN
PRESS Y"<< endl;
cin >> ch;
}while(ch=='y'||ch=='Y');
object.~List();
return 0;
}
c++ only Program 2: Linked List Class For this problem, let us take the linked list...
I need this in C++. This is all
one question
Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...
***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful. Also note that the function must work no matter how many nodes...
implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...
C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function 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 member functions to display the list, check if...
Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function 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 member functions to display the list, check if...
Consider a Linked List program with the following class: typedef int datatype; struct node { datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...
C++ program, item.cpp implementation.
Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done...
LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...
Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...