#include <stdio.h> #include <iostream.h> #include <conio.h> #include <stdlib.h>
ID: 3875855 • Letter: #
Question
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct stock
{
int data;
struct stock *next;
}*nnode,*ptr,*p,*prev,*start;
class LinkedList
{
public:
LinkedList()
{
start=NULL;
}
void create()
{
nnode=new stock;
cout<<"enter the data you want to enter";
cin>>nnode->data;
nnode->next=NULL;
start=nnode;
}
void addAtBeginning()
{
nnode=new stock;
cout<<"enter the data you want to enter";
cin>>nnode->data;
nnode->next=start;
start=nnode;
}
void addAtLast()
{
for(ptr=start;ptr!=NULL;prev=ptr,ptr=ptr->next);
cout<<"enter the data you want to enter";
nnode=new stock;
cin>>ptr->data;
prev->next=nnode;
nnode->next=NULL;
}
void deleteFirst()
{
ptr=start;
start=ptr->next;
delete(ptr);
}
void deleteLast()
{
for(ptr=start;ptr!=NULL;p=prev,prev=ptr,ptr=ptr->next);
p->next=NULL;
delete(prev);
}
void display()
{
for(ptr=start;ptr!=NULL;ptr=ptr->next)
cout<<ptr->data;
}
int totalNumberOfNodes()
{
stock *ptr;
ptr=start;
int count=0;
while (ptr!=NULL)
{
count++;
ptr=ptr->next;
}
return count;
}
void reverseLinkedList()
{
}
void swapTwoNodes()
{
cout<<"Enter the index location of two nodes that you want the user to be swapped in the linked list";
cin>>indexlocation1>>indexlocation2;
}
void sortElementsInLinkedList()
{
}
};
int main()
{
int ch;
LinkedList l;
clrscr();
while(1)
{
cout<<"enter choice";
cin>>ch;
switch(ch)
{
case 1:l.create();
break;
case 2:l.addAtBeginning();
break;
case 3:l.addAtLast();
break;
case 4:l.deleteFirst();
break;
case 5:l.deleteLast();
break;
case 6:l.display();
break;
case 7:l.reverseLinkedList();
break;
case 8:l.swapTwoNodes();
break;
case 9:l.sortElementsInLinkedList()
break;
default:cout<<"invalid choice";
}
}
getch();
return 0;
}
Please write function reverseLinkedList() to reverse all the elements in Linkedlist by swapping and exchanging the first node value with last node..second node with second last and so on.
swapTwoNodes() function to swap any two nodes values entered by user in Linked list and sortElementsInLinkedList() function to arrange all the elements in ascending order in a Single LinkedList keeping the entire structure same with proper comments and explanation.
Explanation / Answer
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct stock
{
int data;
struct stock *next;
}*nnode,*ptr,*p,*prev,*start;
class LinkedList
{
public:
LinkedList()
{
start=NULL;
}
void create()
{
nnode=new stock;
cout<<"enter the data you want to enter";
cin>>nnode->data;
nnode->next=NULL;
start=nnode;
}
void addAtBeginning()
{
nnode=new stock;
cout<<"enter the data you want to enter";
cin>>nnode->data;
nnode->next=start;
start=nnode;
}
void addAtLast()
{
for(ptr=start;ptr!=NULL;prev=ptr,ptr=ptr->next);
cout<<"enter the data you want to enter";
nnode=new stock;
cin>>ptr->data;
prev->next=nnode;
nnode->next=NULL;
}
void deleteFirst()
{
ptr=start;
start=ptr->next;
delete(ptr);
}
void deleteLast()
{
for(ptr=start;ptr!=NULL;p=prev,prev=ptr,ptr=ptr->next);
p->next=NULL;
delete(prev);
}
void display()
{
for(ptr=start;ptr!=NULL;ptr=ptr->next)
cout<<ptr->data;
}
int totalNumberOfNodes()
{
stock *ptr;
ptr=start;
int count=0;
while (ptr!=NULL)
{
count++;
ptr=ptr->next;
}
return count;
}
void reverseLinkedList(Node **head) // This function mainly calls reverseUntil() with prev as NULL
{
if (!head)
return;
reverseUntil(*head, NULL, head);
}
void reverseUntil(Node *curr, Node *prev, Node **head) // A simple and tail recursive function to reverse a linked // list. prev is passed as NULL initially.
{
/* If last node mark it head*/
if (!curr->next)
{
*head = curr;
/* Update next to prev node */
curr->next = prev;
return;
}
/* Save curr->next node for recursive call */
node *next = curr->next;
/* and update next ..*/
curr->next = prev;
reverseUntil(next, curr, head);
}
void swapTwoNodes( call * &head, call * &first, call * &second) // Swap Two Nodes
{
}
void sortElementsInLinkedList() // function to arrange all the elements in ascending order in a Single LinkedList
{
struct list *q,*p;
int data;
q=start;
while(q!=NULL)
{
p=q->link;
while(p!=NULL)
{
if(q->data>p->data)
{
data=q->data;
q->data=p->data;
p->data=data;
}
p=p->link;
}
q=q->link;
}
}
};
int main()
{
int ch;
LinkedList l;
clrscr();
while(1)
{
cout<<"enter choice";
cin>>ch;
switch(ch)
{
case 1:l.create();
break;
case 2:l.addAtBeginning();
break;
case 3:l.addAtLast();
break;
case 4:l.deleteFirst();
break;
case 5:l.deleteLast();
break;
case 6:l.display();
break;
case 7:l.reverseLinkedList();
break;
case 8:l.swapTwoNodes();
break;
case 9:l.sortElementsInLinkedList()
break;
default:cout<<"invalid choice";
}
}
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.