CodeProject
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct STR_DATA
{
char *name;
struct STR_DATA *next;
}NODE;
int count=0;
void add_names(NODE **head);
void sort_names(NODE *head);
void print_names(NODE *head);
char printmenu(NODE *head);
int main()
{
NODE *head=NULL;
char ch='y';
int flag=1;
while(flag)
{
ch = printmenu(head);
switch(ch)
{
case '0':
printf("\n\nEXITING...");
flag = 0;
break;
case '1':
while ((ch = getchar()) != '\n' && ch != EOF);
add_names(&head);
break;
case '2':
while ((ch = getchar()) != '\n' && ch != EOF);
sort_names(head);
break;
default:
printf("\n\nENTER VALID CHOICE...");
break;
}
}
printf("\n\n");
system("wait");
printf("\n");
return 0;
}
char printmenu(NODE *head)
{
char ch;
printf("\nCurrent list: ");
print_names(head);
printf("\nWHAT TO DO: ");
printf("\n[1] ADD a name\n[2] SORT names");
printf("\n\nEnter choice: ");
scanf("%c",&ch);
return ch;
}
void add_names(NODE **head)
{
NODE *new_node;
int num_char=0;
char *str1=NULL,ch='x';
new_node =(NODE*)malloc(1*sizeof(NODE));
count++;
new_node->next=*head;
*head=new_node;
printf("\nEnter name : ");
while(ch!='\n' && ch != EOF)
{
ch=getchar();
if(ch=='\n')
break;
num_char++;
str1 = (char*)realloc( str1,(num_char+1)*sizeof(char));
if(str1 == NULL)
{
printf("\nERROR: reallocating memory...\n\n");
exit(1);
}
str1[num_char-1]=ch;
str1[num_char]='\0';
}
new_node->name = (char*)malloc((num_char+1)*sizeof(char));
strcpy(new_node->name,str1);
free(str1);
}
void sort_names(NODE *head)
{
int i,j;
NODE *curr,*prev;
char *temp1,*temp2;
for(i=0;i<count-1;i++)
{
prev=head;
curr=head->next;
for(j=0;j<count-1-i;j++)
{
if(strcmp(prev->name,curr->name) > 0)
{
temp1 = (char*)malloc(strlen(prev->name));
temp2 = (char*)malloc(strlen(curr->name));
strcpy(temp1,prev->name);
strcpy(temp2,curr->name);
curr->name = (char*)realloc(curr->name,strlen(temp1));
prev->name = (char*)realloc(prev->name,strlen(temp2));
strcpy(prev->name,temp2);
strcpy(curr->name,temp1);
free(temp1);
free(temp2);
}
prev=curr;
curr=curr->next;
}
}
}
void print_names(NODE *head)
{
if(!head)
{
printf("EMPTY");
return;
}
while(head)
{
printf("%s|",head->name);
head=head->next;
}
}
#include<stdlib.h>
#include<string.h>
typedef struct STR_DATA
{
char *name;
struct STR_DATA *next;
}NODE;
int count=0;
void add_names(NODE **head);
void sort_names(NODE *head);
void print_names(NODE *head);
char printmenu(NODE *head);
int main()
{
NODE *head=NULL;
char ch='y';
int flag=1;
while(flag)
{
ch = printmenu(head);
switch(ch)
{
case '0':
printf("\n\nEXITING...");
flag = 0;
break;
case '1':
while ((ch = getchar()) != '\n' && ch != EOF);
add_names(&head);
break;
case '2':
while ((ch = getchar()) != '\n' && ch != EOF);
sort_names(head);
break;
default:
printf("\n\nENTER VALID CHOICE...");
break;
}
}
printf("\n\n");
system("wait");
printf("\n");
return 0;
}
char printmenu(NODE *head)
{
char ch;
printf("\nCurrent list: ");
print_names(head);
printf("\nWHAT TO DO: ");
printf("\n[1] ADD a name\n[2] SORT names");
printf("\n\nEnter choice: ");
scanf("%c",&ch);
return ch;
}
void add_names(NODE **head)
{
NODE *new_node;
int num_char=0;
char *str1=NULL,ch='x';
new_node =(NODE*)malloc(1*sizeof(NODE));
count++;
new_node->next=*head;
*head=new_node;
printf("\nEnter name : ");
while(ch!='\n' && ch != EOF)
{
ch=getchar();
if(ch=='\n')
break;
num_char++;
str1 = (char*)realloc( str1,(num_char+1)*sizeof(char));
if(str1 == NULL)
{
printf("\nERROR: reallocating memory...\n\n");
exit(1);
}
str1[num_char-1]=ch;
str1[num_char]='\0';
}
new_node->name = (char*)malloc((num_char+1)*sizeof(char));
strcpy(new_node->name,str1);
free(str1);
}
void sort_names(NODE *head)
{
int i,j;
NODE *curr,*prev;
char *temp1,*temp2;
for(i=0;i<count-1;i++)
{
prev=head;
curr=head->next;
for(j=0;j<count-1-i;j++)
{
if(strcmp(prev->name,curr->name) > 0)
{
temp1 = (char*)malloc(strlen(prev->name));
temp2 = (char*)malloc(strlen(curr->name));
strcpy(temp1,prev->name);
strcpy(temp2,curr->name);
curr->name = (char*)realloc(curr->name,strlen(temp1));
prev->name = (char*)realloc(prev->name,strlen(temp2));
strcpy(prev->name,temp2);
strcpy(curr->name,temp1);
free(temp1);
free(temp2);
}
prev=curr;
curr=curr->next;
}
}
}
void print_names(NODE *head)
{
if(!head)
{
printf("EMPTY");
return;
}
while(head)
{
printf("%s|",head->name);
head=head->next;
}
}
No comments:
Post a Comment