I must create a program which sorts linear list. The list must be implemented as a pointer list, which holds the strings. Sorting must be ascending (in alphabetic order).
Code:
#include<stdio.h>
struct list{
char string[100]; //the value part of the list
struct list *p_next; //a pointer to the next element of the list
};
int main() {
struct list *p_start; //a pointer which points at the start of the list
p_start=add(p_start,"SomeString1");
p_start=add(p_start,"Blahhhh");
p_start=add(p_start,"Life sucks!");
p_start=add(p_start,"Programming can be hard!");
p_start=add(p_start,"Nothing to say here");
p_start=add(p_start,"SomeStsd");
p_start=add(p_start,"sdsdsdsd");
return 0;
}
void print_list(struct list *p_start){ //a method to printout the list
while(p_start!=NULL){
printf("%s\n",p_start->string);
p_start=p_start->p_next;
}
}
struct list *add(struct list *p_start,char added_value[])
{
int *q;
q=(struct list*)malloc(sizeof(struct list));//must create a pointer on that new element of the list
q->value=added_value;
q->p_next=p_start;
p_start=q;
return(q);
}
Now in the above code I tried to just make some list of strings without sorting them, and each time I add a string I add it at the beginning. I get a few errors. Any help would be great. And suggestions how to sort the list too.