SlideShare a Scribd company logo
#include
#include
#include
using namespace std;
struct node
{
int val;
struct node *next;
}*startNode;
// declaring class
class LinkedList
{
public:
node* create_node(int);
void insert_last();
void sort();
void search();
void reverse();
void display();
LinkedList()
{
startNode = NULL;
}
};
// main
main()
{
int userChoice, linkedListNodes, i;
LinkedList LinkList;
startNode = NULL;
while (1)
{
// displaying menu
cout<<" 1)Insert";
cout<<"2) Remove";
cout<<"3) Search";
cout<<"4) Display";
cout<<"5) Reverse";
cout<<"6) Exit ";
// reading user choice
cout<<"Enter your userChoice : ";
cin>>userChoice;
switch(userChoice)
{
case 1:
cout<<"Inserting node to linked list: ";
LinkList.insert_begin();
cout;
break;
case 2:
cout<<"Delete element from linked list: ";
LinkList.delete();
break;
case 3:
cout<<"Searching data in linked list: ";
LinkList.search();
cout;
break;
case 4:
cout<<"Displaying linked list data: ";
LinkList.display();
cout;
break;
case 5:
cout<<"Linked list reversing: ";
LinkList.reverse();
cout;
break;
case 6:
cout<<"Exiting...";
exit(1);
break;
default:
cout<<"Enter correct userChoice";
}
}
}
// node creating to store into list
node *LinkedList::create_node(int val)
{
struct node *tempNode, *tempNode;
tempNode = new(struct node);
if (tempNode == NULL)
{
cout<<"No memory allocated ";
return 0;
}
else
{
tempNode->val = val;
tempNode->next = NULL;
return tempNode;
}
}
/*
* Inserting Node at last
*/
void LinkedList::insert_last()
{
int val;
cout<<"Enter the val to be inserted: ";
cin>>val;
struct node *tempNode, *tempNode;
tempNode = create_node(val);
tempNode = startNode;
while (tempNode->next != NULL)
{
tempNode = tempNode->next;
}
tempNode->next = NULL;
tempNode->next = tempNode;
cout<<"Element Inserted";
}
// deleting element
void LinkedList::delete()
{
int position, i, count = 0;
if (startNode == NULL)
{
cout<<"Linkedlist empty"<>position;
struct node *tempNode, *ptr;
tempNode = startNode;
if (position == 1)
{
startNode = tempNode->next;
}
else
{
while (tempNode != NULL)
{
tempNode = tempNode->next;
count++;
}
if (position > 0 && position <= count)
{
tempNode = startNode;
for (i = 1;i < position;i++)
{
ptr = tempNode;
tempNode = tempNode->next;
}
ptr->next = tempNode->next;
}
else
{
cout<<"Enter correct position";
}
}
}
// search data in linked list
void LinkedList::search()
{
int val, position = 0;
bool flagValue = false;
if (startNode == NULL)
{
cout<<"List is empty";
return;
}
cout<<"Enter the val to search: ";
cin>>val;
struct node *tempNode;
tempNode = startNode;
while (tempNode != NULL)
{
position++;
if (tempNode->val == val)
{
flagValue = true;
cout<<"Data "<next;
}
if (!flagValue)
cout<<"data not found";
}
// linkedlist reverese
void LinkedList::reverse()
{
struct node *pointer1, *pointer2, *pointer3;
if (startNode == NULL)
{
cout<<"List is empty"<next == NULL)
{
return;
}
pointer1 = startNode;
pointer2 = pointer1->next;
pointer3 = pointer2->next;
pointer1->next = NULL;
pointer2->next = pointer1;
while (pointer3 != NULL)
{
pointer1 = pointer2;
pointer2 = pointer3;
pointer3 = pointer3->next;
pointer2->next = pointer1;
}
startNode = pointer2;
}
// display list
void LinkedList::display()
{
struct node *tempNode;
if (startNode == NULL)
{
cout<<"The List is Empty";
return;
}
tempNode = startNode;
cout<<"Data in list are ";
while (tempNode != NULL)
{
cout<val<<"->";
tempNode = tempNode->next;
}
}
Solution
#include
#include
#include
using namespace std;
struct node
{
int val;
struct node *next;
}*startNode;
// declaring class
class LinkedList
{
public:
node* create_node(int);
void insert_last();
void sort();
void search();
void reverse();
void display();
LinkedList()
{
startNode = NULL;
}
};
// main
main()
{
int userChoice, linkedListNodes, i;
LinkedList LinkList;
startNode = NULL;
while (1)
{
// displaying menu
cout<<" 1)Insert";
cout<<"2) Remove";
cout<<"3) Search";
cout<<"4) Display";
cout<<"5) Reverse";
cout<<"6) Exit ";
// reading user choice
cout<<"Enter your userChoice : ";
cin>>userChoice;
switch(userChoice)
{
case 1:
cout<<"Inserting node to linked list: ";
LinkList.insert_begin();
cout;
break;
case 2:
cout<<"Delete element from linked list: ";
LinkList.delete();
break;
case 3:
cout<<"Searching data in linked list: ";
LinkList.search();
cout;
break;
case 4:
cout<<"Displaying linked list data: ";
LinkList.display();
cout;
break;
case 5:
cout<<"Linked list reversing: ";
LinkList.reverse();
cout;
break;
case 6:
cout<<"Exiting...";
exit(1);
break;
default:
cout<<"Enter correct userChoice";
}
}
}
// node creating to store into list
node *LinkedList::create_node(int val)
{
struct node *tempNode, *tempNode;
tempNode = new(struct node);
if (tempNode == NULL)
{
cout<<"No memory allocated ";
return 0;
}
else
{
tempNode->val = val;
tempNode->next = NULL;
return tempNode;
}
}
/*
* Inserting Node at last
*/
void LinkedList::insert_last()
{
int val;
cout<<"Enter the val to be inserted: ";
cin>>val;
struct node *tempNode, *tempNode;
tempNode = create_node(val);
tempNode = startNode;
while (tempNode->next != NULL)
{
tempNode = tempNode->next;
}
tempNode->next = NULL;
tempNode->next = tempNode;
cout<<"Element Inserted";
}
// deleting element
void LinkedList::delete()
{
int position, i, count = 0;
if (startNode == NULL)
{
cout<<"Linkedlist empty"<>position;
struct node *tempNode, *ptr;
tempNode = startNode;
if (position == 1)
{
startNode = tempNode->next;
}
else
{
while (tempNode != NULL)
{
tempNode = tempNode->next;
count++;
}
if (position > 0 && position <= count)
{
tempNode = startNode;
for (i = 1;i < position;i++)
{
ptr = tempNode;
tempNode = tempNode->next;
}
ptr->next = tempNode->next;
}
else
{
cout<<"Enter correct position";
}
}
}
// search data in linked list
void LinkedList::search()
{
int val, position = 0;
bool flagValue = false;
if (startNode == NULL)
{
cout<<"List is empty";
return;
}
cout<<"Enter the val to search: ";
cin>>val;
struct node *tempNode;
tempNode = startNode;
while (tempNode != NULL)
{
position++;
if (tempNode->val == val)
{
flagValue = true;
cout<<"Data "<next;
}
if (!flagValue)
cout<<"data not found";
}
// linkedlist reverese
void LinkedList::reverse()
{
struct node *pointer1, *pointer2, *pointer3;
if (startNode == NULL)
{
cout<<"List is empty"<next == NULL)
{
return;
}
pointer1 = startNode;
pointer2 = pointer1->next;
pointer3 = pointer2->next;
pointer1->next = NULL;
pointer2->next = pointer1;
while (pointer3 != NULL)
{
pointer1 = pointer2;
pointer2 = pointer3;
pointer3 = pointer3->next;
pointer2->next = pointer1;
}
startNode = pointer2;
}
// display list
void LinkedList::display()
{
struct node *tempNode;
if (startNode == NULL)
{
cout<<"The List is Empty";
return;
}
tempNode = startNode;
cout<<"Data in list are ";
while (tempNode != NULL)
{
cout<val<<"->";
tempNode = tempNode->next;
}
}

More Related Content

DOC
Ds 2 cycle
PDF
#includeiostream#includecstdio#includecstdlibusing names.pdf
DOCX
Linked lists
PPTX
Link List Programming Linked List in Cpp
PDF
Using the provided table interface table.h and the sample linked lis.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
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
Ds 2 cycle
#includeiostream#includecstdio#includecstdlibusing names.pdf
Linked lists
Link List Programming Linked List in Cpp
Using the provided table interface table.h and the sample linked lis.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
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf

Similar to #includeiostream #includecstdio #includecstdlib using na.pdf (20)

PDF
How do you stop infinite loop Because I believe that it is making a.pdf
PPTX
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
PPTX
Linked list
PDF
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
PDF
linked listLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.pdf
DOCX
Lab Week 2 Game Programming.docx
PDF
In C++Write a recursive function to determine whether or not a Lin.pdf
PDF
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
PDF
C++ Program to Implement Singly Linked List #includei.pdf
PDF
#include iostream #includestdlib.h using namespace std;str.pdf
PDF
In the class we extensively discussed a node class called IntNode in.pdf
PDF
Please correct my errors upvote Clears our entire .pdf
DOCX
Linked list imp of list
PDF
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
PPTX
DSA(1).pptx
PDF
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
DOCX
#include iostream#include d_node.h #include d_nodel.h.docx
PDF
mainpublic class AssignmentThree {    public static void ma.pdf
PDF
In C++ I need help with this method that Im trying to write fillLi.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Linked list
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
linked listLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.pdf
Lab Week 2 Game Programming.docx
In C++Write a recursive function to determine whether or not a Lin.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
C++ Program to Implement Singly Linked List #includei.pdf
#include iostream #includestdlib.h using namespace std;str.pdf
In the class we extensively discussed a node class called IntNode in.pdf
Please correct my errors upvote Clears our entire .pdf
Linked list imp of list
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
DSA(1).pptx
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
#include iostream#include d_node.h #include d_nodel.h.docx
mainpublic class AssignmentThree {    public static void ma.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf

More from harihelectronicspune (20)

PDF
Entero pluri assay ,done to detect the enterobacteriaceae bacteria w.pdf
PDF
steam distillation is an effective way to separat.pdf
PDF
Nothing, really. Cu has a charge of 3+ or 2+ in .pdf
PDF
2) Starter culture is the culture of microorganisms used to inoculat.pdf
PDF
1. What were the organizational benefits for having a cloud computin.pdf
PDF
what is x(4-3x)-7Solutionx(4-3x)-7 is just an algebraic expres.pdf
PDF
What probability value would complete the following probability dist.pdf
PDF
TriggerTrigger is set of statements or stored program which inclu.pdf
PDF
The equation isE = hcL where E is the energy, h is planc.pdf
PDF
The white matter of the cerebellum is called arbor vitae which means.pdf
PDF
The following organs are arranged from anterior to posterior as foll.pdf
PDF
The Open Systems Interconnect (OSI) model has seven layers. this tex.pdf
PDF
The transition in phosphorescence, from a ground singlet state to a .pdf
PDF
this is wrong the answer is 2.1 because x is small and doesnt cont.pdf
PDF
There are four basic principles required by IMA ethical standards,vi.pdf
PDF
The synthesis of particular gene products is controlled by mechanism.pdf
PDF
Risk assessment is the process which - identify hazards, analyzes an.pdf
PDF
SolutionWe will assign letters to the first column of graphs and .pdf
PDF
Solution Three of the many ways pathogens can cause tissue damage.pdf
PDF
Legal & Political factors having significant impact on the U.S. rest.pdf
Entero pluri assay ,done to detect the enterobacteriaceae bacteria w.pdf
steam distillation is an effective way to separat.pdf
Nothing, really. Cu has a charge of 3+ or 2+ in .pdf
2) Starter culture is the culture of microorganisms used to inoculat.pdf
1. What were the organizational benefits for having a cloud computin.pdf
what is x(4-3x)-7Solutionx(4-3x)-7 is just an algebraic expres.pdf
What probability value would complete the following probability dist.pdf
TriggerTrigger is set of statements or stored program which inclu.pdf
The equation isE = hcL where E is the energy, h is planc.pdf
The white matter of the cerebellum is called arbor vitae which means.pdf
The following organs are arranged from anterior to posterior as foll.pdf
The Open Systems Interconnect (OSI) model has seven layers. this tex.pdf
The transition in phosphorescence, from a ground singlet state to a .pdf
this is wrong the answer is 2.1 because x is small and doesnt cont.pdf
There are four basic principles required by IMA ethical standards,vi.pdf
The synthesis of particular gene products is controlled by mechanism.pdf
Risk assessment is the process which - identify hazards, analyzes an.pdf
SolutionWe will assign letters to the first column of graphs and .pdf
Solution Three of the many ways pathogens can cause tissue damage.pdf
Legal & Political factors having significant impact on the U.S. rest.pdf

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Types and Its function , kingdom of life
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
master seminar digital applications in india
PDF
Computing-Curriculum for Schools in Ghana
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Insiders guide to clinical Medicine.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Pre independence Education in Inndia.pdf
Complications of Minimal Access Surgery at WLH
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Types and Its function , kingdom of life
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
master seminar digital applications in india
Computing-Curriculum for Schools in Ghana
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial disease of the cardiovascular and lymphatic systems
Insiders guide to clinical Medicine.pdf
GDM (1) (1).pptx small presentation for students
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Anesthesia in Laparoscopic Surgery in India
PPH.pptx obstetrics and gynecology in nursing
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pre independence Education in Inndia.pdf

#includeiostream #includecstdio #includecstdlib using na.pdf

  • 1. #include #include #include using namespace std; struct node { int val; struct node *next; }*startNode; // declaring class class LinkedList { public: node* create_node(int); void insert_last(); void sort(); void search(); void reverse(); void display(); LinkedList() { startNode = NULL; } }; // main main() { int userChoice, linkedListNodes, i; LinkedList LinkList; startNode = NULL; while (1) { // displaying menu
  • 2. cout<<" 1)Insert"; cout<<"2) Remove"; cout<<"3) Search"; cout<<"4) Display"; cout<<"5) Reverse"; cout<<"6) Exit "; // reading user choice cout<<"Enter your userChoice : "; cin>>userChoice; switch(userChoice) { case 1: cout<<"Inserting node to linked list: "; LinkList.insert_begin(); cout; break; case 2: cout<<"Delete element from linked list: "; LinkList.delete(); break; case 3: cout<<"Searching data in linked list: "; LinkList.search(); cout; break; case 4: cout<<"Displaying linked list data: "; LinkList.display(); cout; break; case 5: cout<<"Linked list reversing: "; LinkList.reverse(); cout; break; case 6:
  • 3. cout<<"Exiting..."; exit(1); break; default: cout<<"Enter correct userChoice"; } } } // node creating to store into list node *LinkedList::create_node(int val) { struct node *tempNode, *tempNode; tempNode = new(struct node); if (tempNode == NULL) { cout<<"No memory allocated "; return 0; } else { tempNode->val = val; tempNode->next = NULL; return tempNode; } } /* * Inserting Node at last */ void LinkedList::insert_last() { int val; cout<<"Enter the val to be inserted: "; cin>>val; struct node *tempNode, *tempNode;
  • 4. tempNode = create_node(val); tempNode = startNode; while (tempNode->next != NULL) { tempNode = tempNode->next; } tempNode->next = NULL; tempNode->next = tempNode; cout<<"Element Inserted"; } // deleting element void LinkedList::delete() { int position, i, count = 0; if (startNode == NULL) { cout<<"Linkedlist empty"<>position; struct node *tempNode, *ptr; tempNode = startNode; if (position == 1) { startNode = tempNode->next; } else { while (tempNode != NULL) { tempNode = tempNode->next; count++; } if (position > 0 && position <= count) { tempNode = startNode; for (i = 1;i < position;i++) {
  • 5. ptr = tempNode; tempNode = tempNode->next; } ptr->next = tempNode->next; } else { cout<<"Enter correct position"; } } } // search data in linked list void LinkedList::search() { int val, position = 0; bool flagValue = false; if (startNode == NULL) { cout<<"List is empty"; return; } cout<<"Enter the val to search: "; cin>>val; struct node *tempNode; tempNode = startNode; while (tempNode != NULL) { position++; if (tempNode->val == val) { flagValue = true; cout<<"Data "<next; } if (!flagValue) cout<<"data not found";
  • 6. } // linkedlist reverese void LinkedList::reverse() { struct node *pointer1, *pointer2, *pointer3; if (startNode == NULL) { cout<<"List is empty"<next == NULL) { return; } pointer1 = startNode; pointer2 = pointer1->next; pointer3 = pointer2->next; pointer1->next = NULL; pointer2->next = pointer1; while (pointer3 != NULL) { pointer1 = pointer2; pointer2 = pointer3; pointer3 = pointer3->next; pointer2->next = pointer1; } startNode = pointer2; } // display list void LinkedList::display() { struct node *tempNode; if (startNode == NULL) { cout<<"The List is Empty"; return; }
  • 7. tempNode = startNode; cout<<"Data in list are "; while (tempNode != NULL) { cout<val<<"->"; tempNode = tempNode->next; } } Solution #include #include #include using namespace std; struct node { int val; struct node *next; }*startNode; // declaring class class LinkedList { public: node* create_node(int); void insert_last(); void sort(); void search(); void reverse(); void display(); LinkedList() { startNode = NULL; } };
  • 8. // main main() { int userChoice, linkedListNodes, i; LinkedList LinkList; startNode = NULL; while (1) { // displaying menu cout<<" 1)Insert"; cout<<"2) Remove"; cout<<"3) Search"; cout<<"4) Display"; cout<<"5) Reverse"; cout<<"6) Exit "; // reading user choice cout<<"Enter your userChoice : "; cin>>userChoice; switch(userChoice) { case 1: cout<<"Inserting node to linked list: "; LinkList.insert_begin(); cout; break; case 2: cout<<"Delete element from linked list: "; LinkList.delete(); break; case 3: cout<<"Searching data in linked list: "; LinkList.search(); cout; break; case 4:
  • 9. cout<<"Displaying linked list data: "; LinkList.display(); cout; break; case 5: cout<<"Linked list reversing: "; LinkList.reverse(); cout; break; case 6: cout<<"Exiting..."; exit(1); break; default: cout<<"Enter correct userChoice"; } } } // node creating to store into list node *LinkedList::create_node(int val) { struct node *tempNode, *tempNode; tempNode = new(struct node); if (tempNode == NULL) { cout<<"No memory allocated "; return 0; } else { tempNode->val = val; tempNode->next = NULL; return tempNode; } }
  • 10. /* * Inserting Node at last */ void LinkedList::insert_last() { int val; cout<<"Enter the val to be inserted: "; cin>>val; struct node *tempNode, *tempNode; tempNode = create_node(val); tempNode = startNode; while (tempNode->next != NULL) { tempNode = tempNode->next; } tempNode->next = NULL; tempNode->next = tempNode; cout<<"Element Inserted"; } // deleting element void LinkedList::delete() { int position, i, count = 0; if (startNode == NULL) { cout<<"Linkedlist empty"<>position; struct node *tempNode, *ptr; tempNode = startNode; if (position == 1) { startNode = tempNode->next; } else {
  • 11. while (tempNode != NULL) { tempNode = tempNode->next; count++; } if (position > 0 && position <= count) { tempNode = startNode; for (i = 1;i < position;i++) { ptr = tempNode; tempNode = tempNode->next; } ptr->next = tempNode->next; } else { cout<<"Enter correct position"; } } } // search data in linked list void LinkedList::search() { int val, position = 0; bool flagValue = false; if (startNode == NULL) { cout<<"List is empty"; return; } cout<<"Enter the val to search: "; cin>>val; struct node *tempNode; tempNode = startNode;
  • 12. while (tempNode != NULL) { position++; if (tempNode->val == val) { flagValue = true; cout<<"Data "<next; } if (!flagValue) cout<<"data not found"; } // linkedlist reverese void LinkedList::reverse() { struct node *pointer1, *pointer2, *pointer3; if (startNode == NULL) { cout<<"List is empty"<next == NULL) { return; } pointer1 = startNode; pointer2 = pointer1->next; pointer3 = pointer2->next; pointer1->next = NULL; pointer2->next = pointer1; while (pointer3 != NULL) { pointer1 = pointer2; pointer2 = pointer3; pointer3 = pointer3->next; pointer2->next = pointer1; } startNode = pointer2; }
  • 13. // display list void LinkedList::display() { struct node *tempNode; if (startNode == NULL) { cout<<"The List is Empty"; return; } tempNode = startNode; cout<<"Data in list are "; while (tempNode != NULL) { cout<val<<"->"; tempNode = tempNode->next; } }