C programme for Single Link-List by using Structure

C programme for Single Link-List by using Structure


Link to Turbo C: HERE 

/* programme of single 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 *add;
}*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->add=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->add=NULL;
    m->add=ptr;
    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->add;
    }
}
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->add=f;
    f=ptr;
}
void deletefirst()    /*deletes first memory */
{
    struct student *temp;
    temp=f;
    f=f->add;
    free(temp);
    printf("First memory deleted");
}
void deletelast()    /* deletes the last memory */
{
    struct student *j,*k;
    j=f;
    while(j->add!=NULL)
    {
        k=j;
        j=j->add;
    }
    m=k;
    m->add=NULL;
    free(j);
}
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->add!=NULL)
    {
        if(ob->rno==n)
        {
            flag=1;
            break;
        }
        p=ob;
        ob=ob->add;
    }
if(flag==1)
    {
    p1=ob->add;
    p->add=p1;
    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->add!=NULL)
    {
        if(ob->rno==n)
        {
            flag=1;
            break;
        }
        p=ob;
        ob=ob->add;
    }
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=p->add;
    ob=newm->add;
    }
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