EmbLogic's Blog

link list operation

—test——–
#! /bin/bash
rcs -i header.h
rcs -i llist_ato_sir.c
ci header.h
ci llist_ato_sir.c
co header.h
co llist_ato_sir.c
gcc -o result llist_ato_sir.c
./result
rm -rf header.h
rm -rf llist_ato_sir.c
rm -rf result

//——-header file——
@#include
#include
struct link
{
int info;
struct link *next;
};
static int node_no=0;
struct link *creat_node();
struct link *delete_node(struct link *);
int delete_at_end(struct link *);
struct link* delete_at_beg(struct link *);
int delete_at_pos(struct link *);
struct link * delete_key(struct link *);
struct link *insert_node(struct link *);
struct link *node_at_beg(struct link *);
int node_at_pos(struct link *);
int node_at_end(struct link *);
int node_key(struct link *);
int traverse_ll(struct link *);
int getchoice(int );
int search_element(struct link *);
@

//—————link list main function——–
@#include”header.h”
struct link *create_node()
{

struct link *temp;
temp=(struct link *)malloc(sizeof(struct link));
printf(“\n enter the data :”);
scanf(“%d”,&temp->info);
temp->next=NULL;
node_no++;
return temp;
}
delete_at_end(struct link *temp)
{
struct link *end,*p;
end=temp;
while(end->next!=NULL)
{
p=end;
end=end->next;
}
p->next=NULL;
free(end);
return 0;
}

struct link* delete_at_beg(struct link *temp)
{
struct link *beg;
beg=temp;
temp=beg->next;
free(beg);

return temp;
}
delete_at_pos(struct link *temp)
{
int node,i;
struct link *nnode=temp,*pnode=temp,*r=NULL;
printf(“\n you have %d node”,node_no);
printf(“\n which node you want to delete”);
scanf(“%d”,&node);
if(node==2)
{

struct link *p,*q;

p=temp->next;
q=p->next;
temp->next=q;

free(p);
node_no–;

}
else
{
for(i=0;inext;

}

for(i=0;inext;

}
pnode->next=nnode;
node_no–;
}
return 0;

}
struct link *delete_key(struct link *temp)
{
int val,flag=0;
struct link *p=temp,*r;
printf(“\n enter the value whose node you want to delete :\n”);
scanf(“%d”,&val);
if(val==temp->info)
{
flag=1;
node_no–;
temp=temp->next;
free(p);
return temp;
}
else
{
while(p!=NULL)
{
if(p->info==val)
{
r->next=p->next;
flag=1;
node_no–;
free(p);
break;

}
r=p;
p=p->next;

}
}
if(flag==0)
{
printf(“\n entered data is not present in link list \n”);

}
return temp;
}
struct link*delete_node(struct link *temp)
{

printf(“\n\t 1.delete node at begining”);

printf(“\n\t 2. delete node at position”);
printf(“\n\t 3.delete node at end”);
printf(“\n\t 4.delete node at key value”);
int choice;
printf(“\n enter your choice :”);
scanf(“%d”,&choice);

switch(choice)
{
case 1:
temp=delete_at_beg(temp);
break;
case 2:
delete_at_pos(temp);
break;
case 3:
delete_at_end(temp);
break;
case 4:
temp=delete_key(temp);
break;
default:
printf(“\n \aenter right choice”);
}
return temp;
}

/*struct link *delete_node(struct link *temp)
{
int node,i;
struct link *nnode=temp,*pnode=temp,*r=NULL;
printf(“\n you have %d node”,node_no);
printf(“\n which node you want to delete”);
scanf(“%d”,&node);
if(node==1)
{
struct link *q;
//q=(struct link *)malloc(sizeof(struct link));
q=temp;
temp=temp->next;

free(q);

node_no–;
}
else if(node==2)
{
// nnode=temp->next;
// nnode=nnode->next;
struct link *p,*q;
p=(struct link *)malloc(sizeof(struct link));
p=temp->next;
q=temp->next;
q=q->next;
//temp->next=temp->next->next;

free(p);
node_no–;

}
else
{
for(i=0;inext;

}

for(i=0;inext;
}
pnode->next=nnode;
node_no–;
}
return temp;
}*/
struct link *node_at_beg(struct link *temp)
{
struct link *beg;
beg=create_node();
beg->next=temp;

return beg;
}
node_at_pos(struct link *temp)
{
int position,i;
struct link *new,*pos=temp;
printf(“\n no. of node in link list :%d”,node_no);
printf(“\n please enter the position where you want to insert node :”);
scanf(“%d”,&position);
if(position>node_no)
{
printf(“\n enter the position less than total node ..!!”);
goto out;
}
new=create_node();
for(i=1;inext;
}
new->next=pos;
for(i=1;inext;
}
temp->next=new;
return 0;
out:
return 0;
}
node_at_end(struct link *temp)
{
struct link *end;

end=create_node();
while(temp->next!=NULL)
{
temp=temp->next;
}

temp->next=end;
return 0;
}

node_key(struct link *temp)
{
int val,count=0;

struct link *key=temp,*insert;
printf(“\n enter the key value where you want to add node :”);
scanf(“%d”,&val);
while(key!=NULL)
{
if(val==key->info)
{
insert=create_node();
insert->next=key->next;
key->next=insert;
count++;
break;
}
key=key->next;
}
if(count==0)

printf(“\n key value is not present in link list”);
return 0;
}
struct link *insert_node(struct link *temp)
{
printf(“\n\t 1.insert node at begining”);

printf(“\n\t 2. insert node at position”);
printf(“\n\t 3.insert node at end”);
printf(“\n\t 4.insert node at key value”);
int choice;
printf(“\n enter choice:”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
temp=node_at_beg(temp);
break;
case 2:
node_at_pos(temp);
break;
case 3:
node_at_end(temp);
break;
case 4:
node_key(temp);
break;
default:
printf(“\n \aenter right choice”);
}

return temp;
}

traverse_llist(struct link *temp)
{
while(temp!=NULL)
{
printf(“\n node no.—> %d”,temp->info);
temp=temp->next;
}
return 0;
}
search_element(struct link *temp)
{
int no,count=0,flag=0;
printf(“\n enter the no. which you want to search :”);
scanf(“%d”,&no);
while(temp!=NULL)
{
count++;
if(no==temp->info)
{
flag=1;
printf(“\nyour search for %d found”,no);
break;
}
temp=temp->next;
}
if(flag==0)
{
printf(“\nyour searching no. is not present in link list”);
}
return 0;
}
getchoice(int choice)
{

static int flag=0;
struct link *new,*start;
switch(choice)
{

case 1:
if(flag==0)
{
new=start=create_node();
printf(“\n link list created”);
flag=1;
}
else
printf(“\n link list created already”);
break;

case 2:
if(flag==1)
new=insert_node(new);
else
printf(“\n create link list first”);
break;
case 3:
if(flag==1)
new=delete_node(new);
else
printf(“\n create link list first”);
break;
case 4:
if(flag==1)
traverse_llist(new);
else
printf(“\n create link list first”);
break;
case 5:
search_element(new);
break;
case 6:
// sorting_element();
break;
default:
printf(“enter right choice”);

case 0:
exit(0);

}
return 0;

}

int main()
{
int choice;
printf(“——-MAIN MENU———-”);
while(1)
{
printf(“\n 1.CREATE LINk LIST———”);
printf(“\n 2.INSERT NODE————–”);
printf(“\n 3.DELETE NODE————–”);
printf(“\n 4.TRAVERSE LINK LIST——-”);
printf(“\n 5.SEARCHING ELEMENT________”);
printf(“\n 6.SORTING ELEMENT__________”);
printf(“\n 0.EXIT LINKLIST___________”);
printf(“\n enter choice :”);
scanf(“%d”,&choice);
getchoice(choice);
}
return 0;
}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>