SlideShare a Scribd company logo
/* C++ Program to Implement Singly Linked List */
#include
#include
#include
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;
/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void sort();
void search();
void update();
void reverse();
void display();
single_llist()
{
start = NULL;
}
};
/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<info = value;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void single_llist::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<next;s !=NULL;s = s->next)
{
if (ptr->info > s->info)
{
value = ptr->info;
ptr->info = s->info;
s->info = value;
}
}
ptr = ptr->next;
}
}
/*
* Delete element at a given position
*/
void single_llist::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<>pos;
cout<<"Enter the new value: ";
cin>>value;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start->info = value;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<next;
}
s->info = value;
}
cout<<"Node Updated"<>value;
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->info == value)
{
flag = true;
cout<<"Element "<next;
}
if (!flag)
cout<<"Element "<next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
start = ptr2;
}
/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<info<<"->";
temp = temp->next;
}
cout<<"NULL"<
Solution
/* C++ Program to Implement Singly Linked List */
#include
#include
#include
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;
/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void sort();
void search();
void update();
void reverse();
void display();
single_llist()
{
start = NULL;
}
};
/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<info = value;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void single_llist::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<next;s !=NULL;s = s->next)
{
if (ptr->info > s->info)
{
value = ptr->info;
ptr->info = s->info;
s->info = value;
}
}
ptr = ptr->next;
}
}
/*
* Delete element at a given position
*/
void single_llist::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<>pos;
cout<<"Enter the new value: ";
cin>>value;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start->info = value;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<next;
}
s->info = value;
}
cout<<"Node Updated"<>value;
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->info == value)
{
flag = true;
cout<<"Element "<next;
}
if (!flag)
cout<<"Element "<next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
start = ptr2;
}
/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<info<<"->";
temp = temp->next;
}
cout<<"NULL"<

More Related Content

PDF
C++ Program to Implement Singly Linked List #includei.pdf
PDF
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
PDF
In C++ I need help with this method that Im trying to write fillLi.pdf
PDF
linked listLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.pdf
PDF
using set identitiesSolutionimport java.util.Scanner; c.pdf
PDF
#includeiostream struct node {    char value;    struct no.pdf
DOC
Ds 2 cycle
PDF
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
C++ Program to Implement Singly Linked List #includei.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
linked listLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdf
#includeiostream struct node {    char value;    struct no.pdf
Ds 2 cycle
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf

Similar to C++ Program to Implement Singly Linked List #includeiostream.pdf (20)

PDF
Singly Linked List
PDF
#includeiostream #includecstdio #includecstdlib using na.pdf
DOCX
Shortened Linked List in C programming easy to learn for exam
PDF
take the following code and give details of what each line of code i.pdf
PDF
Double linked list header file below for FYI#include iostream.pdf
DOCX
Implement of c &amp; its coding programming by sarmad baloch
PDF
hi i have to write a java program involving link lists. i have a pro.pdf
PDF
#includeiostream#includecstdio#includecstdlibusing names.pdf
PPT
Doublylinklist
DOCX
Lab Week 2 Game Programming.docx
PDF
double linked list header file code#include iostream#include.pdf
PDF
Polynomialmotilalanehrunationalinstitute.pdf
PDF
Shapes and calculate (area and contour) / C++ oop concept
PDF
Data Structure
DOCX
Data structure lab on practical computer science.docx
DOCX
Data Structure lab on a practical in computer science
TXT
PDF
Shapes and calculate (area and contour) / C++ oop concept
Singly Linked List
#includeiostream #includecstdio #includecstdlib using na.pdf
Shortened Linked List in C programming easy to learn for exam
take the following code and give details of what each line of code i.pdf
Double linked list header file below for FYI#include iostream.pdf
Implement of c &amp; its coding programming by sarmad baloch
hi i have to write a java program involving link lists. i have a pro.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf
Doublylinklist
Lab Week 2 Game Programming.docx
double linked list header file code#include iostream#include.pdf
Polynomialmotilalanehrunationalinstitute.pdf
Shapes and calculate (area and contour) / C++ oop concept
Data Structure
Data structure lab on practical computer science.docx
Data Structure lab on a practical in computer science
Shapes and calculate (area and contour) / C++ oop concept
Ad

More from angelsfashion1 (20)

PDF
The density of water. The shape of protein molecu.pdf
PDF
SrCl2 So.pdf
PDF
Silica gel is a very polar absorbent, and so ho.pdf
PDF
PbI2-Pb2+ and 2I- Molar Concentration of Iodide.pdf
PDF
NiSO4 as its a weak salt and will not dissociates.pdf
PDF
NaCl is ionic compound and polar where as benzene.pdf
PDF
No. Sodium lauryl sulfate will not form insoluble.pdf
PDF
Moles of H2O = 4.564=6.75 moles .pdf
PDF
it is ribose and the other is deoxyribose .pdf
PDF
True.It is a confirmal mapping transforming imaginary axis of s-pl.pdf
PDF
These are two of the three major perspectives on sociology. Each of .pdf
PDF
I have no Idea sorry. .pdf
PDF
The main body of the new policy The employees can write blogs to .pdf
PDF
The emotional and psychological effects the HIVAIDS epidemic on the.pdf
PDF
Specific heat of water = 4.184 Jg.oCHeat released by dissolution .pdf
PDF
Solution is simple.Comments added start with calling a funct.pdf
PDF
Since the gene responsible for the color of pea and shape of seed of.pdf
PDF
HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf
PDF
Questionaccording to this rule of solubility rules most sulfat.pdf
PDF
Option (d) is correct. This is because dominant mutation involves si.pdf
The density of water. The shape of protein molecu.pdf
SrCl2 So.pdf
Silica gel is a very polar absorbent, and so ho.pdf
PbI2-Pb2+ and 2I- Molar Concentration of Iodide.pdf
NiSO4 as its a weak salt and will not dissociates.pdf
NaCl is ionic compound and polar where as benzene.pdf
No. Sodium lauryl sulfate will not form insoluble.pdf
Moles of H2O = 4.564=6.75 moles .pdf
it is ribose and the other is deoxyribose .pdf
True.It is a confirmal mapping transforming imaginary axis of s-pl.pdf
These are two of the three major perspectives on sociology. Each of .pdf
I have no Idea sorry. .pdf
The main body of the new policy The employees can write blogs to .pdf
The emotional and psychological effects the HIVAIDS epidemic on the.pdf
Specific heat of water = 4.184 Jg.oCHeat released by dissolution .pdf
Solution is simple.Comments added start with calling a funct.pdf
Since the gene responsible for the color of pea and shape of seed of.pdf
HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf
Questionaccording to this rule of solubility rules most sulfat.pdf
Option (d) is correct. This is because dominant mutation involves si.pdf
Ad

Recently uploaded (20)

PDF
Trump Administration's workforce development strategy
PPTX
Lesson notes of climatology university.
PDF
Weekly quiz Compilation Jan -July 25.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Microbial disease of the cardiovascular and lymphatic systems
Trump Administration's workforce development strategy
Lesson notes of climatology university.
Weekly quiz Compilation Jan -July 25.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Computing-Curriculum for Schools in Ghana
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Structure & Organelles in detailed.
Supply Chain Operations Speaking Notes -ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
GDM (1) (1).pptx small presentation for students
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial disease of the cardiovascular and lymphatic systems

