SlideShare a Scribd company logo
1 of 5
reverse the linked list (2,4,8,10) by:
stack;
iteration;
recursion.
Using C++
Solution
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#includ<stack>
using namespace std;
struct Node {
int value;
struct Node* next;
}node;
struct Node* reverse(struct Node* head) {Â Â // Iteration Method
struct Node* current,*prev;
current = head;
prev = NULL;
while(current!=NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next; }
head = prev;-
return head; }
struct Node* insert(Node* head,int value) {
Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->value = value;
temp->next = NULL;
if(head = = NULL)
head = temp;
else {
Node*temp1=head;
while(temp1->next!=NULL)
temp1 = temp1->next;
temp1->next = temp;
} return head;
}
public void print(Node* head) {
while(head!=NULL) {
cout<<head->value;
head = head->next; }
}
struct Node*recursiveReverse(struct Node* &head) {Â Â //Recursion method
if(head==NULL || head->next ==NULL) return;
Node*first = head;
Node *rest = head->next;
if(rest !=NULL) return;
recursiveReverse(rest);
first ->next->next = first;
first->next =NULL;
head = rest;
return head; }
void Reverse(struct Node* head)Â Â // stack Implementation
{
stack<node*> Stack;
node *traverse = head;
while(traverse!=NULL) {
Stack.push(traverse);
traverse = traverse->next;Â Â }
node *temp = Stack.top();
head = temp;
Stack.pop();
while(!Stack.empty()) {
temp->next = Stack.top();
Stack.pop();
temp = temp->next;Â Â }
temp->next = NULL;
return Stack; }
int main() {
struct Node* head =NULL;
head = insert(head,2);
head = insert(head,4);
head = insert(head,8);
head = insert(head,10);
print(head);
head= reverse(head);
print(head);
head = recursiveReverse(head);
print(head);
head = void Reverse(head);
void print(head);
return 0;
}
reverse the linked list (2-4-8-10) by- stack- iteration- recursion-  U.docx

More Related Content

Similar to reverse the linked list (2-4-8-10) by- stack- iteration- recursion- U.docx

Binary Tree in C++ coding in the data structure
Binary Tree in C++ coding in the data structureBinary Tree in C++ coding in the data structure
Binary Tree in C++ coding in the data structureZarghamullahShah
 
Help explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfHelp explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfalmonardfans
 
Solution#includestdio.h#includeconio.h#includealloc.h.pdf
Solution#includestdio.h#includeconio.h#includealloc.h.pdfSolution#includestdio.h#includeconio.h#includealloc.h.pdf
Solution#includestdio.h#includeconio.h#includealloc.h.pdfpoddaranand1
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptxSajalFayyaz
 
I need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdfI need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdfrohit219406
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03Nasir Mehmood
 
Hi,you covered mostly things.there are issue to point and link poi.pdf
Hi,you covered mostly things.there are issue to point and link poi.pdfHi,you covered mostly things.there are issue to point and link poi.pdf
Hi,you covered mostly things.there are issue to point and link poi.pdfaryan9007
 
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdfC++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdfarjuncp10
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 
operating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdfoperating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdfaquacareser
 
Please find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfPlease find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfangelfragranc
 
#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docxajoy21
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxnoreendchesterton753
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
Code#include stdio.h #includemalloc.h struct node {  .pdf
Code#include stdio.h #includemalloc.h struct node {  .pdfCode#include stdio.h #includemalloc.h struct node {  .pdf
Code#include stdio.h #includemalloc.h struct node {  .pdfbrijmote
 

Similar to reverse the linked list (2-4-8-10) by- stack- iteration- recursion- U.docx (20)

Binary Tree in C++ coding in the data structure
Binary Tree in C++ coding in the data structureBinary Tree in C++ coding in the data structure
Binary Tree in C++ coding in the data structure
 
Help explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfHelp explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdf
 
Solution#includestdio.h#includeconio.h#includealloc.h.pdf
Solution#includestdio.h#includeconio.h#includealloc.h.pdfSolution#includestdio.h#includeconio.h#includealloc.h.pdf
Solution#includestdio.h#includeconio.h#includealloc.h.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 
Linked lists
Linked listsLinked lists
Linked lists
 
I need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdfI need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdf
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03
 
Hi,you covered mostly things.there are issue to point and link poi.pdf
Hi,you covered mostly things.there are issue to point and link poi.pdfHi,you covered mostly things.there are issue to point and link poi.pdf
Hi,you covered mostly things.there are issue to point and link poi.pdf
 
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdfC++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
C++Write a function void headEnqueue(Queue q, int key) which enqu.pdf
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 
Linked Stack program.docx
Linked Stack program.docxLinked Stack program.docx
Linked Stack program.docx
 
