C program to implement linked list

Below given C program implements linked 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
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct list
{
    int data;
    struct list *next,*prev;
}*head=NULL;
void main()
{
    void insert();
    void del();
    int c;
    clrscr();
 
    /****************************************************************/
 
    printf("\n\tOUTPUT::\n");
    printf("\n\t1:insert \t\t2:delete\n\t3:exit");
    while(1)
    {
        printf("\n\tENTER CHOICE: ");
        scanf("%d",&c);
        switch(c)
        {
        case 1: insert();  break;
        case 2: del();  break;
        case 3: exit(1);
        }
    }
}
/*************************************************************/
void insert()
{
    struct list *temp,*new1,*pr;
    int pos,i=1;
    char ch;
    temp=head;
    printf("\tENTER POSITION TO BE INSERTED: ");
    scanf("%d",&pos);
    new1=(struct list*)malloc(sizeof(struct list));
    printf("\tENTER DATA: ");
    scanf("%d",&new1->data);
    if(pos==1)
    {
        new1->next=temp;
        head=new1;
    }
    else
    {
        while(i
            <pos)
        {
            pr=temp;
            temp=temp->next;
            i++;
        }
        temp->prev=new1;
        new1->next=temp;
        new1->prev=pr;
        pr->next=new1;
    }
}
/****************************************************************/
void del()
{
    struct list *temp,*pr,*t;
    int pos,i=1;
    temp=pr=head;
    printf("\tENTER POSITION TO BE DELETED: ");
    scanf("%d",&pos);
    temp=head;
    while(i
        <pos)
    {
        pr=temp;
        temp=temp->next;
        i++;
    }
    t=temp;
    temp=temp->next;
    pr->next=temp;
    temp->prev=pr;
    free(t);
}

2 Responses to “C program to implement linked list”

Leave a Reply