C++ Program to Implement Singly Linked List #includeiostream.pdf

  • 1. /* C++ Program to Implement Singly Linked List */ #include #include #include /* * Node Declaration */ struct node { int info; struct node *next; }*start; /* * Class Declaration */ class single_llist { public: node* create_node(int); void insert_begin(); void insert_pos(); void insert_last(); void delete_pos(); void sort(); void search(); void update(); void reverse(); void display(); single_llist() { start = NULL; } };
  • 2. /* * Main :contains menu */ main() { int choice, nodes, element, position, i; single_llist sl; start = NULL; while (1) { cout<>choice; switch(choice) { case 1: cout<<"Inserting Node at Beginning: "<info = value; temp->next = NULL; return temp; } } /* * Inserting element in beginning */ void single_llist::insert_begin() { int value; cout<<"Enter the value to be inserted: "; cin>>value; struct node *temp, *p; temp = create_node(value); if (start == NULL) { start = temp; start->next = NULL; } else
  • 3. { p = start; start = temp; start->next = p; } cout<<"Element Inserted at beginning"<>value; struct node *temp, *s; temp = create_node(value); s = start; while (s->next != NULL) { s = s->next; } temp->next = NULL; s->next = temp; cout<<"Element Inserted at last"<>value; struct node *temp, *s, *ptr; temp = create_node(value); cout<<"Enter the postion at which node to be inserted: "; cin>>pos; int i; s = start; while (s != NULL) { s = s->next; counter++; } if (pos == 1) { if (start == NULL) { start = temp; start->next = NULL; } else {
  • 4. ptr = start; start = temp; start->next = ptr; } } else if (pos > 1 && pos <= counter) { s = start; for (i = 1; i < pos; i++) { ptr = s; s = s->next; } ptr->next = temp; temp->next = s; } else { cout<<"Positon out of range"<next;s !=NULL;s = s->next) { if (ptr->info > s->info) { value = ptr->info; ptr->info = s->info; s->info = value; } } ptr = ptr->next; } } /* * Delete element at a given position */ void single_llist::delete_pos() {
  • 5. int pos, i, counter = 0; if (start == NULL) { cout<<"List is empty"<>pos; struct node *s, *ptr; s = start; if (pos == 1) { start = s->next; } else { while (s != NULL) { s = s->next; counter++; } if (pos > 0 && pos <= counter) { s = start; for (i = 1;i < pos;i++) { ptr = s; s = s->next; } ptr->next = s->next; } else { cout<<"Position out of range"<>pos; cout<<"Enter the new value: "; cin>>value; struct node *s, *ptr; s = start; if (pos == 1) {
  • 6. start->info = value; } else { for (i = 0;i < pos - 1;i++) { if (s == NULL) { cout<<"There are less than "<next; } s->info = value; } cout<<"Node Updated"<>value; struct node *s; s = start; while (s != NULL) { pos++; if (s->info == value) { flag = true; cout<<"Element "<next; } if (!flag) cout<<"Element "<next == NULL) { return; } ptr1 = start; ptr2 = ptr1->next; ptr3 = ptr2->next; ptr1->next = NULL; ptr2->next = ptr1; while (ptr3 != NULL) { ptr1 = ptr2;
  • 7. ptr2 = ptr3; ptr3 = ptr3->next; ptr2->next = ptr1; } start = ptr2; } /* * Display Elements of a link list */ void single_llist::display() { struct node *temp; if (start == NULL) { cout<<"The List is Empty"<info<<"->"; temp = temp->next; } cout<<"NULL"< Solution /* C++ Program to Implement Singly Linked List */ #include #include #include /* * Node Declaration */ struct node { int info; struct node *next; }*start; /* * Class Declaration
  • 8. */ class single_llist { public: node* create_node(int); void insert_begin(); void insert_pos(); void insert_last(); void delete_pos(); void sort(); void search(); void update(); void reverse(); void display(); single_llist() { start = NULL; } }; /* * Main :contains menu */ main() { int choice, nodes, element, position, i; single_llist sl; start = NULL; while (1) { cout<>choice; switch(choice) { case 1: cout<<"Inserting Node at Beginning: "<info = value; temp->next = NULL;
  • 9. return temp; } } /* * Inserting element in beginning */ void single_llist::insert_begin() { int value; cout<<"Enter the value to be inserted: "; cin>>value; struct node *temp, *p; temp = create_node(value); if (start == NULL) { start = temp; start->next = NULL; } else { p = start; start = temp; start->next = p; } cout<<"Element Inserted at beginning"<>value; struct node *temp, *s; temp = create_node(value); s = start; while (s->next != NULL) { s = s->next; } temp->next = NULL; s->next = temp; cout<<"Element Inserted at last"<>value;
  • 10. struct node *temp, *s, *ptr; temp = create_node(value); cout<<"Enter the postion at which node to be inserted: "; cin>>pos; int i; s = start; while (s != NULL) { s = s->next; counter++; } if (pos == 1) { if (start == NULL) { start = temp; start->next = NULL; } else { ptr = start; start = temp; start->next = ptr; } } else if (pos > 1 && pos <= counter) { s = start; for (i = 1; i < pos; i++) { ptr = s; s = s->next; } ptr->next = temp; temp->next = s; }
  • 11. else { cout<<"Positon out of range"<next;s !=NULL;s = s->next) { if (ptr->info > s->info) { value = ptr->info; ptr->info = s->info; s->info = value; } } ptr = ptr->next; } } /* * Delete element at a given position */ void single_llist::delete_pos() { int pos, i, counter = 0; if (start == NULL) { cout<<"List is empty"<>pos; struct node *s, *ptr; s = start; if (pos == 1) { start = s->next; } else { while (s != NULL) { s = s->next; counter++;
  • 12. } if (pos > 0 && pos <= counter) { s = start; for (i = 1;i < pos;i++) { ptr = s; s = s->next; } ptr->next = s->next; } else { cout<<"Position out of range"<>pos; cout<<"Enter the new value: "; cin>>value; struct node *s, *ptr; s = start; if (pos == 1) { start->info = value; } else { for (i = 0;i < pos - 1;i++) { if (s == NULL) { cout<<"There are less than "<next; } s->info = value; } cout<<"Node Updated"<>value; struct node *s; s = start; while (s != NULL)
  • 13. { pos++; if (s->info == value) { flag = true; cout<<"Element "<next; } if (!flag) cout<<"Element "<next == NULL) { return; } ptr1 = start; ptr2 = ptr1->next; ptr3 = ptr2->next; ptr1->next = NULL; ptr2->next = ptr1; while (ptr3 != NULL) { ptr1 = ptr2; ptr2 = ptr3; ptr3 = ptr3->next; ptr2->next = ptr1; } start = ptr2; } /* * Display Elements of a link list */ void single_llist::display() { struct node *temp; if (start == NULL) { cout<<"The List is Empty"<info<<"->";