Thursday, September 28, 2006

Linear SINGLE LINK LIST C Program Example

I am writing this single link list c program after very long time !!! One of my colleague asked me some of the sample program for link list. I though I can share this sample ...

/* ======================== sll.h =====================*/

/*
** Description : Linear SINGLE LINK LIST example for learning.
**
** Author Name : Udhaya Kumar.V
**
** Created Date: 28-Sep-2006
**
** File Name : sll.h
**
** Copyrights : Any one can use for learning purpose.
*/

#ifndef _H_SLL
#define _H_SLL

typedef struct web
{
char url[ 256 ];
char ip[ 16 ];
struct web *Next;
}WEB;

WEB *Start, *Last, *Cur, *Prev;

int add_webpage( WEB *Rec ); /* To Add a node in Link List */
int lookup_webpage( char *url ); /* To Looup webpage record from Link List */
void display_webpage( void ); /* Display all webpage link list records */
int delete_webpage( char *url ); /* To delete webpage node from link list based on url */

#endif

/* ======================== sll.c ===================*/

/*
** Description : Linear SINGLE LINK LIST example for learning.
**
** Author Name : Udhaya Kumar.V
**
** Created Date: 28-Sep-2006
**
** File Name : sll.c
**
** Copyrights : Any one can use for learning purpose.
*/

#include
#include "sll.h"

/*
** Description: To add new node in WEB Link List.
**
** Return Value: 0 for Success, -1 for Failure
*/

int add_webpage( WEB *Rec )
{
/* allocate memory for current node */
if( ( Cur = (WEB *) malloc( sizeof( WEB ) ) ) == NULL )
return( -1 );

/* copy the structure to current node */
memcpy( Cur, Rec, sizeof( WEB ) );

/* adding first node */
if ( Start == NULL )
Start = Cur;
else
Last->Next = Cur;

Last = Cur;
Cur->Next = NULL;

return( 0 );
}

/*
** Description: To Looup webpage record from Link List.
**
** Return Value: 0 for Record found, -1 for Not found
*/

int lookup_webpage( char *url )
{
/* Loop will travel from first node to last node in Link List */
for( Cur = Start; Cur; Cur=Cur->Next )
{
if( !strcmp( Cur->url, url ) ) /* If record found */
return( 0 );
}

return( -1 ); /* If record not found */
}

/*
** Description: Display all webpage records from link list.
**
*/

void display_webpage( void )
{
for( Cur = Start; Cur; Cur=Cur->Next )
printf( "URL: %20s IP: %16s\n", Cur->url, Cur->ip );
}

/*
** Description: To delete webpage node from link list based on url.
**
** Return Value: 0 for Record deleted successfully, -1 for Not found
*/

int delete_webpage( char *url )
{
WEB *Temp;

/* Loop will travel from first node to last node in Link List */
for( Cur = Start; Cur; Prev = Cur, Cur = Cur->Next )
{
/* find the node */
if( !strcmp( Cur->url, url ) )
{
Temp = Cur;
if( Cur == Start ) /* If first node */
{
Start = Temp->Next;
}
else if( Cur->Next == NULL ) /* If last node */
{
Prev->Next = NULL;
Last = Prev;
}
else /* If middle node */
{
Prev->Next = Cur->Next;
}
free( Temp );
return( 0 );
}
}
return( -1 );
}

main( void )
{
WEB web_site;
int input;

while( 1 )
{
printf( "\n1.Add Webpage 2.Display Webpages 3.Delete Webpage 4.exit - your choice: " );
scanf( "%d", &input );

switch( input )
{
case 1: /* 1.Add Webpage */
/* Get the url and ip value from user */
printf( "Enter URL and IP with space:" );
scanf( "%s%s", web_site.url, web_site.ip );

if( lookup_webpage( web_site.url ) != 0 )
{
if( add_webpage( &web_site ) == 0 )
printf( "web_site record added successfully.\n" );
}
else
printf( "Given web site already exist in the Link List.\n" );

break;

case 2: /* 2.Display Webpages */
display_webpage();
break;
case 3: /* 3.Delete Webpage */
printf( "\nPlease enter URL to delete:" );
scanf( "%s", web_site.url );

if( delete_webpage( web_site.url ) == 0 )
printf( "web_site record deleted from link list.\n" );
else
printf( "given url not found from Link List.\n" );

break;

case 4:
printf( "Bye .. I don.t know this program useful for u or not ...\n" );
exit( 0 );

default:
printf( "Use proper option(1,2,3,4)\n" );
}
}
}

/* ======================== How To Compile ===============*/

$ cc sll.c -o webpage

/* ======================== How To Execute =============*/

$ webpage

1.Add Webpage 2.Display Webpages 3.Delete Webpage 4.exit - your choice: 1
Enter URL and IP with space:udhaya.com 10.12.12.12
web_site record added successfully.

1.Add Webpage 2.Display Webpages 3.Delete Webpage 4.exit - your choice: 1
Enter URL and IP with space:google.com 10.11.11.21
web_site record added successfully.

1.Add Webpage 2.Display Webpages 3.Delete Webpage 4.exit - your choice: 1
Enter URL and IP with space:udhaya.com 12.12.12.12
Given web site already exist in the Link List.

1.Add Webpage 2.Display Webpages 3.Delete Webpage 4.exit - your choice: 2
URL: udhaya.com IP: 10.12.12.12
URL: google.com IP: 10.11.11.21

1.Add Webpage 2.Display Webpages 3.Delete Webpage 4.exit - your choice: 3

Please enter URL to delete:google.com
web_site record deleted from link list.

1.Add Webpage 2.Display Webpages 3.Delete Webpage 4.exit - your choice: 2
URL: udhaya.com IP: 10.12.12.12

1.Add Webpage 2.Display Webpages 3.Delete Webpage 4.exit - your choice:

test

test