Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include<stdio.

h>
#include<stdlib.h>
int choice,i,n;
struct node
{
char name[30];
char USN[30];
char branch[30];
int sem;
char phno[30];
struct node*link;
};
struct node*start=NULL,*temp,*cur,*ptr;

void insert_first()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter USN,name,branch,sem,phone no\n");
scanf("%s%s%s%d%s",temp->USN,temp->name,temp->branch,&temp->sem,temp->phno);
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
temp->link=start;
start=temp;
}
}

void insert_end()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter USN,name,branch,sem,phone no\n");
scanf("%s%s%s%d%s",temp->USN,temp->name,temp->branch,&temp->sem,temp->phno);
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
ptr=start;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=temp;
}
}

void display()
{
int count=0;
if(start==NULL)
{
printf("empty\ncount=%d\n",count);
return;
}
else
{
ptr=start;
while(ptr!=NULL)
{
printf("\n%s\t%s\t%s\t%d\t%s\n",ptr->USN,ptr->name,ptr->branch,ptr-
>sem,ptr->phno);
count++;
ptr=ptr->link;
}
printf("total number of nodes:%d\n",count);
}
}

void delete_first()
{
if(start==NULL)
printf("list is empty\n");
else
{
ptr=start;
start=start->link;
free(ptr);
}
}

void delete_end()
{
if(start==NULL)
printf("list is empty\n");
else if(start->link==NULL)
{
free(start);
start=NULL;
}
else
{
cur=start;
ptr=start->link;
while(ptr->link!=NULL)
{
cur=ptr;
ptr=ptr->link;
}
free(ptr);
cur->link=NULL;
}
}

void main()
{
while(1)
{
printf("press 1.creat SLL 2.insert first 3.display 4.insert end 5.delete
first 6.delete end 7.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter the number of Employees\n");
scanf("%d",&n);
for(i=0;i<n;i++)
insert_first();
break;
case 2:insert_first();
break;
case 3:display();
break;
case 4:insert_end();
break;
case 5:delete_first();
break;
case 6:delete_end();
break;
case 7:exit(0);
}
}
}

You might also like