Advertisement
Guest User

Practice Linked Lists

a guest
May 25th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1.  
  2.  
  3. //Written by Michael Simarta
  4. //With the help of Bernice Chen
  5. //List2.c file that implements all functions declared in List2.h
  6. //implement the removeUpperCase() functions here
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <assert.h>
  11. #include "list2.h"
  12.  
  13. typedef struct _node {
  14.     char value;
  15.     Node next;
  16. } node;
  17.  
  18. typedef struct _list {
  19.     Node head;
  20. } list;
  21.  
  22. //when the removeUpperCase() function is complete,
  23. //compile with testList2.c, type
  24. //"gcc -Wall -Werror -O -o test testList2.c List2.c"
  25. //to run program, type
  26. //"./test"
  27.  
  28. //TODO
  29. //Read List2.h to help you look at the hints
  30. //remove values which are uppercase
  31. //
  32. //given a list X
  33. //(Empty List)
  34. //then removeUpperCase(list);
  35. //the list is still X
  36. //
  37. //given a list a->X
  38. //then removeUpperCase(list);
  39. //the list is a->X
  40. //
  41. //given a list A->X
  42. //then removeUpperCase(list);
  43. //the list is X
  44. //
  45. //given a list A->a->X
  46. //then removeUpperCase(list);
  47. //the list is a->X
  48. //
  49. //given a list C->a->A->D->X
  50. //then removeUpperCase(list);
  51. //the list is a->X
  52. //
  53. //given a list A->a->B->b->C->c->d->D->E->e->X
  54. //then removeUpperCase(list);
  55. //the list is a->b->c->d->e->X
  56.  
  57. void removeUpperCase(List l) {
  58.    
  59.     Node current = l->head;
  60.    
  61.     // when l is empty
  62.     if (current->next == NULL) {
  63.         current = NULL;
  64.    
  65.     } else {
  66.         while (current->next != NULL) {
  67.             if((current->value <= 'a')) {
  68.                 Node savePtr = current->next;
  69.                 free(current);
  70.                 current = savePtr;
  71.             }
  72.        
  73.         }
  74.     }
  75.    
  76. }
  77.  
  78. //returns a new list of length 0
  79. List newList() {
  80.     List l = malloc(sizeof(list));
  81.     assert ( l!=NULL );
  82.     l->head = NULL;
  83.     return l;
  84. }
  85.  
  86. //frees everything malloced for the list
  87. void destroy(List l) {
  88.     Node curNode = l->head;
  89.     Node prevNode = NULL;
  90.    
  91.     while (curNode != NULL) {
  92.         prevNode = curNode;
  93.         curNode = prevNode->next;
  94.         free(prevNode);
  95.     }
  96.    
  97.     free(l);
  98. }
  99.  
  100. //appends a node of value to the end of the list
  101. void append(List l, char value) {
  102.     Node newNode = malloc(sizeof(node));
  103.     newNode->value = value;
  104.     newNode->next = NULL;
  105.     Node curNode = l->head;
  106.     if ( curNode==NULL ) {
  107.         l->head = newNode;
  108.     } else {
  109.         while ( curNode->next!=NULL ) {
  110.             curNode = curNode->next;
  111.         }
  112.         curNode->next = newNode;
  113.     }
  114. }
  115.  
  116. //returns the int value of the node at index
  117. //assumes input index is within range of the list's length
  118. char getValue(List l, int index) {
  119.    
  120.     Node curNode = l->head;
  121.     assert (curNode!=NULL);
  122.     int counter = 0;
  123.     while (counter<index) {
  124.         curNode = curNode->next;
  125.         counter++;
  126.     }
  127.     return curNode->value;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement