Program for dynamic implementation of linked list


#include<iostream.h>
#include<conio.h>

struct node
{
    int info;
    struct node *next;
};
struct node *list=0;
struct node * getnode(void)
{
/*function to allocate memory for new node*/
    return((struct node*)new(struct node));
}/*end getnode*/

void freenode(struct node *p)
{
/*free dynamically allocated memory*/
    delete(p);
}/*end freenode*/
void display()
{
/*display all nodes*/
    int i;
    struct node *t;
    t=list;
    if(t==NULL)
        cout<<"\nThe linked list is empty";
    else
    {       cout<<"\n";
        while(t!=NULL)
        {
            cout<<"-->|"<<t->info<<"|";
            t=t->next;
        }
    }
}/*end display*/
void insertbeg(int x)
{
/*insert new element at the beginning of linked list*/
    struct node *q;
    q=getnode();
    q->info=x;
    q->next=list;
    list=q;
    display();
}/*end insertbeg*/

void insertend(int x)
{
/*insert new element at the end of linked list*/
    struct node *q,*temp;
    q=getnode();
    q->info=x;
    q->next=NULL;
    temp=list;
    if(temp==NULL)
        list=q;
    else
    {
        while(temp->next!=NULL)
            temp=temp->next;
    temp->next=q;
    }
display();
}/*void insertend*/
void insafter(int p,int x)
{
/*insert new element after a specific node*/
    struct node *q,*t;
    int i;
    if(list==NULL)
        cout<<"\nInvalid insertion";
    else
    {
    t=list;
    for(i=0;i<p-1;i++)
        t=t->next;
    q=getnode();
    q->info=x;
    q->next=t->next;
    t->next=q;
    display();
    }
}/*end insafter*/
void delafter(int p)
{
/*delete new element after a specific node*/
    int x,i;
    struct node *t,*q;
    t=list;
    for(i=0;i<p-1;i++)
        t=t->next;
    if((list==NULL)||(t->next==NULL))
        cout<<"\nInvalid deletion(list is empty/end of list)";
    else
    {
    q=t->next;
    x=q->info;
    t->next=q->next;
    freenode(q);
    cout<<"\nThe deleted element is "<<x;
    }
display();
}/*end delafter*/
void deletebeg(void)
{
/*delete a node from the beginning of the linked list*/
    int x;
    struct node *q;
    q=list;
    if(list==NULL)
        cout<<"\nThe linked list is empty";
    else
    {
        x=q->info;
        list=q->next;
        cout<<"\nThe deleted element is "<<x;
        freenode(q);
        display();
    }
}/*end deletebeg*/
void deleteend(void)
{
/*delete a node from the end of the linked list*/
    struct node *q,*temp;
    int x;
    q=list;
    if(q==NULL)
        cout<<"\nThe linked list is empty";
    else
    {
        if(q->next==NULL)
        {
            x=q->info;
            list=NULL;
        }
        else
        {
            while(q->next!=NULL)
            {
                temp=q;
                q=q->next;
            }
        x=q->info;
        temp->next=NULL;
        }
    cout<<"\nThe deleted element is "<<x;
    freenode(q);
    }
display();
}/*end deleteend*/
void insloc(int p,int x)
{
/*insert new node at a specific location*/
    int t,i;
    struct node *q,*temp;
    temp=list;
    for(i=0;i<(p-2);i++)
    {
        temp=temp->next;
        if(temp==NULL)
        {
        cout<<"\nThere are less than "<<p<<"elements in list "<<p;
        break;
        }
    }
    if(temp!=NULL)
    {
        q=getnode();
        q->info=x;
        q->next=temp->next;
        temp->next=q;
    }
display();
}/*end insloc*/
void delloc(int p)
{
/*delete a node from a specific location*/
    int i;
    struct node *temp,*t;
    temp=list;
    if(p==1)
        list=list->next;
    for(i=0;i<p-1;i++)
    {
        if(temp->next==NULL)
        {
        cout<<"\nThere are less than "<<p<<"elements in list "<<p;
        break;
        }
        t=temp;
        temp=temp->next;
    }
    if(temp->next!=NULL)
    {
        cout<<"\nThe deleted element is "<<temp->info;
        t->next=temp->next;
        freenode(temp);
    }
display();
}/*end delloc*/
void search(int x)
{
/*search a node in linked list*/
    struct node *t;
    int i;
    for(t=list,i=0;t!=NULL;t=t->next,i++)
    {
        if(t->info==x)
        {
        cout<<"\nPosition of element "<<x<<"is "<<i+1<<endl;
        break;
        }
    }
if(t==NULL)
cout<<"\nElement not found";
display();
}/*end search*/
void main()
{
    struct node * getnode(void);
    int ch,p,info,x;
    char ans='y';

    clrscr();
    cout<<"\n\tDynamic Linked List";
    cout<<"\n1.Insert after a node";
    cout<<"\n2.Delete after a node";
    cout<<"\n3.Insert at begining ";
    cout<<"\n4.Delete at begining ";
    cout<<"\n5.Insert at end ";
    cout<<"\n6.Delete at end ";
    cout<<"\n7.Insert at location";
    cout<<"\n8.Delete at location";
    cout<<"\n9.Search an element";
    back:cout<<"\nEnter ur choice : ";
    cin>>ch;
    switch(ch)
    {
    case 1: cout<<"\nEnter the node no : ";
        cin>>p;
        cout<<"\nEnter the info : ";
        cin>>x;
        insafter(p,x);
        break;
    case 2: cout<<"\nEnter the node no : ";
        cin>>p;
        delafter(p);
        break;
    case 3: cout<<"\nEnter the info : ";
        cin>>x;
        insertbeg(x);
        break;
    case 4: deletebeg();
        break;
    case 5: cout<<"\nEnter the info : ";
        cin>>x;
        insertend(x);
        break;
    case 6: deleteend();
        break;
    case 7: cout<<"\nEnter the node no : ";
        cin>>p;
        cout<<"\nEnter the info : ";
        cin>>x;
        insloc(p,x);
        break;
    case 8: cout<<"\nEnter the node no : ";
        cin>>p;
        delloc(p);
        break;
    case 9 :cout<<"\nEnter the element : ";
        cin>>x;
        search(x);
        break;
    case 10: display();
        break;
    default:cout<<"\nWrong choice ";
    }
    cout<<"\nDo u want to continue(y/n)?";
    cin>>ans;
    if(ans=='y')
        goto back;
}/*end main*/

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|