SlideShare a Scribd company logo
1 of 2
Download to read offline
please help me in C++
Objective: Create a singly linked list of numbers based upon user input.
Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat
until they enter -1 for the number. .
Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10
Solution
#include
#include
using namespace std;
// It contains the value and the pointer to the next element in the list.
struct element {
int value;
struct element *next;
};
struct element *head = NULL;
// The list is stored in the object of this class.
class linkedlist {
public:
// To display list, we first see if head is NULL,
// If head is NULL it means the list is empty.
// Then we traverse along the list and display each item, until the next pointer is NULL.
void display() {
if(head == NULL){
cout << "The list is empty ";
} else {
struct element *temp;
temp = head;
while(temp != NULL){
cout << temp->value << " " ;
temp = temp->next;
}
cout << " ";
}
}
// To add an element to the list,create a newElement and store the value.
// If head is NULL, add the newElement to the head.
// To add an element at the starting of the list.
// The next pointer of the newElement must point to the old head.
// The new head must be pointed to the newElement.
void add(int a){
struct element *newElement;
newElement = (struct element *)malloc(sizeof(struct element));
newElement->value = a;
newElement->next = NULL;
if(head == NULL) {
head = newElement;
} else {
newElement->next = head;
head = newElement;
}
}
};
int main()
{
// Create an object of the class linkedlist.
linkedlist l = linkedlist();
int a;
do {
cout << "Enter a number : ";
cin >> a;
if(a!=-1) { // If the number is -1 do not add it to the list.
l.add(a); // Add the number to the list.
}
l.display(); // Display the list.
} while(a!=-1); // Keep asking for a number until -1 is pressed.
return 0;
}

More Related Content

Similar to please help me in C++Objective Create a singly linked list of num.pdf

Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfnitinarora01
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
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.pdfpoblettesedanoree498
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdfdeepakangel
 
10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docxtodd991
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
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 .pdfsudhinjv
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfezycolours78
 
Im creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfIm creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfarkmuzikllc
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfarorastores
 
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.pdfcallawaycorb73779
 
please i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdfplease i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdfezonesolutions
 
I have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfI have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfarkmuzikllc
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfudit652068
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfannaelctronics
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfeyewaregallery
 
Write an algorithm that reads a list of integers from the keyboard, .pdf
Write an algorithm that reads a list of integers from the keyboard, .pdfWrite an algorithm that reads a list of integers from the keyboard, .pdf
Write an algorithm that reads a list of integers from the keyboard, .pdfArrowdeepak
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfUsing the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfmallik3000
 
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfHomework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfezzi97
 

Similar to please help me in C++Objective Create a singly linked list of num.pdf (20)

Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
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
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
 
10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx10- Consider the following data structure used for implementing a link.docx
10- Consider the following data structure used for implementing a link.docx
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
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
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
Im creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdfIm creating a skip list program for my Algorithms and Advanced Data.pdf
Im creating a skip list program for my Algorithms and Advanced Data.pdf
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
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
 
please i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdfplease i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdf
 
I have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfI have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdf
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdf
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdfin C++ , Design a linked list class named IntegerList to hold a seri.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
 
Write an algorithm that reads a list of integers from the keyboard, .pdf
Write an algorithm that reads a list of integers from the keyboard, .pdfWrite an algorithm that reads a list of integers from the keyboard, .pdf
Write an algorithm that reads a list of integers from the keyboard, .pdf
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfUsing the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdf
 
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfHomework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
 

More from aminbijal86

can someone proofread this for meAs a child, I used to watch T.V..pdf
can someone proofread this for meAs a child, I used to watch T.V..pdfcan someone proofread this for meAs a child, I used to watch T.V..pdf
can someone proofread this for meAs a child, I used to watch T.V..pdfaminbijal86
 
Describe the arrangement of Regulations for lifting machines and acc.pdf
Describe the arrangement of Regulations for lifting machines and acc.pdfDescribe the arrangement of Regulations for lifting machines and acc.pdf
Describe the arrangement of Regulations for lifting machines and acc.pdfaminbijal86
 
