Need help with a programming problem using the Processing language: Description
ID: 3823980 • Letter: N
Question
Need help with a programming problem using the Processing language:
Description You will implement and submit a doubly link list, with the following requirements. Requirements: You MUST to implement the following requirements to your doubly link list Class Node o int value o Node next, prev Holds the next and previous nodes in the list. Class Link List o Node head Holds the node at the front of the list o Node tail Holds the node at the end of the list o int size() returns a number nodes in the list o void print() prints all the values of all the nodes in the list o void AddTo Front (Node n) adds a node 'n' to the front of the listExplanation / Answer
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
//Node created
struct node
{
//To store data
int info;
//To store next pointer
struct node *next;
//To store previous pointer
struct node *prev;
};//End of structure
//Rename
typedef struct node ND;
//Class definition
class DoubleLinkedList
{
public:
//Head and tail pointer to point start and end of linked list respectively
ND *head, *tail;
//To store length
int len;
//Creates a linked list
void create_list(int value);
//Adds a node at the beginning
void AddToFront(ND *);
//Adds a node at the end
void AddToBack(ND *);
//Remove a node at specified index position
void Remove(int);
//Remove a node based on the value
void RemoveValues(int);
//Display all the nodes
void print();
//Display number of nodes available
int size();
//Constructor
DoubleLinkedList()
{
head = tail = NULL;
len = 0;
}//End of constructor
};//End of class
//Main function
int main()
{
int choice, element, position;
//Creates an object
DoubleLinkedList dl;
//Creates a temporary node
ND *temp;
//Loops till user choice
while (1)
{
//Menu display
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<" Doubly linked list "<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1. Create Node"<<endl;
cout<<"2. Add at beginning"<<endl;
cout<<"3. Add at end"<<endl;
cout<<"4. Remove Index"<<endl;
cout<<"5. Remove Value"<<endl;
cout<<"6. Display"<<endl;
cout<<"7. Size"<<endl;
cout<<"8.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
//Calls appropriate function based on choice
switch ( choice )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.create_list(element);
cout<<endl;
break;
case 2:
temp = new(ND);
dl.AddToFront(temp);
cout<<endl;
break;
case 3:
temp = new(ND);
dl.AddToBack(temp);
cout<<endl;
break;
case 4:
if (dl.head == NULL)
{
cout<<"List empty, nothing to delete"<<endl;
break;
}
cout<<"Enter the index position to delete: ";
cin>>element;
dl.Remove(element);
cout<<endl;
break;
case 5:
if (dl.head == NULL)
{
cout<<"List empty, nothing to delete"<<endl;
break;
}
cout<<"Enter the value to delete: ";
cin>>element;
dl.RemoveValues(element);
break;
case 6:
dl.print();
cout<<endl;
break;
case 7:
cout<<"Number of elements are: "<<dl.size()<<endl;
break;
case 8:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}//End of switch
}//End of loop
return 0;
}//End of function
//To remove a given value from the linked list
void DoubleLinkedList::RemoveValues(int value)
{
//Creates temporary nodes
ND *tmp, *q;
//If first element is deleted
if (head -> info == value)
{
tmp = head;
head = head->next;
head->prev = NULL;
cout<<"Element Deleted"<<endl;
//Release memory
free(tmp);
return;
}//End of if
//q will point to head
q = head;
//Loops till end
while (q -> next -> next != NULL)
{
//If the value to be deleted in between
if (q -> next -> info == value)
{
tmp = q -> next;
q -> next = tmp -> next;
tmp -> next -> prev = q;
cout<<"Element Deleted"<<endl;
//Release memory
free(tmp);
return;
}//End of if
//Move to next position
q = q->next;
}//End of loop
//If last value is deleted
if (q -> next -> info == value)
{
tmp = q -> next;
//Release memory
free(tmp);
q -> next = NULL;
cout<<"Element Deleted"<<endl;
return;
}//End of if
cout<<"Element "<<value<<" not found"<<endl;
}//End of function
//To create a linked list
void DoubleLinkedList::create_list(int value)
{
//Creates temporary nodes
ND *s, *temp;
temp = new(ND);
//Assigns value
temp->info = value;
temp->next = NULL;
//If first node
if (head == NULL)
{
temp->prev = NULL;
head = temp;
tail = temp;
}//End of if
//Otherwise
else
{
//S pointing to head
s = head;
//Moves till end
while (s -> next != NULL)
s = s -> next;
s -> next = temp;
temp -> prev = s;
tail = temp;
}//End of else
}//End of function
//Inserts a node at the beginning
void DoubleLinkedList::AddToFront(ND *temp)
{
int data;
//If head is null no node is available
if (head == NULL)
{
cout<<"First Create the list."<<endl;
return;
}//End of if
//Accepts data
cout<<" Enter the data: ";
cin>>data;
//Insert node
temp -> prev = NULL;
temp -> info = data;
temp -> next = head;
head -> prev = temp;
head = temp;
cout<<"Element Inserted"<<endl;
}//End of function
//Insert a node at the end
void DoubleLinkedList::AddToBack(ND *temp)
{
int data;
//If head is null no node is available
if (head == NULL)
{
cout<<"First Create the list."<<endl;
return;
}//End of if
//Accepts data
cout<<" Enter data to insert at end: ";
cin>>data;
//Inserts node
temp -> info = data;
temp -> prev = tail;
tail -> next = temp;
temp -> next = NULL;
cout<<"Element Inserted"<<endl;
}//End of function
//Delete the node at specified index position
void DoubleLinkedList::Remove(int pos)
{
int count = 0;
//Creates temporary node
ND *tmp, *q;
//If First index deleted
if (pos == 0)
{
tmp = head;
head = head->next;
head -> prev = NULL;
cout<<"Element Deleted"<<endl;
//Release memory
free(tmp);
return;
}//End of if
//q pointing to head
q = head;
count = 1;
//Loops till end
while (q->next->next != NULL)
{
//If index position in between move up to the position specified
if (count == pos)
{
tmp = q -> next;
q -> next = tmp -> next;
tmp -> next -> prev = q;
cout<<"Element Deleted"<<endl;
//Release memory
free(tmp);
return;
}//End of if
//Move to next
q = q->next;
//Increase the counter by one
count++;
}//End of while
//q pointing to last but one
q = tail->prev;
//If Last index position deleted
if (pos + 1 == len)
{
tmp = q -> next;
//Release memory
free(tmp);
q -> next = NULL;
tail = q;
cout<<"Element Deleted"<<endl;
return;
}//End of if
cout<<"Index "<<pos<<" not found"<<endl;
}//End of function
//Display the linked list
void DoubleLinkedList::print()
{
//Temporary node
ND *q;
//If head is null there is no node
if (head == NULL)
{
cout<<"Empty List. Nothing to display"<<endl;
return;
}//End of if
//q pointing to head
q = head;
cout<<"Doubly Link List Contents :"<<endl;
//Loops till end
while (q != NULL)
{
cout<< q -> info <<" <-> ";
q = q-> next;
}//End of while
}//End of function
//Returns the size of the linked list
int DoubleLinkedList::size()
{
//Temporary node pointing to head
ND *q = head;
//Initializes the counter to zero
int counter = 0;
//Loops till end
while (q != NULL)
{
//Move to next
q = q -> next;
//Increase the counter by one
counter++;
}//End of if
//Assigns to length
len = counter;
//Returns the size
return counter;
}//End of function
Output:
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 1
Enter the element: 11
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 1
Enter the element: 22
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 1
Enter the element: 33
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 1
Enter the element: 44
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 6
Doubly Link List Contents :
11 <-> 22 <-> 33 <-> 44 <->
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 2
Enter the data: 10
Element Inserted
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 6
Doubly Link List Contents :
10 <-> 11 <-> 22 <-> 33 <-> 44 <->
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 3
Enter data to insert at end: 55
Element Inserted
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 5
Enter the value to delete: 0
Element 0 not found
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 6
Doubly Link List Contents :
10 <-> 11 <-> 22 <-> 33 <-> 44 <-> 55 <->
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 5
Enter the value to delete: 33
Element Deleted
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 6
Doubly Link List Contents :
10 <-> 11 <-> 22 <-> 44 <-> 55 <->
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 4
Enter the index position to delete: 0
Element Deleted
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 6
Doubly Link List Contents :
11 <-> 22 <-> 44 <-> 55 <->
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 7
Number of elements are: 4
----------------------------
Doubly linked list
----------------------------
1. Create Node
2. Add at beginning
3. Add at end
4. Remove Index
5. Remove Value
6. Display
7. Size
8.Quit
Enter your choice : 8