SlideShare a Scribd company logo
1 of 2
Download to read offline
Sorting with Linked-List
Write a c++ program to read a series of integers from the user. When End-of-File (Ctrl + D) is
encountered, the program output those numbers which have been sorted in ascending order.
For example, the input is 1 3 5 7 2 4 6, the output will be 1 2 3 4 5 6 7.
Hint: When you insert a value into a linked-list, you need not always add in the front or at the
back of the list. You may insert in a proper position.
main.cpp is given as
#include <iostream>
#include "listElement.h"
using std::cin;
ListElement*
insertToList(ListElement* pList, int v);
void printList(ListElement* p);
int main() {
ListElement* pList = NULL;
int n;
while (cin >> n) {
if (n <= 0) break;
pList = insertToList(pList, n);
}
printList(pList);
return 0;
}
printList.cpp is given as
#include <iostream>
#include "listElement.h"
void printList(ListElement* p) {
while (p != NULL) {
std::cout << p->value << ' ';
p = p->pNext;
}
std::cout << std::endl;
}
listElement.h is given as
struct ListElement {
int value; // value of an element
ListElement* pNext; // Pointer to a list element
};
Note:
You should not modify the contents of either main.cpp or printList.cpp.
All you need to do is preparing a .cpp file which contains the definition of the insertToList()
function.

More Related Content

Similar to Sorting with Linked-List Write a c++ program to read a series of integ.pdf

Create a C program which auto-completes suggestions when beginning t.pdf
Create a C program which auto-completes suggestions when beginning t.pdfCreate a C program which auto-completes suggestions when beginning t.pdf
Create a C program which auto-completes suggestions when beginning t.pdf
fedosys
 
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
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
Dmitri Nesteruk
 
816 LAB Playlist output linked list C++ Given main .pdf
816 LAB Playlist output linked list C++ Given main .pdf816 LAB Playlist output linked list C++ Given main .pdf
816 LAB Playlist output linked list C++ Given main .pdf
aarokyaaqua
 
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdfIn C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
shreeaadithyaacellso
 
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxlab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
DIPESH30
 
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
 
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxlab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
DIPESH30
 
introductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdfintroductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdf
SameerKhanPathan7
 

Similar to Sorting with Linked-List Write a c++ program to read a series of integ.pdf (20)

Create a C program which auto-completes suggestions when beginning t.pdf
Create a C program which auto-completes suggestions when beginning t.pdfCreate a C program which auto-completes suggestions when beginning t.pdf
Create a C program which auto-completes suggestions when beginning t.pdf
 
Tut1
Tut1Tut1
Tut1
 
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
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
 
How c/c++ works
How c/c++ worksHow c/c++ works
How c/c++ works
 
816 LAB Playlist output linked list C++ Given main .pdf
816 LAB Playlist output linked list C++ Given main .pdf816 LAB Playlist output linked list C++ Given main .pdf
816 LAB Playlist output linked list C++ Given main .pdf
 
Files
FilesFiles
Files
 
