Boost Fertility New Invention Ups Success Rates.pdf
Ds006 linked list- delete from front
1. Deletion from Front:
if(start==NULL)
{
cout<<"No Node to delete...n";
return;
}
Start
Case 1: Linked List is Empty
13
11 12
Start
NULL
Start
Case 1: Linked List is Empty
Case 2: Linked List has Nodes
3. ptr=start;
start=start->next;
delete ptr;
}
*******************************************************************
Program
//Linked List
#include<iostream>
using namespace std;
//Structure of a Node
struct Node
{
int info;
struct Node *next;
}*new_node,*start,*ptr;
struct Node *create_node(int);
int value;
//Create a New Node
struct Node *create_node(int value)
{ new_node = new Node();
new_node->info= value;
new_node->next=NULL;
return new_node;
}
// Insertion of Node at the begining of the Linked List
void insert_begin(struct Node *new_node)
{ //case 1: linked list is Empty
if(start==NULL)
{
start=new_node;
}
else // Linked List not empty
{
4. new_node->next = start;
start= new_node;
}
}
//Delete a Node from Front
void delete_begin()
{
if(start==NULL)
{
cout<<"List is emptynNo Node to deleten";
return;
}
else
{
ptr=start;
start=start->next;
delete ptr;
}
}
//Display or Traverse a Linked List
void display()
{
if(start==NULL)
{
cout<<"n No node to diaplay...nList is Empty";
return;
}
else
{
ptr = start;
while(ptr!=NULL)
{
cout<<ptr->info<<"->";
ptr=ptr->next;
}
cout<<"!!!"<<endl;
5. }
}
int main()
{
int flag=0;
int ch;
while(flag==0)
{
cout<<"Choose an optionn";
cout<<"1. Create a node n2. Insert at Front n3. Delete First node
n4. Display/Travarse n5. Exitn" ;
cin>>ch;
switch(ch)
{
case 1:
cout<<("Enter info for new noden");
cin>>value;
new_node = create_node(value);
if(new_node!= NULL)
{
cout<<"nnew_node created sucessfullyn";
cout<<"new_node->info = "<<new_node-
>info<<endl;
}
else
{
cout<<"Problem in creating noden Try Next
Timen";
return 0;
}
break;
case 2:
insert_begin(new_node);
cout<<"n New node inserted at the begining
successfullyn";
break;
6. case 3:
delete_begin();
cout<<"Node deleted sucessfully...n";
case 4:
cout<<"n Traversing Linked List..n";
display();
break;
case 5:
cout<<"Exiting...n";
return 0;
}
}
return 0;
}