Sunday, May 22, 2011

Write a program for following operations in circular linked list of strings:- a.Creation of linked list b.Insertion at beginning c.Insertion at last d.Insertion at specified position e.Exit

#include /*Header file */
#include
struct node /* structure having a info part and a link part */
{
int info ;
struct node *link ;
} ;
int insertbeg ( struct node **, int ) ; /*insertion*/
int opposite ( struct node ** ) ; /* opposite*/
int display ( struct node * ) ;
int count ( struct node * ) ;

int main( )
{
struct node *opt ;
opt = NULL ;

insertbeg ( &opt, 8 ) ;
insertbeg ( &opt, 24 ) ;
insertbeg ( &opt, 10 ) ;
insertbeg ( &opt, 5 ) ;
insertbeg ( &opt, 3 ) ;
insertbeg ( &opt, 2 ) ;

display ( opt ) ;
printf ( "\nNo. of elements in linked list = %d", count ( opt ) ) ;
opposite ( &opt ) ;
display ( opt ) ;
printf ( "\nNo. of elements in linked list = %d", count ( opt ) ) ;
}

int insertbeg ( struct node **vtx, int var )
{
struct node *item ;
item = malloc ( sizeof ( struct node ) ) ; /*malloc for memory allocation*/
item -> info = var ;
item -> link = *vtx ;
*vtx = item ;
}

int opposite ( struct node **rtx )
{
struct node *vtx, *anode, *bnode ;
vtx = *rtx ;
anode = NULL ;
while ( vtx != NULL )
{
bnode = anode ;
anode = vtx ;
vtx = vtx -> link ;
anode -> link = bnode ;
}

*rtx = anode ;
}

int display ( struct node *vtx )
{
printf ( "\n" ) ;
while ( vtx != NULL)
{
printf ( "%d ", vtx -> info ) ;
vtx = vtx -> link ;
}
}

int count ( struct node * vtx)
{
int cont = 0 ;
while ( vtx != NULL )
{
vtx = vtx -> link ;
cont++ ;
}

return cont ;
}


OUTPUT

2 3 5 10 24 8
No. of elements in linked list = 6
8 24 10 5 3 2
No. of elements in linked list = 6

No comments:

Post a Comment