Uploaded by Animesh Singh

insert at first position

advertisement
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Link
{
int data;
struct Link *next;
};
typedef struct Link node;
node* create(node*,int);
void display(node*);
node* Insert_node_AtFirst_Position(node*,int); //prototyping declaration
void main()
{
node *root=NULL;
int n;
clrscr();
printf("enter the elememt");
scanf("%d",&n);
while(n)
{
root=create(root,n);
printf("enter the element");
scanf("%d", &n);
}
display(root);
root=Insert_node_AtFirst_Position(root,80); //calling function
printf("this is your updated list\n");
display(root);
getch();
}
node* create(node* root, int val)
{
node *temp,*base=root;
temp=(node*)malloc(sizeof(node));
temp->data=val;
temp->next=NULL;
if(root==NULL)
{
return temp;
}
else
{
while(root->next!=NULL)
{
root=root->next;
}
root->next=temp;
}
return base;
}
void display(node* root)
{
//printf("hello");
while(root!=NULL)
{
printf("%d->", root->data);
root=root->next;
}
}
node* Insert_node_AtFirst_Position(node* root,int val)
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=val;
temp->next=NULL;
temp->next=root;
root=temp;
return root;
}
Download