All of the following would require use of the equity method for inve.pdf
All of the following would require use of the equity method for inve.pdfAll of the following would require use of the equity method for inve.pdf
All of the following would require use of the equity method for inve.pdfaminbijal86
 
A nurse palpated enlarged lymph nodes. Describe signs and symptoms t.pdf
A nurse palpated enlarged lymph nodes. Describe signs and symptoms t.pdfA nurse palpated enlarged lymph nodes. Describe signs and symptoms t.pdf
A nurse palpated enlarged lymph nodes. Describe signs and symptoms t.pdfaminbijal86
 
Bell & Porter has the following average times (in hours) Inspecting .pdf
Bell & Porter has the following average times (in hours) Inspecting .pdfBell & Porter has the following average times (in hours) Inspecting .pdf
Bell & Porter has the following average times (in hours) Inspecting .pdfaminbijal86
 
Which part of the clavicle is most commonly fractured A. Later.pdf
Which part of the clavicle is most commonly fractured A. Later.pdfWhich part of the clavicle is most commonly fractured A. Later.pdf
Which part of the clavicle is most commonly fractured A. Later.pdfaminbijal86
 
Which of the following is NOT a criticism of the National Bureau of .pdf
Which of the following is NOT a criticism of the National Bureau of .pdfWhich of the following is NOT a criticism of the National Bureau of .pdf
Which of the following is NOT a criticism of the National Bureau of .pdfaminbijal86
 
When working with communications, is it more important to detect or .pdf
When working with communications, is it more important to detect or .pdfWhen working with communications, is it more important to detect or .pdf
When working with communications, is it more important to detect or .pdfaminbijal86
 
Which of the following are principles of the AICPA Code of Professio.pdf
Which of the following are principles of the AICPA Code of Professio.pdfWhich of the following are principles of the AICPA Code of Professio.pdf
Which of the following are principles of the AICPA Code of Professio.pdfaminbijal86
 
Thouhgts on the future of information technology (IT) software secur.pdf
Thouhgts on the future of information technology (IT) software secur.pdfThouhgts on the future of information technology (IT) software secur.pdf
Thouhgts on the future of information technology (IT) software secur.pdfaminbijal86
 
Two of the following ions depend on their conversion into a gas .pdf
Two of the following ions depend on their conversion into a gas .pdfTwo of the following ions depend on their conversion into a gas .pdf
Two of the following ions depend on their conversion into a gas .pdfaminbijal86
 
The systemic acquired resistance (SAR) demonstrated in the experimen.pdf
The systemic acquired resistance (SAR) demonstrated in the experimen.pdfThe systemic acquired resistance (SAR) demonstrated in the experimen.pdf
The systemic acquired resistance (SAR) demonstrated in the experimen.pdfaminbijal86
 
One implication of the Lyon hypothesis was that adult females would b.pdf
One implication of the Lyon hypothesis was that adult females would b.pdfOne implication of the Lyon hypothesis was that adult females would b.pdf
One implication of the Lyon hypothesis was that adult females would b.pdfaminbijal86
 
the problems of vietnam in communication during international busine.pdf
the problems of vietnam in communication during international busine.pdfthe problems of vietnam in communication during international busine.pdf
the problems of vietnam in communication during international busine.pdfaminbijal86
 
The first signs of niacin deficiency are all of the following EXCEPT .pdf
The first signs of niacin deficiency are all of the following EXCEPT .pdfThe first signs of niacin deficiency are all of the following EXCEPT .pdf
The first signs of niacin deficiency are all of the following EXCEPT .pdfaminbijal86
 
The Ba(s) gets formed after reductionBa2+ + 2e- -- Ba(s) which is.pdf
The Ba(s) gets formed after reductionBa2+ + 2e- -- Ba(s) which is.pdfThe Ba(s) gets formed after reductionBa2+ + 2e- -- Ba(s) which is.pdf
The Ba(s) gets formed after reductionBa2+ + 2e- -- Ba(s) which is.pdfaminbijal86
 
