Skip to content

Instantly share code, notes, and snippets.

@murikadan
Created August 30, 2012 06:35
Show Gist options
  • Save murikadan/3523346 to your computer and use it in GitHub Desktop.
Save murikadan/3523346 to your computer and use it in GitHub Desktop.
Linked List Sort
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char *val;
struct node *next;
};
struct node* makenode(char *);
void insert(char *,struct node**,struct node**);
void stringgen(char *,struct node*[],struct node*[]);
void freequeue(struct node*);
void output(struct node*[],struct node*[],long int*);
void enqueue(struct node**,struct node**,struct node**);
void partition(struct node*,struct node*,struct node*[]);
void quicksort(struct node*,struct node*);
void swap(struct node*,struct node*);
void traverse(struct node*);
int main()
{
int i=0,n,q;
long int *k=NULL;
char *w[50];
struct node *front[26],*rear[26];
for(i=0;i<26;i++)
front[i]=rear[i]=NULL;
FILE *fp;
fp=fopen("input","r");
fscanf(fp,"%d",&n);
for(i=0;i<n;i++)
{
w[i]=malloc(sizeof(char)*2001);
fscanf(fp,"%s",w[i]);
}
fscanf(fp,"%d",&q);
k=malloc(sizeof(long int)*q);
for(i=0;i<q;i++)
fscanf(fp,"%ld",&k[i]);
for(i=0;i<n;i++)
stringgen(w[i],front,rear);
output(front,rear,k);
free(k);
for(i=0;i<n;i++)
free(w[i]);
for(i=0;i<26;i++)
freequeue(front[i]);
return 0;
}
void stringgen(char *c,struct node* front[],struct node* rear[])
{
int i=0,j=0,x=0;
char temp[2001];
strcpy(temp,c);
while(temp[0]!='\0')
{
for(i=0;temp[i]!='\0';i++)
{
char *buf=malloc(sizeof(char)*(i+2));
for(j=0;j<=i;j++)
{
buf[j]=temp[j];
}
buf[j]='\0';
int h=(int)(buf[0]-'a');
insert(buf,&(front[h]),&(rear[h]));
}
x++;
strcpy(temp,c+x);
}
}
void enqueue(struct node** front,struct node **rear,struct node** newnode)
{
(*newnode)->next=NULL;
if(*front==NULL)
*front=*rear=*newnode;
else
{
(*rear)->next=*newnode;
(*rear)=*newnode;
}
}
void insert(char *c,struct node** front,struct node** rear)
{
int flag=1;
struct node* temp=*front;
while(temp!=NULL)
{
if(!strcmp(temp->val,c))
{
flag=0;
break;
}
else
temp=temp->next;
}
if(flag!=0)
{
struct node* newnode=makenode(c);
enqueue(front,rear,&newnode);
}
}
void freequeue(struct node* front)
{
while(front!=NULL)
{
struct node* save=front->next;
if(front->val!=NULL)
free(front->val);
free(front);
front=save;
}
}
void partition(struct node* node_p,struct node* node_r,struct node* node_q[])
{
struct node* node_i=NULL;
if(node_p!=NULL&&node_r!=NULL)
{
struct node *node_j=node_p;
while(node_j->next!=node_r)
{
if(strcmp(node_j->val,node_r->val)<=0)
node_i=(node_i!=NULL)?node_i->next:node_p;
swap(node_i,node_p);
}
swap(node_i->next,node_r);
}
node_q[0]=node_i;
node_q[1]=node_i->next;
}
void swap(struct node* node_a,struct node* node_b)
{
char *temp=node_b->val;
node_b->val=node_a->val;
node_a->val=temp;
}
struct node* makenode(char *c)
{
struct node* newnode=malloc(sizeof(struct node));
newnode->next=NULL;
newnode->val=c;
return newnode;
}
void quicksort(struct node* node_p,struct node* node_r)
{
if(node_p->next!=node_r||node_p!=node_r)
{
struct node* node_q[2];
partition(node_p,node_r,node_q);
quicksort(node_p,node_q[0]);
quicksort(node_q[1]->next,node_r);
}
}
void traverse(struct node* head)
{
struct node* temp=head;
while(temp!=NULL)
{
printf("\n%s",temp->val);
temp=temp->next;
}
}
void output(struct node* front[],struct node* rear[],long int* k)
{
int i=0;
for(i=0;i<26;i++)
traverse(front[i]);
}
10
a
b
c
d
e
f
g
h
i
j
3
3
8
23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment