WELCOME IN THE SEA OF C

Here you find some good quality programs.
If any one want any type of help in C and C++ then you can mail me.
Google

Sunday, September 21, 2008

Linked List::

These function show different operation perform on Linked list.

'/' is used instead of '<' and '>'.

#include/stdio.h/
#include/conio.h/
#include/alloch.h/

struct
{
int data; /*defining structure*/
struct node *link;
};

/*add a node at the end of a linked list*/
void append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
{
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{
temp=*q;
while(temp->link==NULL)
temp=temp->link;
r=malloc(sizeof(struct node))
r->data=num;
r->link=NULL;
temp->link=r;
}
}

/* add a new node at the beginning of the linked list */
void addatbeg(struct node **q,int num)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp;
}

/* add a new node after the specified numbers nodes */
void addafter(struct node *q,int loc,int num)
{
struct node *temp,*r;
int i;
temp=q;
for(i=0;ilink;
if(temp==NULL)
{
printf("there are %d element in the list",loc);
return;
}
}
r=malloc(sizeof(struct node));
r->data=num;
r->link=temp->link;
temp->link=r;
}

/* display the content of the linked list */
void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d",q->data);
q=q->link;
}
}

/* counts the number of nodes present in the linked list */
int count(struct node *q)
{
int c=0;
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}

/* delete the specified node from the linked list */
void delete(struct node **q,int num)
{
struct node *old,*temp;
temp=*q;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==*q)
*q=temp->link;
else
old->link=temp->link;
free(temp);
return;
}
else
{
old=temp;
temp=temp->link;
}
}
printf("element %d not found",num);
}