Please help me with this program. Code is almost done, but I needhelp with the b
ID: 3610690 • Letter: P
Question
Please help me with this program. Code is almost done, but I needhelp with the bold comments. Thanks#include<iostream>
usingnamespace std;
classLinkedList
{
private:
struct Node
{
int data;
struct Node *next;
};
Node *head;
void erase();
public:
LinkedList()
{
// SET head TO NULL
}
~LinkedList()
{
// CALL erase()
}
void appendNode(int);
void print();
void reverse();
};
voidLinkedList::appendNode(int num)
{
Node *newNode, *nodePtr;
ListNode *newNode; //To point to a new node
ListNode *nodePtr; //To move through the list
//Allocate a new node and store numthere.
newNode = num;
newNode->value = num;
newNode-> = NULL;
//If there are no nodes in the list,ask newNode the first node.
if (!head)
head = newNode;
else //Otherwise, inset newNodeat end.
{
//InitializenodePtr to head of list.
nodePtr = head;
//Find the lastnode in the list
while(nodePtr->next)
nodePtr = nodePtr->next;
//Insert newNodeas the last node.
nodePtr->next =newNode;
}
}
// WRITE THE CODE TO TRAVERSE THE LIST STARTING AT head ANDPRINT THE VALUES
voidLinkedList::print()
{
Node *nodePtr;
//ADD ONLY 1 LINE OF CODE TO REPLACETHIS COMMENT
while (nodePtr)
{
// ADD ONLY 2LINES OF CODE TO REPLACE THIS COMMENT
}
cout <<endl;
}
voidLinkedList::erase()
{
Node *nodePtr, *nextNode;
ListNode *nodePtr; //To traverse the list
ListNode *nextNode; //To point to the next node
//Position nodePtr at the head of thelist.
nodePtr = head;
//While nodePtr is not at the end ofthe list...
while (nodePtr != NULL)
{
//Save a pointerto the next node.
nextNode =nodePtr->next;
//Delete thecurrent node.
delete nodePtr;
//PositionnodePtr at the next node.
nodePtr = nextNode;
}
head = NULL;
}
voidLinkedList::reverse()
{
Node *newHead = NULL, *newNode, *nodePtr,*tempPtr;
// SET nodePtr To head
while (nodePtr)
{
//USE ONLY 3 LINES OF CODE TO:
//make a new Node and assign its address tonewNode,
//assign the data of the current node to thenew node’s data
//assign NULL to the new node’snext
if (newHead != NULL)
{
//USE ONLY 3 LINES OF CODE TO MAKE THE NEW Node BE THEHEAD OF THE LIST
}
else
{
newHead = newNode;
}
nodePtr =nodePtr->next;
//CALL erase()
//ASSIGN newHead TO head
}
int main()
{
// Create an instance
LinkedList exampleList;
// Build list by appendingnodes
exampleList.appendNode(1);
exampleList.appendNode(3);
exampleList.appendNode(5);
exampleList.appendNode(7);
exampleList.appendNode(9);
// Display the nodes.
cout << "Here are the values in theLinked List: ";
exampleList.print();
cout << endl;
// Reverse the list
cout << "The new reversedlist... ";
exampleList.reverse();
// Display the nodesagain
cout << "Here are the values in theLinked List: ";
exampleList.print();
cout << endl;
sytem("pause");
return 0;
}
_____________________________________________
Sample Output:
There are the values in the Linked List:
1 3 5 7 9
The new revered list...
Here are the values in the Linked List:
9 7 5 3 1
Press any key to continue . . .
Explanation / Answer
Dear,//Program on linked list
#include <iostream.h>
#include <conio.h>
#include <time.h>
class node
{
int data;
char ch1;
node *start,*left,*temp1,*right,*fresh,*temp,*last;
public:
node()
{
start=NULL;
last=NULL;
}
void menu()
{
cout<<"LINKED LIST OPERATIONS"<<endl;
cout<<"======================"<<endl;
cout<<"1.INSERT"<<endl;
cout<<"2.DELETE"<<endl;
cout<<"3.EXIT"<<endl;
}
void create()
{
fresh=new node();
cout<<"Enter the data"<<endl;
cin>>fresh->data;
fresh->left=NULL;
fresh->right=NULL;
}
void insbeg()
{
create();
if(start==NULL && last==NULL)
{
start=fresh;
last=fresh;
}
else
{
fresh->right=start;
start->left=fresh;
start=fresh;
}
}
void insend()
{
create();
temp=start;
while(temp->right!=NULL)
temp=temp->right;
temp->right=fresh;
fresh->left=temp;
last=fresh;
}
void delbeg()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
temp=start;
cout<<"Deleted element is:"<<start->data<<endl;
start=start->right;
start->left=NULL;
delete temp;
}
}
void delend()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
temp=last;
cout<<"Deleted element is:"<<last->data<<endl;
last=last->left;
last->right=NULL;
delete temp;
cout<<last;
}
}
void menuins()
{
do
{
cout<<"INSERT OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"a.INSERT AT START"<<endl;
cout<<"b.INSERT AT END"<<endl;
cout<<"c.TRAVERSE(FORWARD)"<<endl;
cout<<"d.TRAVERSE(BACKWARD)"<<endl;
cout<<"e.EXIT"<<endl;
cout<<"Enter ur choice"<<endl;
cin>>ch1;
switch(ch1)
{
case 'a':insbeg();break;
case 'b':insend();break;
case 'c':traversefront();break;
case 'd':traverseback();break;
case 'e':break;
default:cout<<"Please enter the rightchoice"<<endl;break;
}
}while(ch1!='e');
}
void menudel()
{
do
{
cout<<"DELETE OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"a.DELETE AT START"<<endl;
cout<<"b.DELETE AT END"<<endl;
cout<<"c.TRAVERSE(FORWARD)"<<endl;
cout<<"d.TRAVERSE(BACKWARD)"<<endl;
cout<<"e.EXIT"<<endl;
cout<<"Enter ur choice"<<endl;
cin>>ch1;
switch(ch1)
{
case 'a':delbeg();break;
case 'b':delend();break;
case 'c':traversefront();break;
case 'd':traverseback();break;
case 'e':break;
default:cout<<"Please enter the rightchoice"<<endl;break;
}
}while(ch1!='e');
}
void traversefront()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
cout<<"The elements in the dll(forward)are:"<<endl;
for(temp=start;temp!=NULL;temp=temp->right)
cout<<temp->left<<":"<<temp->data<<":"<<temp<<"->"<<endl;
cout<<endl;
}
}
void traverseback()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
cout<<"The elements in the dll(backward)are:"<<endl;
// end();
for(temp=last;temp!=NULL;temp=temp->left)
cout<<temp->right<<":"<<temp->data<<":"<<temp<<"->";
cout<<endl;
}
}
};
void main()
{
int ch;
node n;
clrscr();
do
{
n.menu();
cout<<"Enter ur choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:n.menuins();
break;
case 2:n.menudel();
break;
case 3:break;
default:cout<<"Pleaseenter the right choice"<<endl;break;
}
}while(ch!=3);
getch();
} I hope thiswill helpful for you.............
//Program on linked list
#include <iostream.h>
#include <conio.h>
#include <time.h>
class node
{
int data;
char ch1;
node *start,*left,*temp1,*right,*fresh,*temp,*last;
public:
node()
{
start=NULL;
last=NULL;
}
void menu()
{
cout<<"LINKED LIST OPERATIONS"<<endl;
cout<<"======================"<<endl;
cout<<"1.INSERT"<<endl;
cout<<"2.DELETE"<<endl;
cout<<"3.EXIT"<<endl;
}
void create()
{
fresh=new node();
cout<<"Enter the data"<<endl;
cin>>fresh->data;
fresh->left=NULL;
fresh->right=NULL;
}
void insbeg()
{
create();
if(start==NULL && last==NULL)
{
start=fresh;
last=fresh;
}
else
{
fresh->right=start;
start->left=fresh;
start=fresh;
}
}
void insend()
{
create();
temp=start;
while(temp->right!=NULL)
temp=temp->right;
temp->right=fresh;
fresh->left=temp;
last=fresh;
}
void delbeg()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
temp=start;
cout<<"Deleted element is:"<<start->data<<endl;
start=start->right;
start->left=NULL;
delete temp;
}
}
void delend()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
temp=last;
cout<<"Deleted element is:"<<last->data<<endl;
last=last->left;
last->right=NULL;
delete temp;
cout<<last;
}
}
void menuins()
{
do
{
cout<<"INSERT OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"a.INSERT AT START"<<endl;
cout<<"b.INSERT AT END"<<endl;
cout<<"c.TRAVERSE(FORWARD)"<<endl;
cout<<"d.TRAVERSE(BACKWARD)"<<endl;
cout<<"e.EXIT"<<endl;
cout<<"Enter ur choice"<<endl;
cin>>ch1;
switch(ch1)
{
case 'a':insbeg();break;
case 'b':insend();break;
case 'c':traversefront();break;
case 'd':traverseback();break;
case 'e':break;
default:cout<<"Please enter the rightchoice"<<endl;break;
}
}while(ch1!='e');
}
void menudel()
{
do
{
cout<<"DELETE OPERATIONS"<<endl;
cout<<"================="<<endl;
cout<<"a.DELETE AT START"<<endl;
cout<<"b.DELETE AT END"<<endl;
cout<<"c.TRAVERSE(FORWARD)"<<endl;
cout<<"d.TRAVERSE(BACKWARD)"<<endl;
cout<<"e.EXIT"<<endl;
cout<<"Enter ur choice"<<endl;
cin>>ch1;
switch(ch1)
{
case 'a':delbeg();break;
case 'b':delend();break;
case 'c':traversefront();break;
case 'd':traverseback();break;
case 'e':break;
default:cout<<"Please enter the rightchoice"<<endl;break;
}
}while(ch1!='e');
}
void traversefront()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
cout<<"The elements in the dll(forward)are:"<<endl;
for(temp=start;temp!=NULL;temp=temp->right)
cout<<temp->left<<":"<<temp->data<<":"<<temp<<"->"<<endl;
cout<<endl;
}
}
void traverseback()
{
if(start==last)
cout<<"List is empty"<<endl;
else
{
cout<<"The elements in the dll(backward)are:"<<endl;
// end();
for(temp=last;temp!=NULL;temp=temp->left)
cout<<temp->right<<":"<<temp->data<<":"<<temp<<"->";
cout<<endl;
}
}
};
void main()
{
int ch;
node n;
clrscr();
do
{
n.menu();
cout<<"Enter ur choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:n.menuins();
break;
case 2:n.menudel();
break;
case 3:break;
default:cout<<"Pleaseenter the right choice"<<endl;break;
}
}while(ch!=3);
getch();
} I hope thiswill helpful for you.............
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.