C programme for Double Link-list or Doubly by using structure

C programme for Double Link-list or Doubly by using structure

Link to Turbo C: HERE

/* programme of single Double link-list*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>    /* for malloc function */
struct student        /*structure name defined */
{
    int rno;
    char name[20];
    struct student *nadd,*padd;
}*f,*m,*ptr;
void menu();
void create();
void insert();
void insertfirst();
void insertany_before();
void deletefirst();
void deletelast();
void deleteany();
void display();
void ext();
void main()


{
 clrscr();
 menu();
 getch();
 }
void menu()
{
int s;
    do
    {
    int ch;
    clrscr();
    printf("1. Create memory\n");
    printf("2.Insert element\n");
    printf("3.Insert element in first\n");
    printf("4.Insert element before\n");
    printf("5.delete first element\n");
    printf("6.Delete last element\n");
    printf("7.Delete any element\n");
    printf("8.Display element of memory\n");
    printf("9. Exit from programme\n");
    printf("Enter your choice(1-9):\n");
    scanf("%d",&ch);
    {
        if(ch==1)
        {
        create();
        }
        else if(ch==2)
        {
        insert();
        }
        else if(ch==3)
        {
        insertfirst();
        }
        else if(ch==4)
        {
        insertany_before();
        }
        else if(ch==5)
        {
        deletefirst();
        }
        else if(ch==6)
        {
        deletelast();
        }
        else if(ch==7)
        {
        deleteany();
        }
        else if(ch==8)
        {
        display();
        }
        else if(ch==9)
        {
        ext();
        }
        else
        {
        printf("\nYou entered wrong choice!!!...");
        }
    printf("\nDo you want to continue(1/0): ");
    scanf("%d",&s);
    }
    }while(s==1);
}
void create()            /* Create a new memory */
{
    ptr=(struct student *)malloc(sizeof(struct student));
    printf("Enter student name: ");
    scanf("%s",&ptr->name);
    printf("Enter students roll no: ");
    scanf("%d",&ptr->rno);
    ptr->nadd=NULL;
    ptr->padd=NULL;
    f=ptr;
    m=ptr;
}
void insert()            /*Insert memory in last */
{
    ptr=(struct student *)malloc(sizeof(struct student));
    printf("Enter student name: ");
    scanf("%s",&ptr->name);
    printf("Enter students roll no: ");
    scanf("%d",&ptr->rno);
    ptr->nadd=NULL;
    m->nadd=ptr;
    ptr->padd=m;
    m=ptr;
}
void display()         /*to display the memory elements */
{
    struct student *dis;
    dis=f;
    while(dis!=NULL)
    {
        printf("Student name= %s\n",dis->name);
        printf("Student roll no= %d\n\n",dis->rno);
        dis=dis->nadd;
    }
}
void insertfirst()      /*Insert a new memory in first */
{
    ptr=(struct student *)malloc(sizeof(struct student));
    printf("Enter student name: ");
    scanf("%s",&ptr->name);
    printf("Enter students roll no: ");
    scanf("%d",&ptr->rno);
    ptr->nadd=f;
    f->padd=ptr;
    ptr->padd=NULL;
    f=ptr;
}
void deletefirst()    /*deletes first memory */
{
    struct student *temp;
    temp=f;
    f=f->nadd;
    f->padd=NULL;
    free(temp);
    printf("First memory deleted");
}
void deletelast()    /* deletes the last memory */
{
    struct student *temp;
    temp=m;
    m=m->padd;
    m->nadd=NULL;
    free(temp);
}
void deleteany()    /* Delete any middle memory */
{
    struct student *p,*p1,*ob;
    int n,flag=0;
    ob=f;
    printf("Enter student roll no: ");
    scanf("%d",&n);
    while(ob!=NULL)
    {
        if(ob->rno==n)
        {
            flag=1;
            break;
        }
        p=ob;
        ob=ob->nadd;
    }
if(flag==1)
    {
    p1=ob->nadd;
    p->nadd=p1;
    p1->padd=p;
    free(ob);
    printf("Memory deleted");
    }
else
    {
    printf("Roll No. not found");
    }
}
void insertany_before()        /*insert the memory before any memory */
{
    struct student *ob,*p;
    int n,flag=0;
    printf("Enter any roll no.: ");
    scanf("%d",&n);
    ob=f;
    while(ob!=NULL)
    {
        if(ob->rno==n)
        {
            flag=1;
            break;
        }
        p=ob;
        ob=ob->nadd;
    }
if(flag==1)
    {
    struct student *newm;
    newm=(struct student *)malloc(sizeof(struct student));
    printf("Enter student name: ");
    scanf("%s",&newm->name);
    printf("\nEnter student roll no: ");
    scanf("%d",&newm->rno);
    newm->padd=p;
    newm->nadd=ob;
    p->nadd=newm;
    ob->padd=newm;
    }
else
    {
    printf("Roll no not found");
    }
 }
void ext()        /* To exit from programme */
{
    printf("You choose to exit programme:\n");
    printf("Bye Bye!!!...");
    getch();
    exit(0);
}

SHARE

Ramandeep Singh

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 $type={blogger}:

Post a Comment