Question: Please modify the code as follows. Add a function named swapLastWithFirst which swaps the first node with the last node. Thereafter, the main function should
Please modify the code as follows.
Add a function named "swapLastWithFirst" which swaps the first node with the last node. Thereafter, the "main" function should call "swapLastWithFirst" and then call "printList".
#include
using namespace std;
struct Node
char data;
struct Node next;
;
struct Node headNULL;
void printList
Node curr head;
whilecurrnext head
cout currdata ;
curr currnext;
cout currdata ;
cout endl;
void insertFirstchar data
Node newNode new Node;
newNodedata data;
newNodenext NULL;
ifhead NULL
head newNode;
newNodenext head;
else
Node curr head;
whilecurrnext head
curr currnext;
currnext newNode;
newNodenext head;
head newNode;
void insertLastchar data
Node newNode new Node;
newNodedata data;
newNodenext NULL;
ifhead NULL
head newNode;
newNodenext head;
else
Node curr head;
whilecurrnext head
curr currnext;
currnext newNode;
newNodenext head;
void insertAtPoschar data, int pos
Node newNode new Node;
newNodedata data;
newNodenext NULL;
int curPos ;
ifhead NULL
head newNode;
newNodenext head;
else
Node curr head;
Node prev;
whilecurPos pos && currnext head
prevcurr;
curr currnext;
curPos;
prevnext newNode;
newNodenext curr;
void deleteAtPosint pos
Node curr head;
int curPos ;
if head NULL
ifpos
whilecurrnext head
curr currnext;
currnext headnext;
Node temp head;
head tempnext;
delete temp;
else
Node prev;
whilecurPos pos && currnext head
prevcurr;
curr currnext;
curPos;
prevnext currnext;
delete curr;
int searchchar value
int pos ;
Node curr head;
ifhead NULL
do
ifcurrdata value
return pos;
pos;
curr currnext;
whilecurr head;
return ;
int main
cout "Hello World!
;
insertFirsta;
insertFirstb;
insertFirstc;
insertFirstd;
printList;
insertLaste;
insertLastf;
insertAtPosg;
printList;
deleteAtPos;
printList;
deleteAtPos;
printList;
int pos searche;
cout pos endl;
pos searchf;
cout pos endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
