7. Data Structure Program | Read Now

Data Structure VTU lab program -07

  • Design, Develop and Implement a menu driven Program in C for the following operations on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo
    1. Create a SLL of N Students Data by using front insertion.
    2. Display the status of SLL and count the number of nodes in it
    3. Perform Insertion / Deletion at End of SLL
    4. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
    5. Exit

Program-7 code [sll.c]

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
struct stud
{
    char usn[11],name[15],branch[4],phno[11];
    int sem;
    struct stud *next;
}*f=NULL,*r=NULL,*t=NULL;
void ins(int ch)
{
    t=(struct stud*)malloc(sizeof(struct stud));
    printf("\nEnter USN:");
    scanf("%s",t->usn);
    printf("Enter Name:");
    scanf("%s",t->name);
    printf("Enter Branch:");
    scanf("%s",t->branch);
    printf("Enter Sem:");
    scanf("%d",&t->sem);
    printf("Enter Phno:");
    scanf("%s",t->phno);
    t->next=NULL;
    if(!r)
        f=r=t;
    else
    {
        if(ch)
        {
            r->next=t;
            r=t;
        }
        else
        {
            t->next=f;
            f=t;
        }
    }
}
void del(int ch)
{
    if(!f)
        printf("\nList Empty");
    else
    {
        struct stud *t1;
        if(f==r)
        {
            t1=f;
            f=r=NULL;
        }
        else if(ch)
        {
            t1=r;
            for(t=f;t->next!=r;t=t->next)
                r=t;
            r->next=NULL;
        }
        else
        {
            t1=f;
            f=f->next;
        }
        printf("\nElement deleted is:\n");
        printf("USN:%s\nName:%s\nBranch:%s\nSem:%d\nPhno:%s\n",t1->usn,t1->name,t1->branch,t1->sem,t1->phno);
        free(t1);
    }
}
void disp()
{
    if(!f)
        printf("\nList Empty!!!");
    else
        printf("\nList elements are:\n");
    for(t=f;t;t=t->next)
        printf("\nUSN:%s\nName:%s\nBranch:%s\nSem:%d\nPhno:%s\n",t->usn,t->name,t->branch,t->sem,t->phno);
}
void main()
{
    int ch,n,i;
    printf("\n........Menu..........,\n");
    printf("1.Create\n");
    printf("2.Display\n");
    printf("3.Insert at end\n");
    printf("4.Delete at end\n");
    printf("5.Insert at beg\n");
    printf("6.Delete at beg\n");
    printf("7.Exit\n");
    while(1)
    {
        printf("\nEnter choice:");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: printf("\nEnter no. of nodes:");
                    scanf("%d",&n);
                    for(i=0;i<n;i++)
                        ins(0);
                    break;
            case 2:disp();break;
            case 3:ins(1);break;
            case 4:del(1);break;
            case 5:ins(0);break;
            case 6:del(0);break;
            case 7:exit(0);
            default:printf("\nInvalid choice!!!!");
        }
    }

}

Data Structure -How to Run this Program

  • Step-1: Copy the above code
  • Step-2: Paste it in any C compiler [Codeblocks/Dev C++/ VsCode etc]
  • Step-3: Save the file name with .C extension
  • Step-4: Compile the program
  • Step-5: Run the program
  • Step-6: Program Execution Successful

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp Icon Join For Job Alerts