Social Icons

Sunday 14 September 2014

Insertion Deletion In Linked List

#include<iostream>
#include<conio.h>
using namespace std;
struct link
{
int data;
struct link *next;

};

void display();
void initial();
void deletion();

struct link *newn,*loc,*start=NULL,*ptr;


int main()
{
int i,item;
for(i=1;i<5;i++)
    {
    cout<<"Enter element";
cin>>item;
newn =new struct link[1];
newn->data=item;
newn->next=NULL;


if(start==NULL)
    {
    start=newn;
    }
    else
    {
    ptr=start;
    while(ptr->next!=NULL)
    {
    ptr=ptr->next;
   
    }
   
    ptr->next=newn;
    }

 }
   
    display();
    //for(i=1;i<=2;i++)
    //{
   
initial();
//}
}





void display()
{
ptr=start;
while(ptr!=NULL)
{
cout<<ptr->data;
cout<<"-->";
ptr=ptr->next;
}


}



void initial()
{
int item;
    cout<<"Enter the item which u want to insert in begining";
    cin>>item;
   
    struct link *p;
    p=new struct link[1];
    p->data=item;
    p->next=NULL;
    if(start==NULL)
    {
    start=p;
   
    }
else
    {
      p->next=start;
 start=p;
    }

display();
deletion();
}


void deletion()
{
char c;
cout<<"do u want to delete from begining";
cin>>c;
if(c=='y')
{
start=start->next;
display();
}

}

No comments:

Post a Comment

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management