Submit a 2-3 page paper addressing each of the following 1.Define .pdf
Submit a 2-3 page paper addressing each of the following 1.Define .pdfSubmit a 2-3 page paper addressing each of the following 1.Define .pdf
Submit a 2-3 page paper addressing each of the following 1.Define .pdfaminbijal86
 
Nessus is a network security toolIn a pragraph describe the tool’s.pdf
Nessus is a network security toolIn a pragraph describe the tool’s.pdfNessus is a network security toolIn a pragraph describe the tool’s.pdf
Nessus is a network security toolIn a pragraph describe the tool’s.pdfaminbijal86
 
Solve given LP problem using simplex method and find maximum value o.pdf
Solve given LP problem using simplex method and find maximum value o.pdfSolve given LP problem using simplex method and find maximum value o.pdf
Solve given LP problem using simplex method and find maximum value o.pdfaminbijal86
 
Show a rightmost derivation for the string(id + id) id Usin.pdf
Show a rightmost derivation for the string(id + id)  id Usin.pdfShow a rightmost derivation for the string(id + id)  id Usin.pdf
Show a rightmost derivation for the string(id + id) id Usin.pdfaminbijal86
 

More from aminbijal86 (20)

can someone proofread this for meAs a child, I used to watch T.V..pdf
can someone proofread this for meAs a child, I used to watch T.V..pdfcan someone proofread this for meAs a child, I used to watch T.V..pdf
can someone proofread this for meAs a child, I used to watch T.V..pdf
 
Describe the arrangement of Regulations for lifting machines and acc.pdf
Describe the arrangement of Regulations for lifting machines and acc.pdfDescribe the arrangement of Regulations for lifting machines and acc.pdf
Describe the arrangement of Regulations for lifting machines and acc.pdf
 
All of the following would require use of the equity method for inve.pdf
All of the following would require use of the equity method for inve.pdfAll of the following would require use of the equity method for inve.pdf
All of the following would require use of the equity method for inve.pdf
 
A nurse palpated enlarged lymph nodes. Describe signs and symptoms t.pdf
A nurse palpated enlarged lymph nodes. Describe signs and symptoms t.pdfA nurse palpated enlarged lymph nodes. Describe signs and symptoms t.pdf
A nurse palpated enlarged lymph nodes. Describe signs and symptoms t.pdf
 
Bell & Porter has the following average times (in hours) Inspecting .pdf
Bell & Porter has the following average times (in hours) Inspecting .pdfBell & Porter has the following average times (in hours) Inspecting .pdf
Bell & Porter has the following average times (in hours) Inspecting .pdf
 
Which part of the clavicle is most commonly fractured A. Later.pdf
Which part of the clavicle is most commonly fractured A. Later.pdfWhich part of the clavicle is most commonly fractured A. Later.pdf
Which part of the clavicle is most commonly fractured A. Later.pdf
 
Which of the following is NOT a criticism of the National Bureau of .pdf
Which of the following is NOT a criticism of the National Bureau of .pdfWhich of the following is NOT a criticism of the National Bureau of .pdf
Which of the following is NOT a criticism of the National Bureau of .pdf
 
When working with communications, is it more important to detect or .pdf
When working with communications, is it more important to detect or .pdfWhen working with communications, is it more important to detect or .pdf
When working with communications, is it more important to detect or .pdf
 
Which of the following are principles of the AICPA Code of Professio.pdf
Which of the following are principles of the AICPA Code of Professio.pdfWhich of the following are principles of the AICPA Code of Professio.pdf
Which of the following are principles of the AICPA Code of Professio.pdf
 
Thouhgts on the future of information technology (IT) software secur.pdf
Thouhgts on the future of information technology (IT) software secur.pdfThouhgts on the future of information technology (IT) software secur.pdf
Thouhgts on the future of information technology (IT) software secur.pdf
 
Two of the following ions depend on their conversion into a gas .pdf
Two of the following ions depend on their conversion into a gas .pdfTwo of the following ions depend on their conversion into a gas .pdf
Two of the following ions depend on their conversion into a gas .pdf
 
The systemic acquired resistance (SAR) demonstrated in the experimen.pdf
The systemic acquired resistance (SAR) demonstrated in the experimen.pdfThe systemic acquired resistance (SAR) demonstrated in the experimen.pdf
The systemic acquired resistance (SAR) demonstrated in the experimen.pdf
 
One implication of the Lyon hypothesis was that adult females would b.pdf
One implication of the Lyon hypothesis was that adult females would b.pdfOne implication of the Lyon hypothesis was that adult females would b.pdf
One implication of the Lyon hypothesis was that adult females would b.pdf
 
the problems of vietnam in communication during international busine.pdf
the problems of vietnam in communication during international busine.pdfthe problems of vietnam in communication during international busine.pdf
the problems of vietnam in communication during international busine.pdf
 
The first signs of niacin deficiency are all of the following EXCEPT .pdf
The first signs of niacin deficiency are all of the following EXCEPT .pdfThe first signs of niacin deficiency are all of the following EXCEPT .pdf
The first signs of niacin deficiency are all of the following EXCEPT .pdf
 
The Ba(s) gets formed after reductionBa2+ + 2e- -- Ba(s) which is.pdf
The Ba(s) gets formed after reductionBa2+ + 2e- -- Ba(s) which is.pdfThe Ba(s) gets formed after reductionBa2+ + 2e- -- Ba(s) which is.pdf
The Ba(s) gets formed after reductionBa2+ + 2e- -- Ba(s) which is.pdf
 
Submit a 2-3 page paper addressing each of the following 1.Define .pdf
Submit a 2-3 page paper addressing each of the following 1.Define .pdfSubmit a 2-3 page paper addressing each of the following 1.Define .pdf
Submit a 2-3 page paper addressing each of the following 1.Define .pdf
 
Nessus is a network security toolIn a pragraph describe the tool’s.pdf
Nessus is a network security toolIn a pragraph describe the tool’s.pdfNessus is a network security toolIn a pragraph describe the tool’s.pdf
Nessus is a network security toolIn a pragraph describe the tool’s.pdf
 
Solve given LP problem using simplex method and find maximum value o.pdf
Solve given LP problem using simplex method and find maximum value o.pdfSolve given LP problem using simplex method and find maximum value o.pdf
Solve given LP problem using simplex method and find maximum value o.pdf
 
Show a rightmost derivation for the string(id + id) id Usin.pdf
Show a rightmost derivation for the string(id + id)  id Usin.pdfShow a rightmost derivation for the string(id + id)  id Usin.pdf
Show a rightmost derivation for the string(id + id) id Usin.pdf
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
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
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
_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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
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
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
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
 
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
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
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
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
_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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
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
 
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
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
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
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
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
 
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
 

please help me in C++Objective Create a singly linked list of num.pdf

  • 1. please help me in C++ Objective: Create a singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10 Solution #include #include using namespace std; // It contains the value and the pointer to the next element in the list. struct element { int value; struct element *next; }; struct element *head = NULL; // The list is stored in the object of this class. class linkedlist { public: // To display list, we first see if head is NULL, // If head is NULL it means the list is empty. // Then we traverse along the list and display each item, until the next pointer is NULL. void display() { if(head == NULL){ cout << "The list is empty "; } else { struct element *temp; temp = head; while(temp != NULL){ cout << temp->value << " " ; temp = temp->next; } cout << " "; }
  • 2. } // To add an element to the list,create a newElement and store the value. // If head is NULL, add the newElement to the head. // To add an element at the starting of the list. // The next pointer of the newElement must point to the old head. // The new head must be pointed to the newElement. void add(int a){ struct element *newElement; newElement = (struct element *)malloc(sizeof(struct element)); newElement->value = a; newElement->next = NULL; if(head == NULL) { head = newElement; } else { newElement->next = head; head = newElement; } } }; int main() { // Create an object of the class linkedlist. linkedlist l = linkedlist(); int a; do { cout << "Enter a number : "; cin >> a; if(a!=-1) { // If the number is -1 do not add it to the list. l.add(a); // Add the number to the list. } l.display(); // Display the list. } while(a!=-1); // Keep asking for a number until -1 is pressed. return 0; }