i want to add a new item to end of linked list..my function addlast will do this
this is my code
Code:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef linked_list node;
void main()
{
void create(node *);
void print(node *);
node *addfirst(node *);
node *addlast(node *);
clrscr();
node *head;
head=(node*)malloc(sizeof(node));
create(head);
print(head);
printf("\n\n\n\n");
head=addfirst(head);
print(head);
printf("\n\n\n\n");
head= addlast(head);
print(head);
getch();
}
void create(node *p)
{
printf("Enter the number : \n");
scanf("%d",&p->number);
if(p->number==999)
{
p->next=NULL;
}
else
{
p->next=(node*)malloc(sizeof(node));
create(p->next);
}
return;
}
void print(node *p)
{
if(p->next!=NULL)
{
printf("%d-->",p->number);
print(p->next);
}
}
node *addfirst(node *p)
{
node *nu;
nu=(node *)malloc(sizeof(node));
printf("Enter the new number to be inserted in the begining :");
scanf("%d",&nu->number);
nu->next=p;
p=nu;
return(p);
}
node *addlast(node *p)
{
node *nu;
node *q;
q=p;
nu=(node *)malloc(sizeof(node));
printf("\n\nEnter the number to be inserted at the last : ");
scanf("%d",&nu->number);
while(q->next!=NULL)
{
q=q->next;
}
nu=q;
q->next=nu;
nu->next=NULL;
return(p);
}