operating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdfoperating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdf
 
Please find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdfPlease find the answer to the above problem as follows- Program.pdf
Please find the answer to the above problem as follows- Program.pdf
 
#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx
 
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docxWrite a C++ function that delete nodes in a doubly linkedlist- It shou.docx
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Code#include stdio.h #includemalloc.h struct node {  .pdf
Code#include stdio.h #includemalloc.h struct node {  .pdfCode#include stdio.h #includemalloc.h struct node {  .pdf
Code#include stdio.h #includemalloc.h struct node {  .pdf
 

More from acarolyn

Research the issues that you might face as a network administrator whe.docx
Research the issues that you might face as a network administrator whe.docxResearch the issues that you might face as a network administrator whe.docx
Research the issues that you might face as a network administrator whe.docxacarolyn
 
Reset Help By the Brnsted-Lowry definition- acids are proton donors an.docx
Reset Help By the Brnsted-Lowry definition- acids are proton donors an.docxReset Help By the Brnsted-Lowry definition- acids are proton donors an.docx
Reset Help By the Brnsted-Lowry definition- acids are proton donors an.docxacarolyn
 
SECTION III- SHORT ANSWER (10 possible points) Please give a brief ans.docx
SECTION III- SHORT ANSWER (10 possible points) Please give a brief ans.docxSECTION III- SHORT ANSWER (10 possible points) Please give a brief ans.docx
SECTION III- SHORT ANSWER (10 possible points) Please give a brief ans.docxacarolyn
 
Select a specific event that has recently occurred (within the last 30.docx
Select a specific event that has recently occurred (within the last 30.docxSelect a specific event that has recently occurred (within the last 30.docx
Select a specific event that has recently occurred (within the last 30.docxacarolyn
 
Security Breaches and the Six Dumb Ideas Consider a recent (2014- 2015.docx
Security Breaches and the Six Dumb Ideas Consider a recent (2014- 2015.docxSecurity Breaches and the Six Dumb Ideas Consider a recent (2014- 2015.docx
Security Breaches and the Six Dumb Ideas Consider a recent (2014- 2015.docxacarolyn
 
Scientifically continuous phase apersed phase Speaking Chapter Review.docx
Scientifically continuous phase apersed phase Speaking Chapter Review.docxScientifically continuous phase apersed phase Speaking Chapter Review.docx
Scientifically continuous phase apersed phase Speaking Chapter Review.docxacarolyn
 
Scenario- You are the Accountant for WanneBee Corporation WannaBee Cor.docx
Scenario- You are the Accountant for WanneBee Corporation WannaBee Cor.docxScenario- You are the Accountant for WanneBee Corporation WannaBee Cor.docx
Scenario- You are the Accountant for WanneBee Corporation WannaBee Cor.docxacarolyn
 
Scenario Allen Manesfield- Jenny Winters- and John Jacobsen have been.docx
Scenario Allen Manesfield- Jenny Winters- and John Jacobsen have been.docxScenario Allen Manesfield- Jenny Winters- and John Jacobsen have been.docx
Scenario Allen Manesfield- Jenny Winters- and John Jacobsen have been.docxacarolyn
 
S10-2 (Learning Objective 1- Describe the authority structure in a cor.docx
S10-2 (Learning Objective 1- Describe the authority structure in a cor.docxS10-2 (Learning Objective 1- Describe the authority structure in a cor.docx
S10-2 (Learning Objective 1- Describe the authority structure in a cor.docxacarolyn
 
rovide 34 paragraphs that define how the IT security landscape has evo.docx
rovide 34 paragraphs that define how the IT security landscape has evo.docxrovide 34 paragraphs that define how the IT security landscape has evo.docx
rovide 34 paragraphs that define how the IT security landscape has evo.docxacarolyn
 
Regarding intermolecular forces Sometimes London dispersion forces ou.docx
Regarding intermolecular forces  Sometimes London dispersion forces ou.docxRegarding intermolecular forces  Sometimes London dispersion forces ou.docx
Regarding intermolecular forces Sometimes London dispersion forces ou.docxacarolyn
 
Questions 12-14 refer to the following aqueous solutions- (A) 1 M BaCl.docx
Questions 12-14 refer to the following aqueous solutions- (A) 1 M BaCl.docxQuestions 12-14 refer to the following aqueous solutions- (A) 1 M BaCl.docx
Questions 12-14 refer to the following aqueous solutions- (A) 1 M BaCl.docxacarolyn
 
Reed Pentak a finance major has been following globalization and made.docx
Reed Pentak a finance major has been following globalization and made.docxReed Pentak a finance major has been following globalization and made.docx
Reed Pentak a finance major has been following globalization and made.docxacarolyn
 
Redesign- Make a prediction relating heat to the movement of molecules.docx
Redesign- Make a prediction relating heat to the movement of molecules.docxRedesign- Make a prediction relating heat to the movement of molecules.docx
Redesign- Make a prediction relating heat to the movement of molecules.docxacarolyn
 
Recycling and composting are important parts of an overall management.docx
Recycling and composting are important parts of an overall management.docxRecycling and composting are important parts of an overall management.docx
Recycling and composting are important parts of an overall management.docxacarolyn
 
R5232- RS422- 20mA Current Loop and 1EE488 are typical standard interf.docx
R5232- RS422- 20mA Current Loop and 1EE488 are typical standard interf.docxR5232- RS422- 20mA Current Loop and 1EE488 are typical standard interf.docx
R5232- RS422- 20mA Current Loop and 1EE488 are typical standard interf.docxacarolyn
 
RADIUS provides three services- authentication- authorization- and acc.docx
RADIUS provides three services- authentication- authorization- and acc.docxRADIUS provides three services- authentication- authorization- and acc.docx
RADIUS provides three services- authentication- authorization- and acc.docxacarolyn
 
Question- Federal funding agencies must form committees to decide whic.docx
Question- Federal funding agencies must form committees to decide whic.docxQuestion- Federal funding agencies must form committees to decide whic.docx
Question- Federal funding agencies must form committees to decide whic.docxacarolyn
 

More from acarolyn (18)

Research the issues that you might face as a network administrator whe.docx
Research the issues that you might face as a network administrator whe.docxResearch the issues that you might face as a network administrator whe.docx
Research the issues that you might face as a network administrator whe.docx
 
Reset Help By the Brnsted-Lowry definition- acids are proton donors an.docx
Reset Help By the Brnsted-Lowry definition- acids are proton donors an.docxReset Help By the Brnsted-Lowry definition- acids are proton donors an.docx
Reset Help By the Brnsted-Lowry definition- acids are proton donors an.docx
 
SECTION III- SHORT ANSWER (10 possible points) Please give a brief ans.docx
SECTION III- SHORT ANSWER (10 possible points) Please give a brief ans.docxSECTION III- SHORT ANSWER (10 possible points) Please give a brief ans.docx
SECTION III- SHORT ANSWER (10 possible points) Please give a brief ans.docx
 
Select a specific event that has recently occurred (within the last 30.docx
Select a specific event that has recently occurred (within the last 30.docxSelect a specific event that has recently occurred (within the last 30.docx
Select a specific event that has recently occurred (within the last 30.docx
 
Security Breaches and the Six Dumb Ideas Consider a recent (2014- 2015.docx
Security Breaches and the Six Dumb Ideas Consider a recent (2014- 2015.docxSecurity Breaches and the Six Dumb Ideas Consider a recent (2014- 2015.docx
Security Breaches and the Six Dumb Ideas Consider a recent (2014- 2015.docx
 
Scientifically continuous phase apersed phase Speaking Chapter Review.docx
Scientifically continuous phase apersed phase Speaking Chapter Review.docxScientifically continuous phase apersed phase Speaking Chapter Review.docx
Scientifically continuous phase apersed phase Speaking Chapter Review.docx
 
Scenario- You are the Accountant for WanneBee Corporation WannaBee Cor.docx
Scenario- You are the Accountant for WanneBee Corporation WannaBee Cor.docxScenario- You are the Accountant for WanneBee Corporation WannaBee Cor.docx
Scenario- You are the Accountant for WanneBee Corporation WannaBee Cor.docx
 
Scenario Allen Manesfield- Jenny Winters- and John Jacobsen have been.docx
Scenario Allen Manesfield- Jenny Winters- and John Jacobsen have been.docxScenario Allen Manesfield- Jenny Winters- and John Jacobsen have been.docx
Scenario Allen Manesfield- Jenny Winters- and John Jacobsen have been.docx
 
S10-2 (Learning Objective 1- Describe the authority structure in a cor.docx
S10-2 (Learning Objective 1- Describe the authority structure in a cor.docxS10-2 (Learning Objective 1- Describe the authority structure in a cor.docx
S10-2 (Learning Objective 1- Describe the authority structure in a cor.docx
 
rovide 34 paragraphs that define how the IT security landscape has evo.docx
rovide 34 paragraphs that define how the IT security landscape has evo.docxrovide 34 paragraphs that define how the IT security landscape has evo.docx
rovide 34 paragraphs that define how the IT security landscape has evo.docx
 
Regarding intermolecular forces Sometimes London dispersion forces ou.docx
Regarding intermolecular forces  Sometimes London dispersion forces ou.docxRegarding intermolecular forces  Sometimes London dispersion forces ou.docx
Regarding intermolecular forces Sometimes London dispersion forces ou.docx
 
Questions 12-14 refer to the following aqueous solutions- (A) 1 M BaCl.docx
Questions 12-14 refer to the following aqueous solutions- (A) 1 M BaCl.docxQuestions 12-14 refer to the following aqueous solutions- (A) 1 M BaCl.docx
Questions 12-14 refer to the following aqueous solutions- (A) 1 M BaCl.docx
 
Reed Pentak a finance major has been following globalization and made.docx
Reed Pentak a finance major has been following globalization and made.docxReed Pentak a finance major has been following globalization and made.docx
Reed Pentak a finance major has been following globalization and made.docx
 
Redesign- Make a prediction relating heat to the movement of molecules.docx
Redesign- Make a prediction relating heat to the movement of molecules.docxRedesign- Make a prediction relating heat to the movement of molecules.docx
Redesign- Make a prediction relating heat to the movement of molecules.docx
 
Recycling and composting are important parts of an overall management.docx
Recycling and composting are important parts of an overall management.docxRecycling and composting are important parts of an overall management.docx
Recycling and composting are important parts of an overall management.docx
 
R5232- RS422- 20mA Current Loop and 1EE488 are typical standard interf.docx
R5232- RS422- 20mA Current Loop and 1EE488 are typical standard interf.docxR5232- RS422- 20mA Current Loop and 1EE488 are typical standard interf.docx
R5232- RS422- 20mA Current Loop and 1EE488 are typical standard interf.docx
 
RADIUS provides three services- authentication- authorization- and acc.docx
RADIUS provides three services- authentication- authorization- and acc.docxRADIUS provides three services- authentication- authorization- and acc.docx
RADIUS provides three services- authentication- authorization- and acc.docx
 
Question- Federal funding agencies must form committees to decide whic.docx
Question- Federal funding agencies must form committees to decide whic.docxQuestion- Federal funding agencies must form committees to decide whic.docx
Question- Federal funding agencies must form committees to decide whic.docx
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

reverse the linked list (2-4-8-10) by- stack- iteration- recursion- U.docx

  • 1. reverse the linked list (2,4,8,10) by: stack; iteration; recursion. Using C++ Solution #include<iostream> #include<stdio.h> #include<stdlib.h> #includ<stack> using namespace std; struct Node { int value; struct Node* next; }node; struct Node* reverse(struct Node* head) {Â Â // Iteration Method struct Node* current,*prev; current = head;
  • 2. prev = NULL; while(current!=NULL) { next = current->next; current->next = prev; prev = current; current = next; } head = prev;- return head; } struct Node* insert(Node* head,int value) { Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->value = value; temp->next = NULL; if(head = = NULL) head = temp; else { Node*temp1=head; while(temp1->next!=NULL) temp1 = temp1->next; temp1->next = temp; } return head; } public void print(Node* head) { while(head!=NULL) {
  • 3. cout<<head->value; head = head->next; } } struct Node*recursiveReverse(struct Node* &head) {Â Â //Recursion method if(head==NULL || head->next ==NULL) return; Node*first = head; Node *rest = head->next; if(rest !=NULL) return; recursiveReverse(rest); first ->next->next = first; first->next =NULL; head = rest; return head; } void Reverse(struct Node* head)Â Â // stack Implementation { stack<node*> Stack; node *traverse = head; while(traverse!=NULL) { Stack.push(traverse); traverse = traverse->next;Â Â } node *temp = Stack.top(); head = temp; Stack.pop();
  • 4. while(!Stack.empty()) { temp->next = Stack.top(); Stack.pop(); temp = temp->next;Â Â } temp->next = NULL; return Stack; } int main() { struct Node* head =NULL; head = insert(head,2); head = insert(head,4); head = insert(head,8); head = insert(head,10); print(head); head= reverse(head); print(head); head = recursiveReverse(head); print(head); head = void Reverse(head); void print(head); return 0; }