SlideShare a Scribd company logo
Write an algorithm that reads a list of integers from the keyboard, creates
a list of them using linked list implementation, and prints the result
Solution
/*
Algorithm:
Implementation of linked list.
Create a node type class which will hold an integer and next pointer to point to another node.
Create a constructor which will construct an object of type node with the integer type data as the
only parameter
and initializing next pointer to NULL.
Create LinkedList class which will only have head pointer.
Create a constructor which will initialize this head pointer to NULL.
Create a append method which will recieve an input of type integer from the user and append it
to LinkedList
using below steps:
1.Create a Node type object.
1.IF head is NULL assign a Node type object created in 1st step.
2.IF head is not NULL then traverse to the end of the linked list till you found a Node which has
next pointer
set to NULL.
3.Add Node type object created in first step to the next pointer of last node.
Create a print method which will traverse the list from the head of the linked list to the last
node(Node
with next pointer NULL) and while traversing it will keep printing the node data.
Get the userinput:
Ask user for integer continously till user input -1 and append all the integers to the linked list.
*/
PROGRAM:
#include
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
class LinkedList
{
private:
Node *head;
public:
LinkedList()
{
head = NULL;
}
void addToFront(int data)
{
Node *temp = new Node(data);
if(head == NULL)
head = temp;
else
{
temp->next = head;
head = temp;
}
}
void append(int data)
{
Node *temp = new Node(data);
Node *current = head;
if(current == NULL)
{
head = temp;
return;
}
while(current->next != NULL)
{
current = current->next;
}
current->next = temp;
}
void print()
{
Node *current = head;
if(current == NULL)
cout << "No element in list.  ";
while(current != NULL)
{
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
void deleteLinkedList()
{
Node *prev;
Node *current = head;
while(current!=NULL)
{
prev = current;
current = current->next;
delete prev;
}
}
void deleteLastNode()
{
Node *current = head;
Node *prev = NULL;
if(current == NULL)
return;
while(current->next != NULL)
{
prev = current;
current = current->next;
}
if(prev == NULL)
{
delete current;
head = NULL;
}
else
{
prev->next = NULL;
delete current;
}
}
~LinkedList()
{
deleteLinkedList();
}
};
int main()
{
LinkedList list;
int data;
while(1)
{
cout << "Enter the integer(-1 to Quit) : ";
cin >> data;
if(data==-1)
break;
list.append(data);
}
cout << "Content of LinkedList is : ";
list.print();
return 0;
}
OUTPUT:
Enter the integer(-1 to Quit) : 1
Enter the integer(-1 to Quit) : 2
Enter the integer(-1 to Quit) : 3
Enter the integer(-1 to Quit) : 4
Enter the integer(-1 to Quit) : -1
Content of LinkedList is : 1 2 3 4
Ad

More Related Content

Similar to Write an algorithm that reads a list of integers from the keyboard, .pdf (20)

C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
poblettesedanoree498
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
Programming Homework Help
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
sudhinjv
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
Masud Parvaze
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
callawaycorb73779
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
arnold 7490
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
C++ Please test your program before you submit the answer.pdf
C++ Please test your program before you submit the answer.pdfC++ Please test your program before you submit the answer.pdf
C++ Please test your program before you submit the answer.pdf
aashisha5
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
fortmdu
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdf
aminbijal86
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
Himadri Sen Gupta
 
File Type cppAdd the following to your linked list of strings pro.pdf
File Type cppAdd the following to your linked list of strings pro.pdfFile Type cppAdd the following to your linked list of strings pro.pdf
File Type cppAdd the following to your linked list of strings pro.pdf
footworld1
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdf
ambersushil
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
Amit Vats
 
Linked list
Linked listLinked list
Linked list
Md. Afif Al Mamun
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
poblettesedanoree498
 
1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf1#include stdio.h#include stdlib.h#include assert.h .pdf
1#include stdio.h#include stdlib.h#include assert.h .pdf
sudhinjv
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
callawaycorb73779
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
C++ Please test your program before you submit the answer.pdf
C++ Please test your program before you submit the answer.pdfC++ Please test your program before you submit the answer.pdf
C++ Please test your program before you submit the answer.pdf
aashisha5
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
fortmdu
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdf
aminbijal86
 
File Type cppAdd the following to your linked list of strings pro.pdf
File Type cppAdd the following to your linked list of strings pro.pdfFile Type cppAdd the following to your linked list of strings pro.pdf
File Type cppAdd the following to your linked list of strings pro.pdf
footworld1
 
Java AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdfJava AssignmentUsing the ListNode.java file below Write method.pdf
Java AssignmentUsing the ListNode.java file below Write method.pdf
ambersushil
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
Amit Vats
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 

More from Arrowdeepak (20)

Firefly luciferase is a commonly used reporter gene. The gene origina.pdf
Firefly luciferase is a commonly used reporter gene. The gene origina.pdfFirefly luciferase is a commonly used reporter gene. The gene origina.pdf
Firefly luciferase is a commonly used reporter gene. The gene origina.pdf
Arrowdeepak
 
Find all hazards in this circuit. Redesign the circuit as a three-le.pdf
Find all hazards in this circuit.  Redesign the circuit as a three-le.pdfFind all hazards in this circuit.  Redesign the circuit as a three-le.pdf
Find all hazards in this circuit. Redesign the circuit as a three-le.pdf
Arrowdeepak
 
Fergie has the choice between investing in State of New York bond at.pdf
Fergie has the choice between investing in State of New York bond at.pdfFergie has the choice between investing in State of New York bond at.pdf
Fergie has the choice between investing in State of New York bond at.pdf
Arrowdeepak
 
Explain the “life” of a secreted protein molecule - trace the pathwa.pdf
Explain the “life” of a secreted protein molecule - trace the pathwa.pdfExplain the “life” of a secreted protein molecule - trace the pathwa.pdf
Explain the “life” of a secreted protein molecule - trace the pathwa.pdf
Arrowdeepak
 
7. Using to Table 1 to assist you, explain the rational for the diver.pdf
7. Using to Table 1 to assist you, explain the rational for the diver.pdf7. Using to Table 1 to assist you, explain the rational for the diver.pdf
7. Using to Table 1 to assist you, explain the rational for the diver.pdf
Arrowdeepak
 
A. list two vertebrae that spinal nerve t12 travels between .pdf
A. list two vertebrae that spinal nerve t12 travels between .pdfA. list two vertebrae that spinal nerve t12 travels between .pdf
A. list two vertebrae that spinal nerve t12 travels between .pdf
Arrowdeepak
 
Do people from different cultures experience emotions differently O.pdf
Do people from different cultures experience emotions differently O.pdfDo people from different cultures experience emotions differently O.pdf
Do people from different cultures experience emotions differently O.pdf
Arrowdeepak
 
Describe the basic elements found in robot controllers. (Choose all t.pdf
Describe the basic elements found in robot controllers. (Choose all t.pdfDescribe the basic elements found in robot controllers. (Choose all t.pdf
Describe the basic elements found in robot controllers. (Choose all t.pdf
Arrowdeepak
 
Consider the following probability distribution The Normal distribut.pdf
Consider the following probability distribution  The Normal distribut.pdfConsider the following probability distribution  The Normal distribut.pdf
Consider the following probability distribution The Normal distribut.pdf
Arrowdeepak
 
1. Please explain 1. What is cosolidation of a soi 2. What are the .pdf
1. Please explain 1. What is cosolidation of a soi 2. What are the .pdf1. Please explain 1. What is cosolidation of a soi 2. What are the .pdf
1. Please explain 1. What is cosolidation of a soi 2. What are the .pdf
Arrowdeepak
 
What type of gametes did Sutton predict Mendel (parental, recombin.pdf
What type of gametes did Sutton predict Mendel (parental, recombin.pdfWhat type of gametes did Sutton predict Mendel (parental, recombin.pdf
What type of gametes did Sutton predict Mendel (parental, recombin.pdf
Arrowdeepak
 
With reference to Barth (2014), discuss how the author assessed the .pdf
With reference to Barth (2014), discuss how the author assessed the .pdfWith reference to Barth (2014), discuss how the author assessed the .pdf
With reference to Barth (2014), discuss how the author assessed the .pdf
Arrowdeepak
 
1. Peter Grant - the evolutionary biologist who has studied finches .pdf
1. Peter Grant - the evolutionary biologist who has studied finches .pdf1. Peter Grant - the evolutionary biologist who has studied finches .pdf
1. Peter Grant - the evolutionary biologist who has studied finches .pdf
Arrowdeepak
 
COMMUTER PASSES Five different types of monthly commuter passes are o.pdf
COMMUTER PASSES Five different types of monthly commuter passes are o.pdfCOMMUTER PASSES Five different types of monthly commuter passes are o.pdf
COMMUTER PASSES Five different types of monthly commuter passes are o.pdf
Arrowdeepak
 
If a researcher rejects a null hypothesis when that hypothesis is ac.pdf
If a researcher rejects a null hypothesis when that hypothesis is ac.pdfIf a researcher rejects a null hypothesis when that hypothesis is ac.pdf
If a researcher rejects a null hypothesis when that hypothesis is ac.pdf
Arrowdeepak
 
Which of the following values of r allows a perfect prediction of sc.pdf
Which of the following values of r allows a perfect prediction of sc.pdfWhich of the following values of r allows a perfect prediction of sc.pdf
Which of the following values of r allows a perfect prediction of sc.pdf
Arrowdeepak
 
Which of the following is a way in which meiosis differs from mitosi.pdf
Which of the following is a way in which meiosis differs from mitosi.pdfWhich of the following is a way in which meiosis differs from mitosi.pdf
Which of the following is a way in which meiosis differs from mitosi.pdf
Arrowdeepak
 
What is the inheritance of the following pedigree X-linked recessiv.pdf
What is the inheritance of the following pedigree  X-linked recessiv.pdfWhat is the inheritance of the following pedigree  X-linked recessiv.pdf
What is the inheritance of the following pedigree X-linked recessiv.pdf
Arrowdeepak
 
What is the profitability index for an investment with the following.pdf
What is the profitability index for an investment with the following.pdfWhat is the profitability index for an investment with the following.pdf
What is the profitability index for an investment with the following.pdf
Arrowdeepak
 
What are the characteristics of each microscope 1.compound2.s.pdf
What are the characteristics of each microscope 1.compound2.s.pdfWhat are the characteristics of each microscope 1.compound2.s.pdf
What are the characteristics of each microscope 1.compound2.s.pdf
Arrowdeepak
 
Firefly luciferase is a commonly used reporter gene. The gene origina.pdf
Firefly luciferase is a commonly used reporter gene. The gene origina.pdfFirefly luciferase is a commonly used reporter gene. The gene origina.pdf
Firefly luciferase is a commonly used reporter gene. The gene origina.pdf
Arrowdeepak
 
Find all hazards in this circuit. Redesign the circuit as a three-le.pdf
Find all hazards in this circuit.  Redesign the circuit as a three-le.pdfFind all hazards in this circuit.  Redesign the circuit as a three-le.pdf
Find all hazards in this circuit. Redesign the circuit as a three-le.pdf
Arrowdeepak
 
Fergie has the choice between investing in State of New York bond at.pdf
Fergie has the choice between investing in State of New York bond at.pdfFergie has the choice between investing in State of New York bond at.pdf
Fergie has the choice between investing in State of New York bond at.pdf
Arrowdeepak
 
Explain the “life” of a secreted protein molecule - trace the pathwa.pdf
Explain the “life” of a secreted protein molecule - trace the pathwa.pdfExplain the “life” of a secreted protein molecule - trace the pathwa.pdf
Explain the “life” of a secreted protein molecule - trace the pathwa.pdf
Arrowdeepak
 
7. Using to Table 1 to assist you, explain the rational for the diver.pdf
7. Using to Table 1 to assist you, explain the rational for the diver.pdf7. Using to Table 1 to assist you, explain the rational for the diver.pdf
7. Using to Table 1 to assist you, explain the rational for the diver.pdf
Arrowdeepak
 
A. list two vertebrae that spinal nerve t12 travels between .pdf
A. list two vertebrae that spinal nerve t12 travels between .pdfA. list two vertebrae that spinal nerve t12 travels between .pdf
A. list two vertebrae that spinal nerve t12 travels between .pdf
Arrowdeepak
 
Do people from different cultures experience emotions differently O.pdf
Do people from different cultures experience emotions differently O.pdfDo people from different cultures experience emotions differently O.pdf
Do people from different cultures experience emotions differently O.pdf
Arrowdeepak
 
Describe the basic elements found in robot controllers. (Choose all t.pdf
Describe the basic elements found in robot controllers. (Choose all t.pdfDescribe the basic elements found in robot controllers. (Choose all t.pdf
Describe the basic elements found in robot controllers. (Choose all t.pdf
Arrowdeepak
 
Consider the following probability distribution The Normal distribut.pdf
Consider the following probability distribution  The Normal distribut.pdfConsider the following probability distribution  The Normal distribut.pdf
Consider the following probability distribution The Normal distribut.pdf
Arrowdeepak
 
1. Please explain 1. What is cosolidation of a soi 2. What are the .pdf
1. Please explain 1. What is cosolidation of a soi 2. What are the .pdf1. Please explain 1. What is cosolidation of a soi 2. What are the .pdf
1. Please explain 1. What is cosolidation of a soi 2. What are the .pdf
Arrowdeepak
 
What type of gametes did Sutton predict Mendel (parental, recombin.pdf
What type of gametes did Sutton predict Mendel (parental, recombin.pdfWhat type of gametes did Sutton predict Mendel (parental, recombin.pdf
What type of gametes did Sutton predict Mendel (parental, recombin.pdf
Arrowdeepak
 
With reference to Barth (2014), discuss how the author assessed the .pdf
With reference to Barth (2014), discuss how the author assessed the .pdfWith reference to Barth (2014), discuss how the author assessed the .pdf
With reference to Barth (2014), discuss how the author assessed the .pdf
Arrowdeepak
 
1. Peter Grant - the evolutionary biologist who has studied finches .pdf
1. Peter Grant - the evolutionary biologist who has studied finches .pdf1. Peter Grant - the evolutionary biologist who has studied finches .pdf
1. Peter Grant - the evolutionary biologist who has studied finches .pdf
Arrowdeepak
 
COMMUTER PASSES Five different types of monthly commuter passes are o.pdf
COMMUTER PASSES Five different types of monthly commuter passes are o.pdfCOMMUTER PASSES Five different types of monthly commuter passes are o.pdf
COMMUTER PASSES Five different types of monthly commuter passes are o.pdf
Arrowdeepak
 
If a researcher rejects a null hypothesis when that hypothesis is ac.pdf
If a researcher rejects a null hypothesis when that hypothesis is ac.pdfIf a researcher rejects a null hypothesis when that hypothesis is ac.pdf
If a researcher rejects a null hypothesis when that hypothesis is ac.pdf
Arrowdeepak
 
Which of the following values of r allows a perfect prediction of sc.pdf
Which of the following values of r allows a perfect prediction of sc.pdfWhich of the following values of r allows a perfect prediction of sc.pdf
Which of the following values of r allows a perfect prediction of sc.pdf
Arrowdeepak
 
Which of the following is a way in which meiosis differs from mitosi.pdf
Which of the following is a way in which meiosis differs from mitosi.pdfWhich of the following is a way in which meiosis differs from mitosi.pdf
Which of the following is a way in which meiosis differs from mitosi.pdf
Arrowdeepak
 
What is the inheritance of the following pedigree X-linked recessiv.pdf
What is the inheritance of the following pedigree  X-linked recessiv.pdfWhat is the inheritance of the following pedigree  X-linked recessiv.pdf
What is the inheritance of the following pedigree X-linked recessiv.pdf
Arrowdeepak
 
What is the profitability index for an investment with the following.pdf
What is the profitability index for an investment with the following.pdfWhat is the profitability index for an investment with the following.pdf
What is the profitability index for an investment with the following.pdf
Arrowdeepak
 
What are the characteristics of each microscope 1.compound2.s.pdf
What are the characteristics of each microscope 1.compound2.s.pdfWhat are the characteristics of each microscope 1.compound2.s.pdf
What are the characteristics of each microscope 1.compound2.s.pdf
Arrowdeepak
 
Ad

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
Debunking the Myths behind AI - v1, Carl Dalby
Debunking the Myths behind AI -  v1, Carl DalbyDebunking the Myths behind AI -  v1, Carl Dalby
Debunking the Myths behind AI - v1, Carl Dalby
Association for Project Management
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo SlidesHow to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo Slides
Celine George
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
How to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo SlidesHow to Add Customer Note in Odoo 18 POS - Odoo Slides
How to Add Customer Note in Odoo 18 POS - Odoo Slides
Celine George
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Ad

Write an algorithm that reads a list of integers from the keyboard, .pdf

  • 1. Write an algorithm that reads a list of integers from the keyboard, creates a list of them using linked list implementation, and prints the result Solution /* Algorithm: Implementation of linked list. Create a node type class which will hold an integer and next pointer to point to another node. Create a constructor which will construct an object of type node with the integer type data as the only parameter and initializing next pointer to NULL. Create LinkedList class which will only have head pointer. Create a constructor which will initialize this head pointer to NULL. Create a append method which will recieve an input of type integer from the user and append it to LinkedList using below steps: 1.Create a Node type object. 1.IF head is NULL assign a Node type object created in 1st step. 2.IF head is not NULL then traverse to the end of the linked list till you found a Node which has next pointer set to NULL. 3.Add Node type object created in first step to the next pointer of last node. Create a print method which will traverse the list from the head of the linked list to the last node(Node with next pointer NULL) and while traversing it will keep printing the node data. Get the userinput: Ask user for integer continously till user input -1 and append all the integers to the linked list. */ PROGRAM: #include using namespace std; class Node
  • 2. { public: int data; Node *next; Node(int data) { this->data = data; this->next = NULL; } }; class LinkedList { private: Node *head; public: LinkedList() { head = NULL; } void addToFront(int data) { Node *temp = new Node(data); if(head == NULL) head = temp; else { temp->next = head; head = temp; } } void append(int data) { Node *temp = new Node(data);
  • 3. Node *current = head; if(current == NULL) { head = temp; return; } while(current->next != NULL) { current = current->next; } current->next = temp; } void print() { Node *current = head; if(current == NULL) cout << "No element in list. "; while(current != NULL) { cout << current->data << " "; current = current->next; } cout << endl; } void deleteLinkedList() { Node *prev; Node *current = head;
  • 4. while(current!=NULL) { prev = current; current = current->next; delete prev; } } void deleteLastNode() { Node *current = head; Node *prev = NULL; if(current == NULL) return; while(current->next != NULL) { prev = current; current = current->next; } if(prev == NULL) { delete current; head = NULL; } else { prev->next = NULL; delete current; } } ~LinkedList() {
  • 5. deleteLinkedList(); } }; int main() { LinkedList list; int data; while(1) { cout << "Enter the integer(-1 to Quit) : "; cin >> data; if(data==-1) break; list.append(data); } cout << "Content of LinkedList is : "; list.print(); return 0; } OUTPUT: Enter the integer(-1 to Quit) : 1 Enter the integer(-1 to Quit) : 2 Enter the integer(-1 to Quit) : 3 Enter the integer(-1 to Quit) : 4 Enter the integer(-1 to Quit) : -1 Content of LinkedList is : 1 2 3 4