create a function that will alphabetize a linked list of wor
create a function that will alphabetize a linked list of words (written in C).
Solution
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#define SIZE 20
struct node {
char data[20];
struct node *next;
};
typedef struct node LISTNODE;
typedef LISTNODE *LISTNODEPTR;
/* Function prototype */
void insert ( LISTNODEPTR *, char[] );
void display ( LISTNODEPTR );
main()
{
char ch,str,*strptr;
LISTNODEPTR first = NULL;
do
{
printf(\"Enter string\ \");
scanf(\"%s\",&str);
strptr=&str;
insert ( &first, strptr );
printf(\"Do want to insert another string[Press Y/N]\ \");
scanf(\"%c\",&ch);
}while(ch==\'Y\'||ch==\'y\');
display(first);
return 0;
}
/* Insert a new value into the list in sorted order */
void insert ( LISTNODEPTR *head, char value[])
{
LISTNODEPTR temp, prev, currentPtr;
temp = malloc(sizeof(LISTNODE));
if ( temp != NULL )
{
strcpy(temp->data, value);
temp->next = NULL;
prev= NULL;
currentPtr = *head;
while ( currentPtr != NULL && value > currentPtr->data )
{
prev = currentPtr;
currentPtr = currentPtr->next;
}
if ( prev == NULL )
{
temp->next = *head;
*head = temp;
}
else
{
prev->next = temp;
temp->next = currentPtr;
}
}
else
printf(\"%c not inserted.\ \", value);
}
/* Print the list */
void display( LISTNODEPTR currentPtr )
{
if ( currentPtr == NULL )
printf(\"List is empty.\ \ \");
else
{
printf(\"The list is:\ \");
while ( currentPtr != NULL )
{
printf(\"%s --> \", currentPtr->data);
currentPtr = currentPtr->next;
}
printf(\"NULL\ \ \");
}
}

