Program to add/delete/print data using structure.

#include<stdio.h>
#include<string.h>
struct link{
char *n;
int a;
struct link *next;
};
typedef struct link *LINK;
LINK add(LINK list,char *n,int a);
LINK del(LINK list,int a);
LINK print(LINK list);
int main()
{
LINK p=NULL,q,r;
char s[25];
int n,no,i;
printf("enter the no ");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("enter name and age");
scanf("%s %d",s,&n);
p=add(p,s,n);
}
q=print(p);
printf("which node u want to delete");
scanf("%d",&i);
r=del(p,i);
q=print(r);
}
LINK add(LINK list,char *n,int a)
{
LINK p;
p=(LINK)malloc(sizeof(struct link));
p->n=(char *)malloc(25);
strcpy(p->n,n);
p->a=a;
p->next=list;
list=p;
return list;
}

LINK print(LINK list)
{
LINK q;
q=(LINK) malloc(sizeof(struct link));
q->n=(char *)malloc(25);
q=list;
while(q!=NULL)
{
printf("%s\n",q->n);
printf("%d\n",q->a);
q=q->next;
}
return q;
}
LINK del(LINK list,int a)
{
LINK p,q;
int i;
p=(LINK)malloc(sizeof(struct link));
q=(LINK) malloc(sizeof(struct link));
for(q=list;q!=NULL;q=q->next)
{
p->next=q;
if(*q->n==i)
{
p->next=q->next;
free(q);
}
}
return ;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s