Showing posts with label linked-list. Show all posts
Showing posts with label linked-list. Show all posts

Monday, 29 December 2014

Delete alternate elements of a linked-list

This code is written in c++ . This code deletes all the alternate nodes of the linked-list starting from the head . We have used the create function to create the nodes of the linked-list and display function to display the elements of the linked-list . Let's talk about the delete_alternate function , this function has been employed to delete the alternate nodes of the linked-list . We have used two pointers both pointing to the head of the list (pptr and ptr) . ptr points to a node at a time whereas pptr points to the previous node of ptr as assigned by the code snippet :--

pptr=ptr;
 ptr=ptr->link; 

We have used a counter which counts the position of node in traversal order , starting from the head . Assigning position 1 to the head we have deleted all the nodes lying at even positions and pointing to the next nodes whenever the node has an odd position . The deletion logic is given in the else clause of the while loop .

else
        {
            pptr->link=ptr->link;
            ptr=ptr->link;
        }

Input List:      8->7->6->5->4->3->2->1

Output List:   8->6->4->2

Code:--

#include<iostream>
#include<conio.h>
using namespace std;

// structure to define single node of the list
struct node
{
    int data;
    node *link;
}*head=NULL,*ptr,*pptr;

// function to create nodes of the list
void create()
{
    int item;
    cout<<"enter the item you want to insert in the list"<<endl;
    cin>>item;
    if(head==NULL)
    {
        ptr=new node;
        ptr->data=item;
        ptr->link=NULL;
        head=ptr;
    }
    else
    {
        ptr=new node;
        ptr->data=item;
        ptr->link=head;
        head=ptr;
    }
}

// function to traverse the list and display the elements
void display()
{
    ptr=head;
    while(ptr!=NULL)
    {
        cout<<ptr->data<<"\t";
        ptr=ptr->link;
    }
}

// function to delete alternative nodes
void delete_alternate()
{
    pptr=head;
    ptr=head;
    int counter=0;
    while(ptr!=NULL)
    {
        counter++;
        if(counter%2==1)
        {
            pptr=ptr;
            ptr=ptr->link;
        }
        else
        {
            pptr->link=ptr->link;
            ptr=ptr->link;
        }
    }
    display();
}

// main method
int main()
{
    int option;
    while(option!=4)
    {
        cout<<endl;
        cout<<"enter 1. to enter the element in the list"<<endl;
        cout<<"enter 2. to display the list"<<endl;
        cout<<"enter 3. to delete alternative elements from the linked-list"<<endl;
        cout<<"enter 4. to exit"<<endl;
        cin>>option;
        switch(option)
        {
            case 1: create();
            break;
            case 2: display();
            break;
            case 3: delete_alternate();
            break;
        }
    }
    getch();
    return 0;
}

Delete duplicate content from linked-list

The code given below is a code written in c++ . This code deletes all the duplicate contents of the linked-list . In the code there is a pointer(ptr) of type node which points  to a node . There is also a pointer pptr which  points to the previous node of ptr . Create function is used to insert elements in the linked-list by creating nodes . Display function is used to display the elements in the list . Let's talk about the delete_duplicate function , this function uses two loops both of which are while loops . First(outer) while loop is used to select an element using the temp pointer and set the values of ptr and pptr pointers . Then we check for duplicate content in the if clause of inner loop . The inner loop is used to compare all the other elements of the linked-list with the selected element . If the duplicate content is found then the particular node is deleted using the line  

pptr->link=ptr->link;
 ptr=ptr->link;

and the ptr is pointed to the next node without changing pptr . In the else clause the pptr pointer is made to point to the next node and so as ptr.


#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node *link;
}*head=NULL,*ptr,*temp,*pptr;

void create()
{
    int item;
    cout<<"enter the item you want to insert"<<endl;
    cin>>item;
    ptr=head;
    if (ptr==NULL)
    {
        ptr=new node;
        ptr->data=item;
        ptr->link=NULL;
        head=ptr;
    }
    else
    {
        ptr=new node;
        ptr->data=item;
        ptr->link=head;
        head=ptr;
    }
}

void delete_duplicate()
{
    temp=head;
    ptr=head->link;
    pptr=head;
    int d;
    while(temp!=NULL)
    {
        d=temp->data;
        ptr=temp->link;
        pptr=temp;
        while(ptr!=NULL)
        {
            if(d==ptr->data)
            {
                pptr->link=ptr->link;
                ptr=ptr->link;
            }
            else
            {
                pptr=ptr;
                ptr=ptr->link;
            }
        }
        temp=temp->link;
    }
}

void display()
{
    ptr=head;
    while(ptr!=NULL)
    {
        cout<<ptr->data<<"\t";
        ptr=ptr->link;
    }
}

int main()
{
    int option;
    while(option!=4)
    {
        cout<<endl;
        cout<<"enter 1 to enter the data in the list"<<endl;
        cout<<"enter 2 to display data "<<endl;
        cout<<"enter 3 to delete duplicate content from the list"<<endl;
        cout<<"enter 4 to exit "<<endl;
        cin>>option;
    switch(option)
    {
        case 1: create();
        break;
        case 2: display();
        break;
        case 3: delete_duplicate();
        break;
    }
    }
    getch();    return 0;
}

Saturday, 27 December 2014

Getting Kth element from last of Linked-List

// to find the kth element from the last of the linked list
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node *link;
}*head=NULL,*ptr;
int n=0;
int create()
{
   
    int value;
    cout<<"enter the data you want to enter in the linked list"<<endl;
    cin>>value;
    ptr=head;
    if(head==NULL)
    {
        ptr=new node;
        ptr->data=value;
        ptr->link=NULL;
        head=ptr;
        n++;
    }
    else
    {
        ptr=new node;
        ptr->data=value;
        ptr->link=head;
        head=ptr;
        n++;
    }
    return n;
}

void display()
{
    ptr=head;
    while(ptr!=NULL)
    {
        cout<<ptr->data<<"\t";
        ptr=ptr->link;
    }
    cout<<endl;
}

void find()
{
    int k;
    cout<<"enter the value of k"<<endl;
    cout<<"i.e enter the position of the element from the last"<<endl;
    cin>>k;
    int c=(n-k)+1;
    int c1=1;
    ptr=head;
    while(c1!=c)
    {
        c1++;
        ptr=ptr->link;
    }
    cout<<endl;
    cout<<"the kth element from the end is"<<endl;
    cout<<ptr->data<<endl;
    cout<<endl;
}

int main()
{
    int option;
    while(option!=4)
    {
    cout<<"enter 1. to insert"<<endl;
    cout<<"enter 2. to display"<<endl;
    cout<<"enter 3. to find"<<endl;
    cout<<"enter 4. to exit"<<endl;
    cin>>option;
    switch(option)
    {
        case 1: create();
        break;
        case 2: display();
        break;
        case 3: find();
        break;
    }
    }
    getch();
    return 0;
}




OUTPUT:--


Reverse of a Linked-List

#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node* link;
}*head=NULL,*ptr,*tptr;

// function to insert elements in the nodes
void create()
{
    int item;
    cout<<"enter the data to be added"<<endl;
    cin>>item;
    if(head==NULL)
    {
        ptr=new node;
        ptr->data=item;
        ptr->link=NULL;
        head=ptr;
    }
    else
    {
        ptr=new node;
        ptr->data=item;
        ptr->link=head;
        head=ptr;
    }
}


// function to display the elements in the linked-list
void display()
{
    ptr=head;
    while(ptr!=NULL)
    {
        cout<<ptr->data<<endl;
        ptr=ptr->link;
    }
}


// function to reverse the linked-list
void reverse()
{
    ptr=head;
    tptr=head->link;
    node *temp;
    if(tptr->link!=NULL)
    {
        temp=tptr->link;
        head->link=NULL;
    }
    while(temp->link!=NULL)
    {
        tptr->link=ptr;
        ptr=tptr;
        tptr=temp;
        temp=temp->link;
    }
    tptr->link=ptr;
    head=temp;
    temp->link=tptr;
}

int main()
{
    int option;
    while(option!=4)
    {
    cout<<"enter 1. to insert in the linked-list"<<endl;
    cout<<"enter 2. to display the list"<<endl;
    cout<<"enter 3. to reverse the list"<<endl;
    cout<<"enter 4. to exit the program"<<endl;
    cin>>option;
    switch(option)
    {
        case 1: create();
        break;
        case 2: display();
        break;
        case 3: reverse();
        break;
    }
    }
    getch();
    return 0;
}