Professional Documents
Culture Documents
* 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 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
******************************************************************************/
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();
/******************************************************************************
* 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
******************************************************************************/
/******************************************************************************
* Function : fnInsertEnd
* Description : Inserts a node at the end of the linked list
* Input Parameters:Takes two node pointers
* Returns:None
******************************************************************************/
/******************************************************************************
* End of linkedlist.c
******************************************************************************/