ACP--- (UNIT--3) Linked list program 1

Linked list program 1 (UNIT-3)


#include<stdio.h>
#include<conio.h>
#include<alloc.h>

void create(void);
void display(void);
void inserta(void);
void insertb(void);
void insertc(void);
void deletea(void);
void deleteb(void);
void deletec(void);

struct list
{
int no;
struct list *next;
}*start,*list,*temp;

void main()
{
int ans=10;
clrscr();
while(ans!=9)
{
clrscr();
printf("\nEnter choice = ");
printf("\n1. Create ");
printf("\n2. Display");
printf("\n3. Insert @ start");
printf("\n4. Insert @ between");
printf("\n5. Insert @ end");
printf("\n6. Delete @ start");
printf("\n7. Delete @ between");
printf("\n8. Delete @ end");
printf("\n9. Exit ");

printf("enter your choice = ");
flushall();
scanf("%d",&ans);

switch(ans)
{
case 1: create(); break;
case 2: display(); break;
case 3: inserta(); break;
case 4: insertb(); break;
case 5: insertc(); break;
case 6: deletea(); break;
case 7: deleteb(); break;
case 8: deletec(); break;
case 9: exit();
default : printf("\n\nEnter your choice (1 to 9) = ");
getch();
}
}
}

void create()
{
char choice='Y';
clrscr();

printf("\n\n\t\tWelcome to link list creation....!!!\n");
start=(struct list *)malloc(sizeof(struct list));
list=start;

while(toupper (choice)=='Y')
{
printf("\nPl. Enter num = "); flushall();
scanf("%d",&list->no);
printf("\nDo you want to continue (Y / N) = "); flushall();
choice=getche();
if(toupper (choice)=='Y')
{
list->next= (struct list *)malloc(sizeof(struct list));
list=list->next;
}
else
{
list->next=NULL;
}
}

}

void display()
{
list=start;
if(list==NULL)
{
printf("\n\nPlease enter value first...!");
}

while(list!=NULL)
{
printf("\n%d",list->no);
list=list->next;
}
getch();
}

void inserta()
{
temp=(struct list *)malloc(sizeof(struct list));
printf("Enter which no you want to add = ");
scanf("%d",&temp->no);
temp->next=start;
start=temp;
display();
}

void insertb()
{
int value;
printf("Enter position value  = ");
scanf("%d",&value);
list=start;
while(list->no!=value)
{
list=list->next;
}
temp=(struct list *)malloc(sizeof(struct list));
printf("Enter which num you want to add =");
scanf("%d",&temp->no);
temp->next=list->next;
list->next=temp;
display();
}

void insertc() //to insert in last
{
list=start;
while(list->next!=NULL)
{
list=list->next;
}
temp=(struct list *)malloc(sizeof(struct list));
printf("Enter value to add = ");  flushall();
scanf("%d",&temp->no);
list->next=temp;
temp->next=NULL;
display();
}

void deletea()
{
start=start->next;
display();
}

void deleteb()
{
int del=0;
list=start;
printf("Enter which num u want to delete = ");
flushall();
scanf("%d",&del);
while(list->next->no != del)
{
list=list->next;
}
list->next=list->next->next;
display();
}

void deletec()   //to delete at last
{
list=start;
while(list->next->next != NULL)
{
list=list->next;
}
list->next=NULL;
display();
}



Comments

Popular posts from this blog

C++ Practice Program

Cloud Computing Important Question-Answer for University Exam

Software Testing Gujarat University Important Questions For Exam