Dear Friends;
First, thanks ( Mr. Salem)
and others. Another, I became a member of this sites and my ID is yescha, now.
Here is my linked list and I am trying to change it circular. But I am not able to change. It prints very strange when I changed its last link to head.
So, please suggest me and send me if you have any solutions or such programms.
***********Program***********
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node *head = (struct node *) NULL;
struct node *end = (struct node *) NULL;
struct node * initnode( char *, int );
void printnode( struct node * );
void printlist( struct node * );
void add( struct node * );
void insertnode( struct node * );
struct node {
char name[20];
int id;
struct node *next;
};
struct node * initnode( char *name, int id )
{
struct node *ptr;
ptr = (struct node *) malloc( sizeof(struct node ) );
if( ptr == NULL )
return (struct node *) NULL;
else {
strcpy( ptr->name, name );
ptr->id = id;
return ptr;
}
}
void printnode( struct node *ptr )
{
printf("Name : %s\n", ptr->name );
printf("ID : %d\n", ptr->id );
}
void printlist( struct node *ptr )
{
while( ptr != NULL )
{
printnode( ptr );
ptr = ptr->next;
/* when I insert here if (ptr==head) break; it is not working */
/* printing same thing infininely */
/* Can you say why???? */
}
}
void add( struct node *new )
{
if( head == NULL )
head = new;
end->next = new;
new->next = NULL;
end = new;
}
void insertnode( struct node *new )
{
struct node *temp, *prev;
if( head == NULL ) {
head = new;
end = new;
head->next = NULL;
return;
}
temp = head;
while( strcmp( temp->name, new->name) < 0 ) {
temp = temp->next;
if( temp == NULL )
break;
}
if( temp == head ) {
new->next = head;
head = new;
}
else {
prev = head;
while( prev->next != temp ) {
prev = prev->next;
}
prev->next = new;
new->next = temp;
if( end == prev )
end = new;
}
}
main()
{
char name[20];
int id, ch = 1;
struct node *ptr;
clrscr();
while( ch != 0 ) {
printf("1 add a name \n");
printf("2 list all names \n");
printf("3 insert a name \n");
printf("0 quit\n");
scanf("%d", &ch );
switch( ch )
{
case 1: /* add a name to end of list */
printf("Enter in name -- ");
scanf("%s", name );
printf("Enter in id -- ");
scanf("%d", &id );
ptr = initnode( name, id );
add( ptr );
break;
case 2: /* list all nodes */
printlist( head );
break;
case 5: /* insert a name in list */
printf("Enter in name -- ");
scanf("%s", name );
printf("Enter in id -- ");
scanf("%d", &id );
ptr = initnode( name, id );
insertnode( ptr );
break;
}
}
}
** I changed * changed the add and insert to similarly but not working .......*/
Please, help me if any one has any idea regarding this
I am also trying...........
BYE!!!!!