C program for Doubly Linked List

This program is for Doubly linked list in C language.
It can perform Doubly linked list insertion at any place in linked list, deletion and displaying list.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *rptr;
struct node *lptr;
} *l,*r,*temp,*dis;

void insertfirst()
    {
    int i,j;
    //struct node *cout;
    printf("\nEnter the data:");

    scanf("%d",&i);
    temp=(struct node*) malloc(sizeof(struct node));
    temp->info=i;
    if (r==NULL)
        {
        temp->rptr=NULL;
        temp->lptr=NULL;
        l=temp;
        r=temp;
        }
    else
    //if(j==l->info)
        {
        temp->lptr=NULL;
        temp->rptr=l;
        l=temp;
        }
    }
void insertmid()
    {
    int j;
    printf("\nEnter the data before which u want to insert the data:");
    scanf("%d",&j);
    struct node *cout;
    if(j==l->info)
        {
        temp->lptr=NULL;
        temp->rptr=l;
        l=temp;
        }
    else
        {
         cout=l;
         while ((cout->rptr)->info!=j && cout->rptr!=NULL)
            {
            cout=cout->rptr;
            }
         if(cout->rptr==NULL)
             {
             printf("\nEntered data not found before which new node will be inserted\n!!CHECK the entered data");
             }
         else
            {
             temp->rptr=cout->rptr;
             cout->rptr=temp;
             temp->lptr=cout;
             (temp->rptr)->lptr=temp;
             }
         }
}
void insertend()
        {
         struct node *cout;
         cout=l;
         while (cout->rptr!=NULL)
            {
            cout=cout->rptr;
            }
            cout->rptr=temp;
            temp->lptr=cout;
            temp->rptr=NULL;
            r=temp;
         }

void delet()
    {
    int k;
    struct node *tem;
    tem=l;
    printf("\nEnter the node's data to be deleted\nNOTE:(if data is repeated the left most data of them will be deleted):");
    scanf("%d",&k);
    if(l->info==k)
        {

        l=l->rptr;
        l->lptr=NULL;
        tem->rptr=NULL;
        free(tem);
        printf("\nNode deleted");
        }
    else
        {
        while(tem->info!=k && tem->rptr!=NULL)
            {
            tem=tem->rptr;
            }
            if(tem->rptr==NULL)
                {
                if(tem->info==k)
                    {
                    (tem->lptr)->rptr=NULL;
                    r=tem->lptr;
                    tem->lptr=NULL;
                    free(tem);
                    printf("\nNode deleted\n");
                    }
                else
                    {
                    printf("\n!!!Info not found Enter appropriate data\n");
                    }
                }
            else
                {
                (tem->rptr)->lptr=(tem->lptr);
                (tem->lptr)->rptr=tem->rptr;
                tem->rptr=tem->lptr=NULL;
                printf("\nNode deleted\n");
                }
            }
    }
void display()
    {
    dis=l;
    if(l==NULL)
        {
        printf("\n\t!!!Linked List Empty!!!\n\t\tFirst enter some elements.");
        }
    else
        {
        printf("\n\t\tThe Doubly Linked List is as below:\n");
        do
        {
            printf("%d<==>",dis->info);
            dis=dis->rptr;
        }
        while(dis->rptr!=NULL);
        printf("%d<=>NULL",dis->info);
        printf("\nLeft most node =%d\nRight most node =%d",l->info,r->info);
        }
    }
void main()
    {
    int i;
    char ch;
    clrscr();
    do
        {
        printf("\nWhat do u want to do with doubly inked list:\n");
        printf("\n1)Insertion at first node\n2)Insertion in between\n3)Insertion at end\n4)display\n5)Deletion\n");
        scanf("%d",&i);
        switch(i)
            {
            case 1:
                insertfirst();
                break;
            case 2:
                insertmid();
                break;
            case 3:
                insertend();
                break;
            case 4;
                display();
                break;
            case 5:
                delet();
                break;
            deafault:
                printf("\n\n!!!!!Not a Valid choice!!!!!");
                break;
            }
        printf("\ndo u want to continue?(y/n)\n");
        ch=getch();
        }
    while(ch=='y'||ch=='Y');

    }



If any explanation needed feel free to ask.!!! 
ENJOY....

Comments

Popular posts from this blog

Windows phone Wifi connectivity problem.

Create Excel file using Java: Apache POI - PART - 2: Line Chart