Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Trees
Home > Programs > Trees

72. Make 3 nodes in doubly linked list and delete the 2nd node. Use pointer as ptr.  Write the program for same.

Answer:

#include <stdio.h>
#include <stdlib.h>
# include <iostream>
using namespace std; 
struct node
{
    struct node *prev;
    int n;
    struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void del();
int count = 0;
void main()
{
    int ch;
    h = NULL;
    temp = temp1 = NULL;
    cout<<"\n 1 - Insert at beginning";
    cout<<"\n 2 - Insert at end";
    cout<<"\n 3 - Insert at position i";
    cout<<"\n 4 - Delete at i";
    cout<<"\n 5 - Exit";
    while (1)
    {
        cout<<"\n Enter choice : ";
        cin>>ch;
        switch (ch)
        {
        case 1:
            insert1();
            break;
        case 2:
            insert2();
            break;
        case 3:
            insert3();
            break;
        case 4:
            del();
            break;
        case 5:
            exit(0);
        default:
            printf("\n Wrong choice menu");
        }
    }
}
 
/* TO create an empty node */
void create()
{
    int data;
 
    temp =(struct node *)malloc(1*sizeof(struct node));
    temp->prev = NULL;
    temp->next = NULL;
    cout<<"\n Enter value to node : ";
    cin>>data;
    temp->n = data;
    count++;
}
 
/*  TO insert at beginning */
void insert1()
{
    if (h == NULL)
    {
        create();
        h = temp;
        temp1 = h;
    }
    else
    {
        create();
        temp->next = h;
        h->prev = temp;
        h = temp;
    }
}
 
/* To insert at end */
void insert2()
{
    if (h == NULL)
    {
        create();
        h = temp;
        temp1 = h;
    }
    else
    {
        create();
        temp1->next = temp;
        temp->prev = temp1;
        temp1 = temp;
    }
}
 
/* To insert at any position */
void insert3()
{
    int pos, i = 2;
 
    cout<<"\n Enter position to be inserted : ";
    cin>>pos;
    temp2 = h;
 
    if ((pos < 1) || (pos >= count + 1))
    {
        cout<<"\n Position out of range to insert";
        return;
    }
    if ((h == NULL) && (pos != 1))
    {
        cout<<"\n Empty list cannot insert other than 1st position";
        return;
    }
    if ((h == NULL) && (pos == 1))
    {
        create();
        h = temp;
        temp1 = h;
        return;
    }
    else
    {
        while (i < pos)
        {
            temp2 = temp2->next;
            i++;
        }
        create();
        temp->prev = temp2;
        temp->next = temp2->next;
        temp2->next->prev = temp;
        temp2->next = temp;
    }
}
 
/* To delete an element */
void del()
{
    int i = 1, pos;
 
    cout<<"\n Enter position to be deleted : ";
    cin>>pos;
    temp2 = h;
 
    if ((pos < 1) || (pos >= count + 1))
    {
        cout<<"\n Error : Position out of range to delete";
        return;
    }
    if (h == NULL)
    {
        cout<<"\n Error : Empty list no elements to delete";
        return;
    }
    else
    {
        while (i < pos)
        {
            temp2 = temp2->next;
            i++;
        }
        if (i == 1)
        {
            if (temp2->next == NULL)
            {
               cout<<"Node deleted from list";
                free(temp2);
                temp2 = h = NULL;
                return;
            }
        }
        if (temp2->next == NULL)
        {
            temp2->prev->next = NULL;
            free(temp2);
            cout<<"Node deleted from list";
            return;
        }
        temp2->next->prev = temp2->prev;
        if (i != 1)
            temp2->prev->next = temp2->next;    /* Might not need this statement if i == 1 check */
        if (i == 1)
            h = temp2->next;
        cout<<"\n Node deleted";
        free(temp2);
    }
    count--;
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here:



Language:

Post Your Reply Here: