implementation of singly linkedlist.

#include<stdio.h>
#include<alloc.h>
#include "linklist.h"

void initialise(linkedlist *lp)
{
lp -> base = NULL;
lp -> count = 0;
}

void uninitialise(linkedlist *lp)
{
while(lp -> count > 0)
del(lp, lp -> count);
}

int append(linkedlist *lp, char *data)
{
node *p;
if(lp -> count == 0)
{
/*first node being added*/
lp -> base = (node *) malloc(sizeof(node));
if(lp -> base == NULL)
return FAILURE;
p = lp -> base;
}
else
{
/*subsequent node being added*/
p = lp -> base;
/*search for last node*/
while(p -> next != NULL)
p = p -> next;

p -> next = (node *) malloc(sizeof(node));
if(p -> next == NULL)
return FAILURE;
p = p -> next;
}
/*copy data to the new node*/
strcpy(p -> name, data);
p -> next = NULL;
lp -> count++;
return SUCCESS;
}

node *locatenode( linkedlist *lp, int pos )
{
/* returns the address of the node at given pos */
node *p;
int i;
if( pos < 1 || pos > lp -> count)
return NULL;

/*locate posth node*/
p = lp -> base;
i = 1;
while(i < pos)
{
p = p -> next;
i++;
}
return p;
}

int insert(linkedlist *lp,int pos,char *data)
{
node *newnode,*p;
int i;
if(pos < 1 || pos > lp -> count)
return FAILURE;

/*locate the (pos-1)th node in the linked list*/
p = locatenode( lp, pos - 1 );

/*create new node*/
newnode = (node *) malloc(sizeof(node));
if(newnode == NULL)
{
printf("\n memory allocation failed");
return FAILURE;
}
newnode -> next = p -> next;
p -> next = newnode;
strcpy(newnode -> name, data);
lp -> count++;
return SUCCESS;
}
int del(linkedlist *lp,int pos)
{
int i;
node *p,*removenode;

if(pos < 1 || pos > lp -> count)
return FAILURE;

/*locate the (pos-1)th node*/
p = locatenode( lp, pos-1 );

removenode = p -> next;
p -> next = removenode -> next;

free(removenode);
lp -> count--;
return SUCCESS;
}

int getvalue(linkedlist *lp,int pos, char *data)
{
node *p;
if(pos < 1 || pos > lp -> count)
return FAILURE;

/*locate posth node*/
p = locatenode( lp, pos );
if( p == NULL )
return FAILURE;

strcpy(data, p -> name);
return SUCCESS;
}

int setvalue(linkedlist *lp, int pos, char *data)
{
node *p;

/*locate posth node*/
p = locatenode( lp, pos );
if( p == NULL )
return FAILURE;

strcpy(p -> name, data);
return SUCCESS;
}

void show(linkedlist *lp)
{
node *p;
int i;
if(lp -> count==0)
{
printf("\nList is empty");
return;
}
printf("\n displaying nodes");
p = lp -> base;
for(i = 1 ; i <= lp -> count ; i++)
{
printf("\n %d [%u] %s [-> %u]",i, p, p -> name, p -> next);
p = p -> next;
}
}

int getcount( linkedlist *lp )
{
return lp -> count;
}

int find( linkedlist *lp, char *data )
{
return SUCCESS;
}

void sort( linkedlist *lp, int order )
{
return;
}

int main(void)
{
int choice, pos, order;
char value[80];
linkedlist l;

initialise(&l);
do
{
printf("\n ** SINGLE LINKED LIST **");
printf("\n1. Add\n2. Insert\n3. Remove\n4. Setvalue\n5. Getvalue");
printf("\n6. Show\n7. Find\n8. Sort\n9. Exit\nOption ? [1 - 9] :");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n enter value :");
fflush(stdin);
gets(value);
if( append(&l, value) == FAILURE )
printf( "\nList is FULL" );
else
printf( "\nNode inserted" );
break;
case 2:
printf("\nEnter position : " );
scanf( "%d", &pos );
printf("\nEnter value :");
fflush(stdin);
gets(value);
if( insert(&l, pos, value) == FAILURE )
printf( "\nInvalid Position" );
else
printf( "\nNode inserted" );
break;
case 3:
printf("\nEnter position : " );
scanf( "%d", &pos );
if( del( &l, pos ) == FAILURE )
printf( "\nInvalid Position" );
else
printf("\nNode removed");
break;
case 4:
printf("\nEnter position : " );
scanf( "%d", &pos );
printf("\nEnter value :");
fflush(stdin);
gets(value);
if( setvalue(&l, pos, value) == FAILURE )
printf( "\nCould not set value" );
break;
case 5:
printf("\nEnter position : " );
scanf( "%d", &pos );
if( getvalue(&l, pos, value) == FAILURE )
printf( "\nCould not get value" );
else
printf( "\nValue at %d = %s", pos, value );
break;
case 6:
show(&l);
break;
case 7:
printf( "\nTo be done as homework" );
/*
printf("\nEnter value :");
fflush(stdin);
value = getche();
pos = find( &l, value );
if( pos > 0 )
printf( "\nValue %c found at pos %d", value, pos );
else
printf( "\nValue %c not found", value );
*/
break;
case 8:
printf( "\nTo be done as homework" );
/*
printf("\nEnter Sort Order [ 1- ASC, 2- DESC] :");
scanf( "%d", &order );
sort( &l, order );
*/
break;
case 9:
uninitialise(&l);
break;
}
}while(choice != 9);

return 0;
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin