strList (C)

Same functionality as list.c (bigger example), but this one can also insert a node at any position of the list. This implementation handles strings.

strList.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct listnode *Listptr;

struct listnode {
  char* value;
  Listptr next;
};

int empty(Listptr);
int in(Listptr, char *);
int n_th(Listptr, int, char *, int);
void insert_at_start(Listptr *, char *);
void insert_at(Listptr *, int, char *);
void insert_at_end(Listptr *, char *);
int delete(Listptr *, char *);
void print(Listptr);
void free_list(Listptr *);

int main(void)
{ Listptr list = NULL;
  
  insert_at(&list, 0, "George");
  insert_at(&list, 1, "Samaras");
  insert_at(&list, 2, "Charalampos");
  insert_at(&list, 3, "Kostoula");
  insert_at(&list, 4, "Theodor");
  insert_at(&list, 5, "Nick");
  insert_at(&list, 0, "A");
  insert_at(&list, 7, "Z");
  insert_at(&list, 3, "M");
  
  printf("List is : "); print(list); 
                                                          /* Output */
         /* List is : A--> George--> Samaras--> M--> Charalampos--> */
                         /*Kostoula--> Theodor--> Nick--> Z--> NULL */

  free_list(&list);
  return 0;
}

int empty(Listptr list)                   /* Check if list is empty */
{ if (list == NULL)                          /* Is it really empty? */
    return 1;                                         /* Yes, it is */
  else
    return 0;                                       /* No, it isn't */
}

int in(Listptr list, char * str)  /* Check if str is member of list */
{ while (list != NULL)         /* Visit list elements up to the end */
                            /* Did we find what we are looking for? */
    if (strcmp(list->value, str) == 0)       
      return 1;                                      /* Yes, we did */
    else
      list = list->next;                  /* No, go to next element */
  return 0;                            /* Finally, v is not in list */
}

int n_th(Listptr list, int n, char *str, int sizeStr)
             /* Return n-th element of list, if it exists, into str */
{ while (list != NULL)    /* Maybe search up to the end of the list */
    if (n-- == 1) {              /* Did we reach the right element? */
      /* If str does not have enough memory, then don't copy string */
      if (strlen(list->value) >= sizeStr) {
          printf("Overflow warning! Not enough memory "
                  "in n_th function to save %s\n" ,list->value);
          return 0;
      }
      strcpy(str, list->value);                   /* Yes, return it */
      return 1;                                      /* We found it */
    }
    else
      list = list->next;                  /* No, go to next element */
  return 0;                             /* Sorry, list is too short */
}

void insert_at_start(Listptr *ptraddr, char* str)
                    /* Insert str as first element of list *ptraddr */
{ Listptr templist;
  templist = *ptraddr;                /* Save current start of list */
          /* In real application, check for NULL pointer of malloc! */
  *ptraddr = malloc(sizeof(struct listnode)); /* Space for new node */
     /* Space for string. Length of str + 1 for the null terminator */
  (*ptraddr)->value = malloc((strlen(str) + 1) * sizeof(char));
  strcpy((*ptraddr)->value, str);                      /* Copy str  */
  (*ptraddr)->next = templist;      /* Next element is former first */
}

void insert_at(Listptr *ptraddr, int pos, char *str)
                   /* Insert str as pos-th element of list *ptraddr */
{ Listptr templist;
  Listptr origin = *ptraddr;
  int i = 0;
  if(empty(*ptraddr) || pos == 0){/* If the list is empty or 0 pos, */
      Listptr templist;
      templist = *ptraddr;            /* Save current start of list */
          /* In real application, check for NULL pointer of malloc! */
                                              /* Space for new node */
      *ptraddr = malloc(sizeof(struct listnode)); 
     /* Space for string. Length of str + 1 for the null terminator */
      (*ptraddr)->value = malloc((strlen(str) + 1) * sizeof(char));
      strcpy((*ptraddr)->value, str);                  /* Copy str  */
      (*ptraddr)->next = templist;  /* Next element is former first */
      return;
  }
  while((*ptraddr)->next != NULL) { /*      While not the last node */
      if(i++ + 1 == pos) {  /* if we are we one step before the pos */
          
          templist = *ptraddr;     /* Save current position of list */
          /* In real application, check for NULL pointer of malloc! */
                                              /* Space for new node */
         *ptraddr = malloc(sizeof(struct listnode)); 
     /* Space for string. Length of str + 1 for the null terminator */
         (*ptraddr)->value = malloc((strlen(str) + 1) * sizeof(char));
         strcpy((*ptraddr)->value, str);               /* Copy str  */
                    /* Next element is next element of former first */
         (*ptraddr)->next = templist->next;   
                         /* Next element of former list is new node */
         templist->next = *ptraddr;
         *ptraddr = origin;
         return;
      }
      else
          *ptraddr = (*ptraddr)->next;        /* Go to next element */
  }                     
  templist = malloc(sizeof(struct listnode)); 
     /* Space for string. Length of str + 1 for the null terminator */
  templist->value = malloc((strlen(str) + 1) * sizeof(char));
  strcpy(templist->value, str);                        /* Copy str  */
                   /* We insert at end, so there is no next element */
  (*ptraddr)->next = templist;
  templist->next = NULL;
  *ptraddr = origin;
}

void insert_at_end(Listptr *ptraddr, char* str)
                     /* Insert str as last element of list *ptraddr */
{ while (*ptraddr != NULL)                     /* Go to end of list */
    ptraddr = &((*ptraddr)->next);/* Prepare what we need to change */
          /* In real application, check for NULL pointer of malloc! */
  *ptraddr = malloc(sizeof(struct listnode)); /* Space for new node */
     /* Space for string. Length of str + 1 for the null terminator */
  (*ptraddr)->value = malloc((strlen(str) + 1) * sizeof(char));
  strcpy((*ptraddr)->value, str);                      /* Copy str  */
  (*ptraddr)->next = NULL;              /* There is no next element */
}

int delete(Listptr *ptraddr, char* str)
             /* Delete element str from list *ptraddr, if it exists */
{ Listptr templist;
  while ((*ptraddr) != NULL)   /* Visit list elements up to the end */
                                     /* Did we find what to delete? */
    if (strcmp((*ptraddr)->value, str) == 0) {    
      templist = *ptraddr;         /* Yes, save address of its node */
      *ptraddr = (*ptraddr)->next;        /* Bypass deleted element */
      free((templist)->value);        /* Free memory for the string */
      free(templist);     /* Free memory for the corresponding node */
      return 1;                           /* We deleted the element */
    }
    else
      ptraddr = &((*ptraddr)->next);/* Prepare what we might change */
  return 0;        /* We didn't find the element we were looking for */
}

void print(Listptr list)                  /* Print elements of list */
{ while (list != NULL) {       /* Visit list elements up to the end */
    printf("%s--> ", list->value);         /* Print current element */
    list = list->next;                        /* Go to next element */
  }
  printf("NULL\n");                            /* Print end of list */
}

void free_list(Listptr *ptraddr)
{ Listptr templist = *ptraddr;
  while(*ptraddr != NULL) {
      *ptraddr = (*ptraddr)->next;
      free(templist->value);
      free(templist);
      templist = *ptraddr;
  }
  *ptraddr = NULL; 
}

This code is developed by me, G. Samaras. However, it is based on list.c.

Have questions about this code? Comments? Did you find a bug? Let me know!😀
Page created by G. (George) Samaras (DIT)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s