DATA STRUCTURES



Admission Enquiry Form

Insertion an element at Rear:

Insertion an element at the rear means old Node link part will have the address of new Node.




Program of insertion in the linked list at rear.

//Insertion in the linked list at rear.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*ptr,*start=NULL,*save;

void create_new_node(int);
void insert_new_node();
void display();

void main()
{
int item;
char ch='y';

while(ch=='y'||ch=='Y')
{
printf("Enter item..\n");
scanf("%d",&item);
create_new_node(item);
insert_new_node();
display();
printf("\nDo you want to enter more nodes press y for yes and n for no...\n");
ch=getch();
}
}//end of main function

void create_new_node(int item)
{
ptr=(int*)malloc(sizeof(struct node));
ptr->data=item;
ptr->link=NULL;
}//end of create_new_node function

void insert_new_node()
{
if(start==NULL)
{
start=ptr;
save=ptr;
}
else
{
start->link=ptr;
start=ptr;
}

}//end of insert_new_node function

void display()
{

struct node *newptr;
newptr=save;

while(newptr!=NULL)
{

printf("%d ->",newptr->data);

newptr=newptr->link;

}

}//end of display function




Insertion of the element at the beginning.

Insertion of element at the beginning means New Node link part will have the address of Old Node.

Program of insertion in the linked list at the beginning.

//Insertion in the linked list at beginning.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*ptr,*start=NULL,*store;

void create_new_node(int);
void insert_new_node();
void display();

void main()
{
int item;
char ch='y';

while(ch=='y'||ch=='Y')
{
printf("Enter item..\n");
scanf("%d",&item);
create_new_node(item);
insert_new_node();
display();
printf("\nDo you want to enter more nodes press y for yes and n for no...\n");
ch=getch();
}
getch();
}//end of main function

void create_new_node(int item)
{
ptr=(int*)malloc(sizeof(struct node));
ptr->data=item;
ptr->link=NULL;
}

void insert_new_node()
{
if(start==NULL)
{
start=ptr;
store=ptr;
}
else
{
ptr->link=start;
start=ptr;
}

}// end of insert_new_node function

void display()
{
struct node *newptr;
newptr=start;
while(newptr!=NULL)
{
printf("<-");
printf("%d ",newptr->data);
newptr=newptr->link;
}
}//end of display function




Insertion of the element at the beginning,in the Middle and at the Last.

//insertion at first,mid,last
#include<stdio.h>
#include<stdlib.h>
struct Node *head=NULL,*ptr;
struct Node
{
int data;
struct Node* link;
};
struct Node* createNode(int value)
{
struct Node* temp;
temp=(struct Node*)malloc(sizeof(struct Node));
temp->data=value;
temp->link=NULL;
return temp;

}//end of function
void addFirst(int value)
{
if(head==NULL)
{
head=createNode(value);
ptr=head;
}
else
{
struct Node *temp;
temp=head;
head=createNode(value);
head->link=temp;
}
}//end of addFirst
void addLast(int value)
{
if(head!=NULL)
{
ptr->link=createNode(value);
ptr=ptr->link;
}

}//end of function
void display()
{
struct Node *ptr;
if(head!=NULL)
{
ptr=head;
printf("\n Elements of List are :\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->link;
}//end of while
}//end of if
else
printf("\n List is empty ?");
}//end of function
void addMid(int value)
{
int count=0,mid,i;
struct Node *temp,*new_node;
temp=head;
while(temp!=NULL)
{
temp=temp->link;
count++;
}
mid=count/2;
printf("\n mid of list is %d",mid);
temp=head;
for(i=0;i<mid;i++)
{
temp=temp->link;
}
new_node=createNode(value);

   new_node->link=temp->link;
temp->link=new_node;
printf("\n Node Added after %d Node :",mid+1);

}//end of function

int main()
{
display();
addFirst(10);
addFirst(20);
addFirst(30);
display();
addLast(40);
addLast(50);
display();
addMid(25);
display();
addMid(35);
display();
printf("\n\n\n");
}//end of main