Linked List

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Linked List Insert at Beginning

#include<stdio.h>
#include<malloc.h>
void insert_front();
void Display_node();
struct node
{
int data;
struct node *link;
};

struct node *header,*temp,*ptr;

int main()
{
int ch;
header=(struct node*)malloc(sizeof(struct node));
header->data=NULL;
header->link=NULL;
while(1)
{
printf("\n1. Insert at beggining");//Insert at Begging
printf("\n2. Display");
printf("\n3. Exit");
printf("\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_front();
break;
case 2: Display_node();
break;
case 3: exit(0);
break;
}
}
return 0;
}
void insert_front()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d",&temp->data);
temp->link=header->link;
header->link=temp;
printf("Data Inserted Successfully!");
}
void Display_node()
{
ptr=header;
while(ptr->link!=NULL)
{
ptr=ptr->link;
printf("%d\t",ptr->data);
}
}

Linked List Insert at End


#include<stdio.h>
#include<malloc.h>
void insert_end();
void Display_node();
struct node
{
int data;
struct node *link;
};

struct node *header,*temp,*ptr;

int main()
{
int ch;
header=(struct node*)malloc(sizeof(struct node));
header->data=NULL;
header->link=NULL;
while(1)
{
printf("\n1. Insert at End");//Insert at End
printf("\n2. Display");
printf("\n3. Exit");
printf("\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_end();
break;
case 2: Display_node();
break;
case 3: exit(0);
break;
}
}
return 0;
}
void insert_end()
{
temp=(struct node*)malloc(sizeof(struct node));
temp->link=NULL;
ptr=header;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
printf("Enter Data: ");
scanf("%d",&temp->data);
ptr->link=temp;
}
void Display_node()
{
ptr=header;
while(ptr->link!=NULL)
{
ptr=ptr->link;
printf("%d\t",ptr->data);
}
}

You might also like