SlideShare a Scribd company logo
1 of 14
Download to read offline
/*
* C++ Program to Implement Singly Linked List
*/
#include
#include
#include
using namespace std;
/*
* 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<<"1.Insert Node at beginning"<>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
using namespace std;
/*
* 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<<"1.Insert Node at beginning"<>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

Similar to C++ Singly Linked List

take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdffastechsrv
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
Implement of c &amp; its coding programming by sarmad baloch
Implement of c &amp; its coding  programming by sarmad balochImplement of c &amp; its coding  programming by sarmad baloch
Implement of c &amp; its coding programming by sarmad balochSarmad Baloch
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdfKUNALHARCHANDANI1
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfarchgeetsenterprises
 
Double linked list header file below for FYI#include iostream.pdf
Double linked list header file below for FYI#include iostream.pdfDouble linked list header file below for FYI#include iostream.pdf
Double linked list header file below for FYI#include iostream.pdffacevenky
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docxAdamq0DJonese
 
double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdffacevenky
 
Doubly linklist
Doubly linklistDoubly linklist
Doubly linklistilsamaryum
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 

Similar to C++ Singly Linked List (20)

Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
take the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdftake the following code and give details of what each line of code i.pdf
take the following code and give details of what each line of code i.pdf
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
Implement of c &amp; its coding programming by sarmad baloch
Implement of c &amp; its coding  programming by sarmad balochImplement of c &amp; its coding  programming by sarmad baloch
Implement of c &amp; its coding programming by sarmad baloch
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
Double linked list header file below for FYI#include iostream.pdf
Double linked list header file below for FYI#include iostream.pdfDouble linked list header file below for FYI#include iostream.pdf
Double linked list header file below for FYI#include iostream.pdf
 
week-18x
week-18xweek-18x
week-18x
 
week-17x
week-17xweek-17x
week-17x
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
Data Structure
Data StructureData Structure
Data Structure
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Hw3
Hw3Hw3
Hw3
 
Linked list
Linked listLinked list
Linked list
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
 
double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Doubly linklist
Doubly linklistDoubly linklist
Doubly linklist
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 

More from anupambedcovers

1). The inheritance of recessive genes (disease causing) can remain .pdf
1). The inheritance of recessive genes (disease causing) can remain .pdf1). The inheritance of recessive genes (disease causing) can remain .pdf
1). The inheritance of recessive genes (disease causing) can remain .pdfanupambedcovers
 
$ 90.08WorkingStep-1Monthly Payment CalculationMonhly Payment=L.pdf
$ 90.08WorkingStep-1Monthly Payment  CalculationMonhly Payment=L.pdf$ 90.08WorkingStep-1Monthly Payment  CalculationMonhly Payment=L.pdf
$ 90.08WorkingStep-1Monthly Payment CalculationMonhly Payment=L.pdfanupambedcovers
 
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdfanupambedcovers
 
Long term Expected return of the Stock return ty.pdf
           Long term Expected return of the Stock            return ty.pdf           Long term Expected return of the Stock            return ty.pdf
Long term Expected return of the Stock return ty.pdfanupambedcovers
 
The English word chemistry has a few senses .pdf
                     The English word chemistry has a few senses .pdf                     The English word chemistry has a few senses .pdf
The English word chemistry has a few senses .pdfanupambedcovers
 
since it is (1)^infinity form ...... we get it b.pdf
                     since it is (1)^infinity form ......  we get it b.pdf                     since it is (1)^infinity form ......  we get it b.pdf
since it is (1)^infinity form ...... we get it b.pdfanupambedcovers
 
yes as NAI is ionic in nature water disolves ion.pdf
                     yes  as NAI is ionic in nature water disolves ion.pdf                     yes  as NAI is ionic in nature water disolves ion.pdf
yes as NAI is ionic in nature water disolves ion.pdfanupambedcovers
 
Mg(0H)2 as there is ionic between Mg & OH and co.pdf
                     Mg(0H)2 as there is ionic between Mg & OH  and co.pdf                     Mg(0H)2 as there is ionic between Mg & OH  and co.pdf
Mg(0H)2 as there is ionic between Mg & OH and co.pdfanupambedcovers
 
It depends on how many unique protons are adjacen.pdf
                     It depends on how many unique protons are adjacen.pdf                     It depends on how many unique protons are adjacen.pdf
It depends on how many unique protons are adjacen.pdfanupambedcovers
 
x = 1087n = 1430p =xn = 0.76standard error, SE = sqrt(p(1.pdf
x = 1087n = 1430p =xn = 0.76standard error, SE = sqrt(p(1.pdfx = 1087n = 1430p =xn = 0.76standard error, SE = sqrt(p(1.pdf
x = 1087n = 1430p =xn = 0.76standard error, SE = sqrt(p(1.pdfanupambedcovers
 
Option B. ie More than one theseSolutionOption B. ie More than.pdf
Option B. ie More than one theseSolutionOption B. ie More than.pdfOption B. ie More than one theseSolutionOption B. ie More than.pdf
Option B. ie More than one theseSolutionOption B. ie More than.pdfanupambedcovers
 
When two genes are located on the same chromosome they are called as.pdf
When two genes are located on the same chromosome they are called as.pdfWhen two genes are located on the same chromosome they are called as.pdf
When two genes are located on the same chromosome they are called as.pdfanupambedcovers
 
We have layered model used in network design since these layers grou.pdf
We have layered model used in network design since these layers grou.pdfWe have layered model used in network design since these layers grou.pdf
We have layered model used in network design since these layers grou.pdfanupambedcovers
 
The half-reactions areAnode Fe = [Fe]2+ + 2 e-Fe(s) release.pdf
The half-reactions areAnode Fe = [Fe]2+ + 2 e-Fe(s) release.pdfThe half-reactions areAnode Fe = [Fe]2+ + 2 e-Fe(s) release.pdf
The half-reactions areAnode Fe = [Fe]2+ + 2 e-Fe(s) release.pdfanupambedcovers
 
The answer is CH2Since moles = massmolar massMoles of C H = .pdf
The answer is CH2Since moles = massmolar massMoles of C  H = .pdfThe answer is CH2Since moles = massmolar massMoles of C  H = .pdf
The answer is CH2Since moles = massmolar massMoles of C H = .pdfanupambedcovers
 
SolutionFICASocial Security withheld taxMedicre tax withheldRates.pdf
SolutionFICASocial  Security withheld taxMedicre tax  withheldRates.pdfSolutionFICASocial  Security withheld taxMedicre tax  withheldRates.pdf
SolutionFICASocial Security withheld taxMedicre tax withheldRates.pdfanupambedcovers
 
Since HCL is a strong acid we can assume that it will fulyy ionise.pdf
Since HCL is a strong acid we can assume that it will fulyy ionise.pdfSince HCL is a strong acid we can assume that it will fulyy ionise.pdf
Since HCL is a strong acid we can assume that it will fulyy ionise.pdfanupambedcovers
 
d) Peptide Note that forming a peptide bond resu.pdf
                     d) Peptide  Note that forming a peptide bond resu.pdf                     d) Peptide  Note that forming a peptide bond resu.pdf
d) Peptide Note that forming a peptide bond resu.pdfanupambedcovers
 
Security has been the number 1 issue for any IT industry and organiz.pdf
Security has been the number 1 issue for any IT industry and organiz.pdfSecurity has been the number 1 issue for any IT industry and organiz.pdf
Security has been the number 1 issue for any IT industry and organiz.pdfanupambedcovers
 
P.E = - k e2 rnP.E in nth orbit is UnP.E is inversely propotio.pdf
P.E = - k e2  rnP.E in nth orbit is UnP.E is inversely propotio.pdfP.E = - k e2  rnP.E in nth orbit is UnP.E is inversely propotio.pdf
P.E = - k e2 rnP.E in nth orbit is UnP.E is inversely propotio.pdfanupambedcovers
 

More from anupambedcovers (20)

1). The inheritance of recessive genes (disease causing) can remain .pdf
1). The inheritance of recessive genes (disease causing) can remain .pdf1). The inheritance of recessive genes (disease causing) can remain .pdf
1). The inheritance of recessive genes (disease causing) can remain .pdf
 
$ 90.08WorkingStep-1Monthly Payment CalculationMonhly Payment=L.pdf
$ 90.08WorkingStep-1Monthly Payment  CalculationMonhly Payment=L.pdf$ 90.08WorkingStep-1Monthly Payment  CalculationMonhly Payment=L.pdf
$ 90.08WorkingStep-1Monthly Payment CalculationMonhly Payment=L.pdf
 
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
 
Long term Expected return of the Stock return ty.pdf
           Long term Expected return of the Stock            return ty.pdf           Long term Expected return of the Stock            return ty.pdf
Long term Expected return of the Stock return ty.pdf
 
The English word chemistry has a few senses .pdf
                     The English word chemistry has a few senses .pdf                     The English word chemistry has a few senses .pdf
The English word chemistry has a few senses .pdf
 
since it is (1)^infinity form ...... we get it b.pdf
                     since it is (1)^infinity form ......  we get it b.pdf                     since it is (1)^infinity form ......  we get it b.pdf
since it is (1)^infinity form ...... we get it b.pdf
 
yes as NAI is ionic in nature water disolves ion.pdf
                     yes  as NAI is ionic in nature water disolves ion.pdf                     yes  as NAI is ionic in nature water disolves ion.pdf
yes as NAI is ionic in nature water disolves ion.pdf
 
Mg(0H)2 as there is ionic between Mg & OH and co.pdf
                     Mg(0H)2 as there is ionic between Mg & OH  and co.pdf                     Mg(0H)2 as there is ionic between Mg & OH  and co.pdf
Mg(0H)2 as there is ionic between Mg & OH and co.pdf
 
It depends on how many unique protons are adjacen.pdf
                     It depends on how many unique protons are adjacen.pdf                     It depends on how many unique protons are adjacen.pdf
It depends on how many unique protons are adjacen.pdf
 
x = 1087n = 1430p =xn = 0.76standard error, SE = sqrt(p(1.pdf
x = 1087n = 1430p =xn = 0.76standard error, SE = sqrt(p(1.pdfx = 1087n = 1430p =xn = 0.76standard error, SE = sqrt(p(1.pdf
x = 1087n = 1430p =xn = 0.76standard error, SE = sqrt(p(1.pdf
 
Option B. ie More than one theseSolutionOption B. ie More than.pdf
Option B. ie More than one theseSolutionOption B. ie More than.pdfOption B. ie More than one theseSolutionOption B. ie More than.pdf
Option B. ie More than one theseSolutionOption B. ie More than.pdf
 
When two genes are located on the same chromosome they are called as.pdf
When two genes are located on the same chromosome they are called as.pdfWhen two genes are located on the same chromosome they are called as.pdf
When two genes are located on the same chromosome they are called as.pdf
 
We have layered model used in network design since these layers grou.pdf
We have layered model used in network design since these layers grou.pdfWe have layered model used in network design since these layers grou.pdf
We have layered model used in network design since these layers grou.pdf
 
The half-reactions areAnode Fe = [Fe]2+ + 2 e-Fe(s) release.pdf
The half-reactions areAnode Fe = [Fe]2+ + 2 e-Fe(s) release.pdfThe half-reactions areAnode Fe = [Fe]2+ + 2 e-Fe(s) release.pdf
The half-reactions areAnode Fe = [Fe]2+ + 2 e-Fe(s) release.pdf
 
The answer is CH2Since moles = massmolar massMoles of C H = .pdf
The answer is CH2Since moles = massmolar massMoles of C  H = .pdfThe answer is CH2Since moles = massmolar massMoles of C  H = .pdf
The answer is CH2Since moles = massmolar massMoles of C H = .pdf
 
SolutionFICASocial Security withheld taxMedicre tax withheldRates.pdf
SolutionFICASocial  Security withheld taxMedicre tax  withheldRates.pdfSolutionFICASocial  Security withheld taxMedicre tax  withheldRates.pdf
SolutionFICASocial Security withheld taxMedicre tax withheldRates.pdf
 
Since HCL is a strong acid we can assume that it will fulyy ionise.pdf
Since HCL is a strong acid we can assume that it will fulyy ionise.pdfSince HCL is a strong acid we can assume that it will fulyy ionise.pdf
Since HCL is a strong acid we can assume that it will fulyy ionise.pdf
 
d) Peptide Note that forming a peptide bond resu.pdf
                     d) Peptide  Note that forming a peptide bond resu.pdf                     d) Peptide  Note that forming a peptide bond resu.pdf
d) Peptide Note that forming a peptide bond resu.pdf
 
Security has been the number 1 issue for any IT industry and organiz.pdf
Security has been the number 1 issue for any IT industry and organiz.pdfSecurity has been the number 1 issue for any IT industry and organiz.pdf
Security has been the number 1 issue for any IT industry and organiz.pdf
 
P.E = - k e2 rnP.E in nth orbit is UnP.E is inversely propotio.pdf
P.E = - k e2  rnP.E in nth orbit is UnP.E is inversely propotio.pdfP.E = - k e2  rnP.E in nth orbit is UnP.E is inversely propotio.pdf
P.E = - k e2 rnP.E in nth orbit is UnP.E is inversely propotio.pdf
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
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
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
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
 
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
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
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
 
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
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
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
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
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 🔝✔️✔️
 
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
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
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
 
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
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

C++ Singly Linked List

  • 1. /* * C++ Program to Implement Singly Linked List */ #include #include #include using namespace std; /* * 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<<"1.Insert Node at beginning"<>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) {
  • 3. 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;
  • 4. 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; } } /*
  • 5. * 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;
  • 6. 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;
  • 7. 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 using namespace std; /* * Node Declaration */ struct node
  • 8. { 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)
  • 9. { cout<<"1.Insert Node at beginning"<>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);
  • 10. 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;
  • 11. 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) {
  • 12. 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) {
  • 13. 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; } /*
  • 14. * 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"<