You are on page 1of 4

/******************************************************************************

* Filename : linkedlist.c
* Author : Education & Research Dept, Infosys Technologies Limited
* Date : 30-March-2009
* Description : Explains to create a singly linked list
******************************************************************************/

/* Include files */
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>

/*Declaration of node structure*/


typedef struct _node
{
int iData;
struct _node * psNext;
}node;

/* Declaration of head pointer*/


node *gpsHead;

/* Declaration of functions*/
node * fnAllocateMemory();
void fnInsert();
void fnInsertBeginning(node *);
void fnInsertEnd(node *,node *);
void fnDisplay();

/******************************************************************************
* Function : main
* Description : main function of the program explains the working of
* a linked list by creating the list in sorted order
* Input Parameters:
* int argc - Number of command line arguments
* char **argv The command line arguments passed
* Returns: 0 on success to the operating system
******************************************************************************/

int main(int argc,char **argv){


int iChoice;
do
{
system("cls");
printf("\t\tSingly Linked List Operations\n");
printf("\t\t------------------------------------------\n");
printf("\t\tChoices should be entered from the keyboard\n\n");
printf("\t\t1. Insert operation\n");
printf("\t\t2. Delete Operation\n");
printf("\t\t3. Display the contents of the Linked List \n" );
printf("\t\t4. Exit\n");
printf("\n\t\tEnter the choice from the keyboard");
scanf("%d",&iChoice);

switch(iChoice) {
case 1: /* Inserts into the Linked List */
fnInsert();
break;
case 2: /*Displays contents of the linked list*/
fnDisplay();
break;
case 3: exit(0);
}
}while(iChoice>0 && iChoice <=3);
return 0;
}

/******************************************************************************
* Function : fnAllocateMemory
* Description : Does dynamic allocation of memory
* Input Parameters:
* Returns: It returns the address of the node created
******************************************************************************/

node * fnAllocateMemory()
{
node *psNw;
psNw=(node*)malloc(sizeof(node));
if(psNw){
return psNw;
}
else{
return NULL;
}
}

/******************************************************************************
* Function : fnInsert
* Description : Inserts data into the linked list in sorted manner
* Input Parameters:None
* Returns:None
******************************************************************************/

void fnInsert()
{
char cChoice='y';
node *psNew,*psTemp,*psPrev;
while(cChoice=='y')
{
/* Create a new node*/
printf("\nCreating a new node....\n");
psNew=fnAllocateMemory();

/*Fill the new node*/


printf("\nFilling data into the new node....\n");
psNew->psNext=NULL;
printf("\nEnter the data:");
scanf("%d",&(psNew->iData));

/* Check for existence of linked list*/


if(gpsHead==NULL){
printf("\nLinked list does not exist, adding first node\n");
gpsHead=psNew;
}
else{
for(psTemp=gpsHead;psTemp && psTemp->iData < psNew->iData;\
psPrev=psTemp,psTemp=psTemp->psNext);
if(psTemp==gpsHead){
printf("The new node is to be inserted in the beginning\n");
fnInsertBeginning(psNew);
}
else{
printf("The node is to be inserted at the end \n");
fnInsertEnd(psPrev,psNew);
}
}
printf("Do you want to continue adding nodes(y or Y):");
fflush(stdin);
scanf("%c",&cChoice);
}
}

/******************************************************************************
* Function : fnDisplay
* Description : Displays data from the linked list
* Input Parameters:None
* Returns:None
******************************************************************************/
void fnDisplay()
{
node *psTemp;
printf("\n The elements of the linked list are:");
for(psTemp=gpsHead;psTemp;psTemp=psTemp->psNext){
printf("%d->",psTemp->iData);
}
getch();
}

/******************************************************************************
* Function : fnInsertBeginning
* Description : Inserts a node at the beginning of the linked list
* Input Parameters:Takes a node pointer
* Returns:None
******************************************************************************/

void fnInsertBeginning(node *psNew)


{
psNew->psNext=gpsHead;
gpsHead=psNew;
}

/******************************************************************************
* Function : fnInsertEnd
* Description : Inserts a node at the end of the linked list
* Input Parameters:Takes two node pointers
* Returns:None
******************************************************************************/

void fnInsertEnd(node *psPrev,node *psNew)


{
psPrev->psNext=psNew;
}

/******************************************************************************
* End of linkedlist.c
******************************************************************************/

You might also like