Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

given the implementation of a singly ( linear ) linked list of character elements Please modify the code as follows. Add a function named swapLastWithFirst

given the implementation of a singly (linear) linked list of character elements 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 *head=NULL;
void printList()
{
Node* curr = head;
while(curr != NULL){
cout << curr->data <<"";
curr = curr->next;
}
cout << endl;
}
void insertFirst(char data){
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
if(head == NULL){
head = newNode;
}
else {
newNode->next = head;
head = newNode;
}
}
void insertLast(char data){
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
if(head == NULL){
head = newNode;
}
else {
Node* curr = head;
while(curr->next != NULL)
curr = curr->next;
curr->next = newNode;
}
}
void insertAtPos(char data, int pos){
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
int curPos =1;
if(head == NULL){
head = newNode;
}
else {
Node *curr = head;
Node *prev;
while(curPos != pos && curr->next != NULL){
prev=curr;
curr = curr->next;
curPos++;
}
prev->next = newNode;
newNode->next = curr;
}
}
void deleteAtPos(int pos){
Node* curr = head;
int curPos =1;
if (head !=NULL)
{
if(pos ==1){
head = curr->next;
delete curr;
}
else{
Node *prev;
while(curPos != pos && curr->next != NULL){
prev=curr;
curr = curr->next;
curPos++;
}
prev->next = curr->next;
delete curr;
}
}
}
int search(char value)
{
int pos =1;
Node* curr = head;
while(curr != NULL){
if(curr->data == value)
return pos;
pos++;
curr = curr->next;
}
return -1;
}
int main(){
cout << "Hello World!
";
insertFirst('a');
insertFirst('b');
insertFirst('c');
insertFirst('d');
printList();
insertLast('e');
insertLast('f');
insertAtPos('g',3);
printList();
deleteAtPos(3);
printList();
deleteAtPos(1);
printList();
int pos = search('e');
cout << pos << endl;
pos = search('f');
cout << pos << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions

Question

What are the requirements of a wildlife rehabilitator?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago