Link List in C++

Single Link List
A link list is linear data structure where each element is separated. Link list is used to link with two or more elements. Single link list is used in C++ data structure. Link is connected with each other. If one link is break all component from the link that is break doesn’t work. Each link stores the address of Head and Tail in link list. Starting component of link list is empty.
#include <iostream.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int info;
struct node *link;
}*start=NULL;

void create()
{
struct node *temp, *q;
temp=(struct node*)malloc(sizeof(struct node));
int data;
cout<<"Enter data "<<endl;
temp->info=data;
if(start==NULL)
{
start=temp;
}
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}

void insert_at_beg()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
int data;
cout<<"Enter data for add at begning at link list"<<endl;
temp->info=data;
temp->link=start;
start=temp;
}

void insert_at_any_pos()
{
struct node *temp, *q;
temp=(struct node*)malloc(sizeof(struct node));
int i, data, pos;
cout<<"Enter data for add at any position of link list "<<endl;
temp->info=data;
cout<<"Enter required postion "<<endl;
cin>>pos;
for(i=0; i<pos; i++)
{
if(q->link==NULL)
{
cout<<"invalid position "<<endl;
}
q=q->link;
}
temp->link=q->link;
q->link=temp;
}

void insert_at_end()
{
 int value;
 cout<<"Enter the value to be inserted: ";
 cin>>value;
struct node *temp, *s;
temp->info=value;
s = start;
 while (s->link != NULL)
{
 s = s->link;
}
temp->link = NULL;
s->link = temp;
 cout<<"Element Inserted at last"<<endl;
}

void del_from_beg()
{
struct node *q;
q=start;
start=q->link;
free(q);
}

void del_from_end()
{
struct node *ptr, *preptr;
preptr=start;
ptr=start->link;
while(ptr->link!=NULL)
{
ptr=ptr->link;
preptr=preptr->link;
}
preptr->link=NULL;
free(ptr);
}
void del_from_any_pos()
{
 int pos, i, counter = 0;
    if (start == NULL)
    {
        cout<<"List is empty"<<endl;
        return;
    }
    cout<<"Enter the position of value to be deleted: ";
    cin>>pos;
    struct node *s, *ptr;
    s = start;
    if (pos == 1)
    {
        start = s->link;
    }
    else
    {
        while (s != NULL)
        {
            s = s->link;
            counter++;
        }
        if (pos > 0 && pos <= counter)
        {
            s = start;
            for (i = 1;i < pos;i++)
            {
                ptr = s;
                s = s->link;
            }
            ptr->link = s->link;
        }
        else
        {
            cout<<"Position out of range"<<endl;
        }
        free(s);
        cout<<"Element Deleted"<<endl;
    }
}
void display()
{
struct node *q;
q=start;
while(q->link!=NULL)
{
cout<<q->info;
q=q->link;
}
}
void main()
{
int ch;
while(1)
{
cout<<"1. Enter 1 for create link list \n2. Enter 2 for insert node at begining \n3. Enter 3 for insert node at any position \n4. Enter 4 for insert at end \n5. Enter 5 for Delete from beg \n6. Enter 6 for Delete from any postion \n7. Enter 7 for Delete from end \n8. Enter 8 for Display \n9. Enter 9 for exit:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
create();
break;
case 2:
insert_at_beg();
break;
case 3:
insert_at_any_pos();
break;
case 4:
insert_at_end();
break;
case 5:
del_from_beg();
break;
case 6:
del_from_end();
break;
case 7:
del_from_any_pos();
break;
case 8:
display();
break;
case 9:
exit(0);
}
getch();
}

}

Result:

Post a Comment

Total Pageviews