Skip to content

Instantly share code, notes, and snippets.

@Ozerich
Created May 22, 2011 09:27
Show Gist options
  • Select an option

  • Save Ozerich/985300 to your computer and use it in GitHub Desktop.

Select an option

Save Ozerich/985300 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstdlib>
int strlen(char *text)
{
int i;
for(i = 0; text[i] != '\0'; i++);
return i;
}
char up(char ch)
{
if(ch >= 'a' && ch <= 'z')
return ch = 'A' + (ch - 'a');
return ch;
}
int strcmp(char *a, char *b)
{
for(int i = 0; i < strlen(a) && i < strlen(b); i++)
{
if(up(a[i]) < up(b[i]))
return -1;
else if(up(a[i]) > up(b[i]))
return 1;
}
if(strlen(a) < strlen(b))
return -1;
else if(strlen(a) > strlen(b))
return 1;
return 0;
}
struct Node
{
char *value;
Node *next, *prev;
Node(char *val)
{
value = new char[1000];
for(int i = 0; i < strlen(val); i++)
value[i] = val[i];
value[strlen(val)] = '\0';
next = prev = NULL;
}
};
Node *list;
void add(char *word)
{
if(list == NULL)
list = new Node(word);
else
{
Node *cur = list, *prev_cur = NULL, *elem = new Node(word);
while(cur != NULL && strcmp(cur->value, word) < 0)
{
prev_cur = cur;
cur = cur->next;
}
if(prev_cur == NULL)
{
elem->next = list;
list->prev = elem;
list = elem;
}
else
{
elem->prev = prev_cur;
elem->next = cur;
prev_cur->next = elem;
cur->prev = prev_cur;
}
}
}
int main()
{
char *in_filename = new char[1000], *out_filename = new char[1000];
printf("Input file: ");
scanf("%s", in_filename);
printf("Output file:");
scanf("%s", out_filename);
//in_filename = "input.txt";
//out_filename = "output.txt";
char *word = new char[1000];
FILE *f = fopen(in_filename, "r+");
while(fscanf(f, "%s", word) != -1)
add(word);
fclose(f);
f = fopen(out_filename, "w+");
Node *cur = list;
while(cur != NULL)
{
fprintf(f, "%s\n", cur->value);
cur = cur->next;
}
fclose(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment