Expand|Select|Wrap|Line Numbers
- list.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "list.h"
- #define LISTSZ(a) (((a)-1)*sizeof(char*)+sizeof(struct list))
- struct list{
- int size;
- int maxsz;
- char *string[1];
- };
- struct list* startlist (int numstrings)
- {
- struct list *q;
- q=malloc(LISTSIZE(numstrings));
- if (q==NULL)
- {
- perror("malloc error\n");
- exit(-1);
- }
- q-> size=0;
- q-> maxsz= numstrings;
- return q;
- }
- struct list* insrtstr(char sizestring[], struct list *p){
- char *srcptr, *destptr;
- struct list *q;
- if(p->size == p->maxsz){
- p-> maxsz+= INCRSZ;
- q=realloc(p, LISTSIZE(p->maxsz));
- if(q==NULL)
- {
- perror("realloc error\n");
- exit (-1);
- }
- p=q;
- }
- p->string[p->size++]= (char*)strdup(sizestring);
- return p;
- }
- /*/void sortlist(struct list *p){
- // sizeone++;
- // sizetwo++; */
- //
Expand|Select|Wrap|Line Numbers
- list.h
- #ifndef _LIST_H_
- #define _LIST_H_
- #define INITSZ 7
- #define INCRSZ 10
- struct list;
- struct list* startlist(int);
- struct list* insrtstr(char [], struct list*);
- void sortlist(struct list *);
- struct list* rmvstr(int, struct list *);
- void printlist(struct list *);
- void freelist(struct list *);
- int strCompare (const void *, const void *);
- #endif
Expand|Select|Wrap|Line Numbers
- driver.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "list.h"
- #define MAX_STRING 1024
- #define REMOVE_POSITION 4
- int main(int argc, char *argv[]){
- FILE *in;
- char *str;
- struct list *anotherList;
- str=malloc (MAX_STRING);
- in=fopen(argv[1], "r");
- anotherList=startlist(INITSZ);
- while(fgets(str, MAX_STRING, in)!= NULL){
- anotherList=insrtstr(str, anotherList);
- }
- sortlist(anotherList);
- anotherList=rmvstr(REMOVE_POSITION, anotherList);
- printlist(anotherList);
- fclose(in);
- free(str);
- freelist(anotherList);
- return EXIT_SUCCESS;
- }