KEMBAR78
Link List Programming Linked List in Cpp | PPTX
Programming Linked List
class LinkedList{
// declaration of the node
struct node{
int info;
node *next;
};
//private variable declared
node *start;
public:
LinkedList() //
Constructor
{
start = NULL;
}
//public fucntion declared
void AddAtBeg(int);
void AddAfter(int, int);
void Delete();
void Count();
void Search(int);
void Display();
};
20
Programming Linked List
// following function will add new element at the beginning
// also used to create first node
void LinkedList::AddAtBeg(int data)
{
node * newNode;
newNode = new node;
newNode->info = data;
newNode->next = start;
start = newNode;
}/*End of addatbeg()*/
21
Programming Linked List
//This function will add new element at any position
void LinkedList::AddAfter(int data, int pos)
{ node *newNode, *q;
q = start;
if(q == NULL) {
cout<<"nnEmpty linked list" << endl;
return;
}
//Finding the position in the linked list to insert
for(int i = 1; i < pos; i++)
q = q->next;
newNode = new node;
newNode->info = data;
newNode->next = q->next;
q->next = newNode;
}
22
Programming Linked List
void LinkedList::Delete()
{
node *tmp, *ptr;
int data;
if(start == NULL)
{
cout<<"nn List is empty"<<endl;
return;
}
cout<<"nnEnter the element for deletion : ";
cin>>data; // delete first node
if(start->info == data)
{ tmp = start;
start = start->next;
delete(tmp);
return;
}
23
ptr = start;
while(ptr->info != data) {
temp = ptr;
ptr = ptr ->next;
}
tmp->next = ptr->next;
delete(ptr); // Delete node in between, or last
node
}/*End of del()*/
24
Programming Linked List
void LinkedList::Display()
{
// Fill in the code that displays the contents of the linked list
} /*End of display() */
void LinkedList::Count()
{
// Fill in the code
that counts the
nodes of the linked
list
} /*End of count() */
void LinkedList::Search(int data)
{
// Fill in the code that will find the position of a node that
holds the ‘data’
} 25
Programming Linked List
int main()
{
int choice, n, m, position, i;
LinkedList po;
while(1)
{
cout<<"1. Add at beginningn";
cout<<"2. Add after n";
cout<<"3. Deleten";
cout<<"4. Displayn";
cout<<"5. Countn";
26
Programming Linked List
cout<<"6. Searchn"
cout<<"7. Quitn";
cout<<"n Enter your choice:";
cin>> choice;
switch(choice) {
case 1:
cout<<"nnEnter the element:";
cin>>m;
po.AddAtBeg(m);
break;
27
Programming Linked List
case 2:
cout<<"nnEnter the element:";
cin>>m;
cout<<"nPosition after inserted element:";
cin>>position;
po.AddAfter(m,position);
break;
case 3: po.Delete();
break;
case 4: po.Display();
break;
case 5: po.Count();
break;
case 6: cout<<"n
nEnter the element
to search:"; 28
Programming Linked List
cin>>m;
po.Search(m);
break;
case 7:
exit(0);
default:
c
out<<"
n
nWron
g
choice
";
}/*End of
switch */
29

Link List Programming Linked List in Cpp

  • 1.
    Programming Linked List classLinkedList{ // declaration of the node struct node{ int info; node *next; }; //private variable declared node *start; public: LinkedList() // Constructor { start = NULL; } //public fucntion declared void AddAtBeg(int); void AddAfter(int, int); void Delete(); void Count(); void Search(int); void Display(); }; 20
  • 2.
    Programming Linked List //following function will add new element at the beginning // also used to create first node void LinkedList::AddAtBeg(int data) { node * newNode; newNode = new node; newNode->info = data; newNode->next = start; start = newNode; }/*End of addatbeg()*/ 21
  • 3.
    Programming Linked List //Thisfunction will add new element at any position void LinkedList::AddAfter(int data, int pos) { node *newNode, *q; q = start; if(q == NULL) { cout<<"nnEmpty linked list" << endl; return; } //Finding the position in the linked list to insert for(int i = 1; i < pos; i++) q = q->next; newNode = new node; newNode->info = data; newNode->next = q->next; q->next = newNode; } 22
  • 4.
    Programming Linked List voidLinkedList::Delete() { node *tmp, *ptr; int data; if(start == NULL) { cout<<"nn List is empty"<<endl; return; } cout<<"nnEnter the element for deletion : "; cin>>data; // delete first node if(start->info == data) { tmp = start; start = start->next; delete(tmp); return; } 23
  • 5.
    ptr = start; while(ptr->info!= data) { temp = ptr; ptr = ptr ->next; } tmp->next = ptr->next; delete(ptr); // Delete node in between, or last node }/*End of del()*/ 24
  • 6.
    Programming Linked List voidLinkedList::Display() { // Fill in the code that displays the contents of the linked list } /*End of display() */ void LinkedList::Count() { // Fill in the code that counts the nodes of the linked list } /*End of count() */ void LinkedList::Search(int data) { // Fill in the code that will find the position of a node that holds the ‘data’ } 25
  • 7.
    Programming Linked List intmain() { int choice, n, m, position, i; LinkedList po; while(1) { cout<<"1. Add at beginningn"; cout<<"2. Add after n"; cout<<"3. Deleten"; cout<<"4. Displayn"; cout<<"5. Countn"; 26
  • 8.
    Programming Linked List cout<<"6.Searchn" cout<<"7. Quitn"; cout<<"n Enter your choice:"; cin>> choice; switch(choice) { case 1: cout<<"nnEnter the element:"; cin>>m; po.AddAtBeg(m); break; 27
  • 9.
    Programming Linked List case2: cout<<"nnEnter the element:"; cin>>m; cout<<"nPosition after inserted element:"; cin>>position; po.AddAfter(m,position); break; case 3: po.Delete(); break; case 4: po.Display(); break; case 5: po.Count(); break; case 6: cout<<"n nEnter the element to search:"; 28
  • 10.
    Programming Linked List cin>>m; po.Search(m); break; case7: exit(0); default: c out<<" n nWron g choice "; }/*End of switch */ 29