Search This Blog

Tuesday, 24 September 2013

link list in c with insert dispose delete modify show find functions

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#include<process.h>
void enter_record();
void show_list();
void dispose();
void delete_record();
void find();
void modify();
struct student
{
int value;
*next;
};
struct student *header,*current,*temp;
void main()
{
int ch;
clrscr();
while(1)
{
printf("======================");
printf("\n\n 1.enter record.\n 2.show list.\n 3.dispose.\n 4.delete record.\n 5.find.\n 6.modify.\n 7.exit\n");
printf("======================\n");
ch=getch();
switch(ch)
{
case '1':
enter_record();
            break;
        case '2':
            show_list();
            break;
        case '3':
            dispose();
            break;
        case '4':
            delete_record();
            break;
        case '5':
            find();
        case '6':
            modify();
        case '7':
            exit(1);


        default:
        printf("you have enter wrong choice");

    }
              }
             // getch();
}

void enter_record()
{

printf("\nEnter id: \n");
current=(struct student *)malloc(sizeof(struct student));
scanf("%d",&current->value);
current->next=NULL;

if(header==NULL)
header=current;
else
temp=header;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=current;
}


void show_list()
{
if(header==NULL)
printf("\nlist is empty\n");
temp=header;
while(temp!=NULL)
{
printf("\n\n id is :%d\n",temp->value);
temp=
temp->next;
}
}

void dispose()
{
struct student *temp;
while(header!=NULL)
{
temp=header->next;
free(header);
header=temp;
 }
printf("\nlist is destroy\n");
}
void delete_record()
{
struct student *prev;
int mc;
printf("\nenter id to b deleted: \n");
scanf("%d",&mc);
if(header==NULL)
printf("\nlist is empty\n");
else if(header->value==mc)
{
temp=header->next;
free(header);
header=temp;
}
else
{
temp=header;
while(temp->next!=NULL&&temp->value!=mc)
{
prev=temp;
temp=temp->next;
prev->next=temp->next;
free(temp);
}
prev->next=NULL;
free(temp);
}
}

void find()
{
int m;
temp=header;
printf("\nenter id to find: \n");
scanf("%d",&m);

while(temp->next!=NULL&&temp->value!=m)
{
temp=temp->next;
}
printf("\nthe required id is present in list: %d \n",temp->value);
show_list();
}
void modify()
{
int id,u;
//printf("enter  id");
//scanf("%d",id);
temp=header;
printf("\n enter id to find whether is is present or not\n");
scanf("%d",&id);

while(temp->next!=NULL&&temp->value!=id)
{
temp= temp->next;
}
printf("the required result is present in list: %d ",temp->value);
printf("\nnew id\n");
scanf("%d",&u);
temp->value=u;
printf("\nid modified\n");
show_list();
getch();
}

No comments:

Post a Comment