Skip to content

Instantly share code, notes, and snippets.

@arn-ob
Created August 1, 2015 21:04
Show Gist options
  • Save arn-ob/eceacb6e9a21cfd1f05e to your computer and use it in GitHub Desktop.
Save arn-ob/eceacb6e9a21cfd1f05e to your computer and use it in GitHub Desktop.
Inserting a value into a Sorted Linked List
//
// Inserted a value into a Sorted Linked List
//
#include<stdio.h>
#define ArryNumber 20
// Global Variable
int INFO[ArryNumber]; //INFO[20];
int LINK[ArryNumber]; //LINK[20];
int START , AVAIL;
void INSLOC(int,int);
int FINDA(int);
// Main Function
void main()
{
//cmt : Local Variable
int PTR , ITEM , LOC;
// You can write like this
INFO[2] = 15; LINK[2] = 4;
INFO[4] = 20; LINK[4] = 6;
INFO[6] = 25; LINK[6] = 8;
INFO[8] = 30; LINK[8] = 10;
INFO[10] = 36; LINK[10] = -1;
LINK[11] = 1;
// Or this
/*
INFO[2] = 15; INFO[4] = 20; INFO[6] = 25; INFO[8] = 30;INFO[10] = 36;
LINK[2] = 4; LINK[4] = 6; LINK[6] = 8; LINK[8] = 10; LINK[10] = -1; LINK[11] = 1;
*/
START = 2;
AVAIL = 11;
PTR = START;
printf(" LIST : \n\n");
while(PTR != -1)
{
printf("\t%d\n",INFO[PTR]);
PTR = LINK[PTR];
}
printf("\n\n Enter The ITEM to be inserted : ");
scanf("%d",&ITEM);
LOC = FINDA(ITEM);
INSLOC(ITEM,LOC);
PTR = START;
printf("\n\n");
printf(" Modified LIST : \n\n");
while(PTR != -1)
{
printf("\t%d\n",INFO[PTR]);
PTR = LINK[PTR];
}
}
//Sub Function
void INSLOC(int I,int L)
{
int NEW;
if(AVAIL == NULL )
{
printf("\nOverload");
exit(1);
}
NEW = AVAIL;
INFO[NEW] = I;
if(L == -1)
{
LINK[NEW] = LINK[L];
LINK[L] = NEW;
}
else
{
LINK[NEW] = LINK[L];
LINK[L] = NEW;
}
}
//Sub Function
int FINDA(int I)
{
int L , SAVE ,P;
if(START == -1)
{
L = -1;
return(L);
}
if(I < INFO[START])
{
L = -1;
return(L);
}
SAVE = START;
P = LINK[START];
while(P != -1)
{
if(I < INFO[P])
{
L = SAVE;
return(L);
}
SAVE = P;
P = LINK[P];
}
L = SAVE;
return(SAVE);
}
// Link Create and Coded by EWU.ICE.Student.Fall13.Name("Arnob")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment