/////BASE ADDRESS OF LINKLIST SHOULD NEVER BE LOST IF UB WANT TO ACCESS THE LINKLIST.....
//TO RECOGNISE LINKLIST I HAVE TWO TRICKS:-
////1)IF start or any pointer->next is on left side means ,,,we are putting to memory at that pointer into another pointer |
//|or memory by replacing other memory...
////2)IF WE ARE PUTTING NORMAL NAME LIKE temp into another pointer or memory...means we are putting memory to another pointe
//r or relacing memory of another normal type name ..//
////POINTS TO REMEMBER:---
////ALWAYS REMEMBER...2 POINTERS CAN POINT SAME MEMORY BUT TWO MEMORIES COULD NOT BE HOLD BY SAME POINTER
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;//info is normal int type variable ...so can not be a cause of linklist...bcz..this is a simple element
struct node *next;//wow///this is not a simple variable // this is pointer like variable of structure...inside the structure//variable is structure inside the structure pointing to itself when act as element
//this element of structure is also acting like a variable of structure...
};
int flag,node_no;//globally declared
struct node* create_linklist();//all the functions are always globally declared...in header file ...
/*A LINKLIST IS NOTHING BUT POINTER OF A STRUCTURE ACTING LIKE A VARIABLE OF STRUCTURE..CONTAINED IN SAME STRUCTURE AS ELEMENT OF STRUCTURE.....POINTER OF STRUCTURE AS LIKE VARIABLE OF STRUCTURE IS POINTING TO ITSELF AS ELEMENT..& AGAIN THAT ELEMENT IS AGAIN ACTING LIKE A VARIABLE POINTING TO ITSELF AS ELEMENT...AGAIN THAT VARIABLE POINTER OF STRUCTURE POINTING TO ITSELF AS ELEMENT... POINTER ACTING LIKE VARIABLE OF STRUCTURE HOLDING ADDRESS OF A PERTICULAR MEMORY.. ....SO LINKLIST IS CREATED UNTILL...WE ARE ALLOCAING ITS MEMORY TO NEXT & NEXT & NEXT...AGAIN & AGAIN */
#include"header.h"//THAT STRUCTURE IS IN HEADER.H
void display(struct node* start)
{
struct node* temp;
temp=start;//put the base address of node into temp
int node_no=0;//node number is 0 at staring
if(start)
{
temp->info=node_no;//put node no. also into temp->info another element of struct node
printf("\nnode[%d] temp->info=%d temp->next=%d ",node_no,temp->info,temp->next);//print node no..& address of temp->next...or u may print temp...0 th node address
printf("\nnode[%d] temp->info=%d temp->next=%d ",node_no,temp->info,temp->next);//print temp->next..address of next node & node no. of next node every time untill nodes are finished & temp->next==NULL...//
int node=0,fleg=1;///every time when case will be selected & this fuction is called ..values will be initialised..
temp=prev=new;//BASE ADDRESS OF LINKLIST IS PUT INTO temp & prev
while(temp->next)
{
if(fleg==0)
{
prev=prev->next;//
node++;///node will count every even no. of node..//node no 2 4 6 8 actually remains 1 ..whenever node become 1 ..node is removed but after ..making flag==1 means moving prev..near to temp... are removed 1 will not bcz it is a base address....
fleg=1;
printf("\nMOVEMENT OF TEMP");
}
else if(fleg==1)
{
printf("\nMOVEMENT OF PREVOIUS");
temp=temp->next;///temp will always lie 1 ahead from prev due to fleg...1st time only temp moves
fleg=0;//at every even no. of node flag is made 0
}
if(node>0 && fleg==0)
{
prev->next=temp->next;//put address of node next to temp into prev->next
free(temp);//free temp
temp->next=NULL;//make temp->next=NULL bcz..it was 1st pointing to node next to temp..
temp=prev->next;//now node next to temp is always made temp to remove 1 more node leaving this node...//
int node=0,fleg=1;//every time when case will be selected & this fuction is called ..values will be initialised..
temp=prev=new;//PUT BASE ADDRESS OF LINKLIST INTO temp & prev...
while(temp->next)
{
if(fleg==1)
{
temp=temp->next;//temp will always lie 1 ahead from prev due to fleg...1st time only temp moves
fleg=0;
node++;//node will count every odd no. of node..//node no 3 5 7 9 actually remains 1 ..whenever node become 1 ..node is removed but after ..making flag==1 means moving prev..near to temp... are removed 1 will not bcz it is a base address....
printf("\nMOVEMENT OF TEMP");
}
else if(fleg==0)//1 time it will go into if & other time into else....
{
printf("\nMOVEMENT OF PREVOIUS");
prev=prev->next;
fleg=1;//flag==1 after making node=odd & moving prev also....odd temp is removed
}
if(node>0 && fleg==1)//so every time when flag==1...temp is removed
{
//node odd & prev moved ...also with that//
prev->next=temp->next;//next memory of temp node is put into prev->next pointer...
free(temp);//free temp
temp->next=NULL;//temp->next which was pointing to memory to next node of temp is now made NULL..
temp=prev->next;//now next even node is made temp;;;every time when node is removed ..next node to node which is removed is made temp to remove 1 more odd leaving this node...& by making nod=1 again
temp=temp->next;//temp is moved to 2nd node/..means memory of 2nd node temp->next is put into temp..means 2nd node is made temp...or we can say name of 2nd node is now temp....//
new->next=temp->next;//now temp is of 2nd node ..temp->next memory means memory of 3rd node is put into..new->nextinto pointer of 1st node...means pointer of 1st node is now pointing to memory of 3rd node but still ..pointer of 2nd node is also pointing to memory of 3rd node
free(temp);//free temp.//make memory of temp further usable...
temp->next=NULL;//make temp->next=NULL which was pointing before to memory of 3rd node///now 2nd node is properlly removed after attaching 1st & 3rd node.....& making temp->next=NULL..//
return 0;
}
int delete_end(struct node* new)//I have to delete the node from end
{
int fleg=0;
struct node *temp,*prev;
prev=new;
temp=prev;//put base address of node into both temp & prev
while(temp->next != NULL)
{
if(fleg==0)//when flag=0 only temp will move to next node
{
temp=temp->next;//then from 2nd time both temp & prev will move means prev will always remain 1 node behind the temp...or we can say temp will always remain 1 node forword to prev.....
fleg=1;
}
else if(fleg==1)
{
prev=prev->next;
fleg=0;
}
}//loop will move both prev & temp untill temp->next != NULL means temp reaches to last node & prev reaches to 2nd last node.....
if(temp->next==NULL)//when temp is last node
{
temp=NULL;
free(temp);//make free temp i.e. last node
prev->next=NULL;//put prev->next =NULL means next 2nd last node is made NULL...which is now last node...tocheck the condition again ...for NULL ...TO DELETE one more node at end (while (tem->next) != NULL) ......//
temp=create_node();//malloc this temp normal pointer of struct node..with same memory as that of structure,,8 bytes
//TO RECOGNISE LINKLIST I HAVE TWO TRICKS:-
//1)IF start or any pointer->next is on left side means ,,,we are putting to memory at that pointer into another pointer ||or memory by replacing other memory...
//2)IF WE ARE PUTTING NORMAL NAME LIKE temp into another pointer or memory...means we are putting memory to another pointer or relacing memory of another normal type name ..//
//POINTS TO REMEMBER:---
//ALWAYS REMEMBER...2 POINTERS CAN POINT SAME MEMORY BUT TWO MEMORIES COULD NOT BE HOLD BY SAME POINTER
temp->next=start->next;//memory holded by start->next..means of 2nd node memory is put into next of temp...now pointer start->next is also pointing to same 2nd node memory...
start->next=temp;//put memory of temp into start->next...will remove the pointer from 2nd node..which is now 3rd node& place the memory of temp node which is now 2nd node into it..//
temp=create_node();//malloc temp...this is called node ..now this node is to be place into end of other nodes that are made before
if(!temp)
{
perror("malloc");
}
while(new->next !=NULL)
{
//new->next=temp;
new=new->next;//move new->next->next->next...till the end node whose next is NULL
}
new->next=temp;//place memory of temp into next of end node
temp->next=NULL;//make now temp is end node ..make temp->next==NULL for next time condition checking node of node...
return 0;
}
int insert(struct node* new)
{
int choice;
printf("\ncase 1:INSERT AT BEGIN");
printf("\ncase 2:INSERT AT END");//menu of insertion the node is presented
printf("\ncase 3:insert the node at n th point");
printf("\ncase 4:insert node at any key value");
printf("\ncase 5:exit");
printf("\nCHOICE FOR INSERT");
scanf("%d",&choice);
switch(choice)
{
case 1:
// display(new);
insert_begin(new);//if u want to insert the node at beginging
//suppose we has already 3 nodes...means new..new->next...new->next->next...
//i have to insert the node between new & new->next...
//display(new);
break;
case 2:
insert_end(new);//if u want to insert the node in end
//suppose we has already 3 nodes...means new..new->next...new->next->next...
//I have to place the node new->next->next->next at new->next->next......node means mallocing to one more next to pointer which will further point to info again & also to itself....
struct node *start,*new;//declare pointer variable of struct node...which are used to make a linklist...
printf("\ncase 1: TO CREATE BASE OF LINKLIST");//menu is presented what we want to do...1st base address creation is very compulsory to create linklist....
printf("\ncase 2: INSERT node");
printf("\ncase 3: DELETE node");
printf("\ncase 4:DISPLAY node");
printf("\ncase 5:SORTING");
printf("\ncase 6: EXIT");
// start=create_linklist();//pointer is pointed to start
new=start;//made both start & new NULL 1st
do
{
printf("\nEnter the IST choice");
scanf("%d",&choice);//enter choice wt u want to do
if(choice>5 || choice<0)//choice should be between..0 to 5 for valid cases of switch
{
printf("\nchoice is invalid");
}
//start=create_linklist();//pointer is pointed to start
switch(choice)//choose cases
{
case 1:
if(flag==0)
{
new=start=create_linklist();//pointer is pointed to start //base address is allocated once /...//in 1 execution of program..//or we can say in lifetime of program..untill program is alive...//
printf("\nbase of linklist is created succesfully=%d",start);
//display(start);
// new->next=NULL;
}
else
printf("\nnode is already created");//when once flag is done 1 in create_linklist..//then again base address will not be provided ....try either anymuch of times
//display(start);
break;//as if we think i could say switch as a loop like if in which after break it does not continue but in if loop it totally go out of loop &in switch control goes back to switch.