(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt
 
Functions
FunctionsFunctions
Functions
 
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdfIn C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docxlab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
lab03build.bat@echo offclsset DRIVE_LETTER=1set.docx
 
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
 
Python Basics
Python BasicsPython Basics
Python Basics
 
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxlab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
 
introductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdfintroductiontoc-140704113737-phpapp01.pdf
introductiontoc-140704113737-phpapp01.pdf
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 

More from john344

SRG 119 HW 6- MICROBIAL GROWTH Complete the cross word by hand- scan i.pdf
SRG 119 HW 6- MICROBIAL GROWTH Complete the cross word by hand- scan i.pdfSRG 119 HW 6- MICROBIAL GROWTH Complete the cross word by hand- scan i.pdf
SRG 119 HW 6- MICROBIAL GROWTH Complete the cross word by hand- scan i.pdf
john344
 

More from john344 (20)

Some microbes can tolerate oxygen while others cannot- What do organis.pdf
Some microbes can tolerate oxygen while others cannot- What do organis.pdfSome microbes can tolerate oxygen while others cannot- What do organis.pdf
Some microbes can tolerate oxygen while others cannot- What do organis.pdf
 
st-Lab Assesment 8B Question 2.pdf
st-Lab Assesment 8B Question 2.pdfst-Lab Assesment 8B Question 2.pdf
st-Lab Assesment 8B Question 2.pdf
 
SQL is sometimes referred to as the most popular programming language.pdf
SQL is sometimes referred to as the most popular programming language.pdfSQL is sometimes referred to as the most popular programming language.pdf
SQL is sometimes referred to as the most popular programming language.pdf
 
SRG 119 HW 6- MICROBIAL GROWTH Complete the cross word by hand- scan i.pdf
SRG 119 HW 6- MICROBIAL GROWTH Complete the cross word by hand- scan i.pdfSRG 119 HW 6- MICROBIAL GROWTH Complete the cross word by hand- scan i.pdf
SRG 119 HW 6- MICROBIAL GROWTH Complete the cross word by hand- scan i.pdf
 
SQL A bookstore wants to stock mystery stories from particular publish.pdf
SQL A bookstore wants to stock mystery stories from particular publish.pdfSQL A bookstore wants to stock mystery stories from particular publish.pdf
SQL A bookstore wants to stock mystery stories from particular publish.pdf
 
Spud really likes potatoes- At best any other goods are worth less tha.pdf
Spud really likes potatoes- At best any other goods are worth less tha.pdfSpud really likes potatoes- At best any other goods are worth less tha.pdf
Spud really likes potatoes- At best any other goods are worth less tha.pdf
 
split(self) (3 points) Divides the original list in half- returning tw.pdf
split(self) (3 points) Divides the original list in half- returning tw.pdfsplit(self) (3 points) Divides the original list in half- returning tw.pdf
split(self) (3 points) Divides the original list in half- returning tw.pdf
 
Spell out the abbreviation then discuss purpose- use- and structure of.pdf
Spell out the abbreviation then discuss purpose- use- and structure of.pdfSpell out the abbreviation then discuss purpose- use- and structure of.pdf
Spell out the abbreviation then discuss purpose- use- and structure of.pdf
 
Spinal nerves and pathways of information- Define-describe the terms b.pdf
Spinal nerves and pathways of information- Define-describe the terms b.pdfSpinal nerves and pathways of information- Define-describe the terms b.pdf
Spinal nerves and pathways of information- Define-describe the terms b.pdf
 
Sphinxes are usually red- A rare purple color is recessive in both Asi.pdf
Sphinxes are usually red- A rare purple color is recessive in both Asi.pdfSphinxes are usually red- A rare purple color is recessive in both Asi.pdf
Sphinxes are usually red- A rare purple color is recessive in both Asi.pdf
 
Space --- the final frontier- So- as I am sure everyone knows- the Nat.pdf
Space --- the final frontier- So- as I am sure everyone knows- the Nat.pdfSpace --- the final frontier- So- as I am sure everyone knows- the Nat.pdf
Space --- the final frontier- So- as I am sure everyone knows- the Nat.pdf
 
Solve the problem and show the working Use the ones complement to fin.pdf
Solve the problem and show the working  Use the ones complement to fin.pdfSolve the problem and show the working  Use the ones complement to fin.pdf
Solve the problem and show the working Use the ones complement to fin.pdf
 
Southeastern Oklahoma State University's business program has the faci.pdf
Southeastern Oklahoma State University's business program has the faci.pdfSoutheastern Oklahoma State University's business program has the faci.pdf
Southeastern Oklahoma State University's business program has the faci.pdf
 
Sophia and Dorcas are sisters who share the same room- The room can ea.pdf
Sophia and Dorcas are sisters who share the same room- The room can ea.pdfSophia and Dorcas are sisters who share the same room- The room can ea.pdf
Sophia and Dorcas are sisters who share the same room- The room can ea.pdf
 
sorry for the spacing on the second half 6- Larke volvmers of nucleoti.pdf
sorry for the spacing on the second half 6- Larke volvmers of nucleoti.pdfsorry for the spacing on the second half 6- Larke volvmers of nucleoti.pdf
sorry for the spacing on the second half 6- Larke volvmers of nucleoti.pdf
 
Song saa private island- Cambodia has madesignificant progress in-sust (1).pdf
Song saa private island- Cambodia has madesignificant progress in-sust (1).pdfSong saa private island- Cambodia has madesignificant progress in-sust (1).pdf
Song saa private island- Cambodia has madesignificant progress in-sust (1).pdf
 
Someone who is an Select one- a- entrepreneurial manager b- immoral ma.pdf
Someone who is an Select one- a- entrepreneurial manager b- immoral ma.pdfSomeone who is an Select one- a- entrepreneurial manager b- immoral ma.pdf
Someone who is an Select one- a- entrepreneurial manager b- immoral ma.pdf
 
some ways in which our economies are dependent on environmental servic.pdf
some ways in which our economies are dependent on environmental servic.pdfsome ways in which our economies are dependent on environmental servic.pdf
some ways in which our economies are dependent on environmental servic.pdf
 
Some strains of the bacteria Escherichia coli readily cause bacteria U.pdf
Some strains of the bacteria Escherichia coli readily cause bacteria U.pdfSome strains of the bacteria Escherichia coli readily cause bacteria U.pdf
Some strains of the bacteria Escherichia coli readily cause bacteria U.pdf
 
Some sweet pea plants have purple flowers and other plants have white.pdf
Some sweet pea plants have purple flowers and other plants have white.pdfSome sweet pea plants have purple flowers and other plants have white.pdf
Some sweet pea plants have purple flowers and other plants have white.pdf
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Sorting with Linked-List Write a c++ program to read a series of integ.pdf

  • 1. Sorting with Linked-List Write a c++ program to read a series of integers from the user. When End-of-File (Ctrl + D) is encountered, the program output those numbers which have been sorted in ascending order. For example, the input is 1 3 5 7 2 4 6, the output will be 1 2 3 4 5 6 7. Hint: When you insert a value into a linked-list, you need not always add in the front or at the back of the list. You may insert in a proper position. main.cpp is given as #include <iostream> #include "listElement.h" using std::cin; ListElement* insertToList(ListElement* pList, int v); void printList(ListElement* p); int main() { ListElement* pList = NULL; int n; while (cin >> n) { if (n <= 0) break; pList = insertToList(pList, n); } printList(pList); return 0; } printList.cpp is given as #include <iostream> #include "listElement.h" void printList(ListElement* p) { while (p != NULL) { std::cout << p->value << ' '; p = p->pNext; } std::cout << std::endl; } listElement.h is given as
  • 2. struct ListElement { int value; // value of an element ListElement* pNext; // Pointer to a list element }; Note: You should not modify the contents of either main.cpp or printList.cpp. All you need to do is preparing a .cpp file which contains the definition of the insertToList() function.