All pastes #494504 Raw Edit

Someone

public c v1 · immutable
·published 2007-05-18 08:56 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <assert.h>// Node of a listtypedef struct NodeT {  char data;  struct NodeT * next;} Node;// List with a pointer to the first (head) and the last (foot)// element of the listtypedef struct ListT {  Node * head;  Node * foot;} List;// create a new list and return the pointer to the listList * makeNewList ();// insert a new item at the head of the listvoid insertAtHead (List *list, char newItem);// free list: free every node and the list structure itselfvoid freeList (List *list); // pretty print listvoid printList (List *list);           int lengthOfList (List *list);int main (int argc, char ** argv) {   int i=0;   int k=0;   int j=0;   int h=0;   char d;   char c;   int userInput[20];   int replaceInput[20];   int ruleInput[20];   int Output[20];    int f=0;/////////////////////make this a function start  -- this is the input function (grammar.txt)   while((c = getchar()) != '\n'){//      List *list = makeNewList();      userInput[i]=c;      i++;      f++;//      lengthOfList (list);//      freeList (list);   }     h=i;   //number of characters that go in   List *inputList = makeNewList();   while (k<i){      while (i>j){         insertAtHead (inputList, userInput[i-1]);         i--;      }      k++;   }   printList (inputList);///////////////////////make this a function end         ///////////////////////make this a function start --- This is the output function (prints out after rules)           //   char f;///////////////////////make this a function end                                                           //////////////////////Make this a function start -- this is the replacing function   i=0;   while ((d = getchar()) != '\n'){      replaceInput[i]=d;      i++;      }   k=0;   j=0;   List *replaceList = makeNewList();   while (k<i){      while (i>j){         insertAtHead (replaceList, replaceInput[i-1]);         i--;      }      k++;   }           printList (replaceList);//////////////////////Make this a function start  -- this is the rule function (what rules you want)   i=0;   char e;   while ((e =getchar()) != '\n'){      ruleInput[i]=e;      i++;   }   k=0;   j=0;   List *ruleList = makeNewList();   while (k<i){      while (i>j){         insertAtHead (ruleList, ruleInput[i-1]);         i--;      }      k++;   }      printList (ruleList);///////////////////////make this a function end    List *newList = makeNewList();   Node *currentNode;   Node *replaceNode=NULL;   Node *ruleNode;   Node *newNode;   assert(inputList);   assert(replaceList);   assert(ruleList);   currentNode = inputList->head;      //currentnode is the input node   replaceNode = replaceList->head;   ruleNode = ruleList->head;   i=h;   while (currentNode){        printf("currentNode is %c and replaceNode is %c\n", currentNode->data,replaceNode->data);      if (currentNode->data != replaceNode->data){        printf("Then newNode = currentNode\n");         newNode = currentNode;              insertAtHead (newList, Output[i-1]);          i--;      } else {         printf("Else newNode = ruleNode\n");        newNode = ruleNode;        insertAtHead (newList, Output[i-1]);        replaceNode = replaceNode->next;        newNode=newNode->next;        i--;      }        currentNode = currentNode->next;      printList(newList);   }  /*      j=0;   k=0;   while (j<h){     printf("UserInput is: %c and replaceInput is: %c and ruleInput is %c\n", userInput[j], replaceInput[k], ruleInput[k]);      if (userInput[j]!= replaceInput[k]){         Output[k]=userInput[j];         printf("Then Output = userInput\n");      } else {       printf("Else Output = Rule.\n");      Output[k]=ruleInput[k];               }      j++;      k++;   }*/ //////////////////////Make this a function end //////////////////////Make this a function start -- this is the output print function/*   i=0;   while (i<k){      printf("%c",Output[k]);      i++;   }   *///////////////////////Make this a function end       return (0);}List * makeNewList () {  // allocate space for the List struct  List * l = (List*) malloc (sizeof(List));  // check if assert was succesfull  assert (l);   // list is empty, so head and foot don't point to a node yet  l->head = NULL;  l->foot = NULL;  // return the list pointer  return (l);}void insertAtHead (List *list, char newItem) {  // allocate space for the new node struct  Node * node = (Node *) malloc (sizeof(Node));  // check if assert was succesfull  assert (node);  // initialise data field of the node  node->data = newItem;   // make sure the list pointer is not NULL  assert (list);  // let node point to first node in list  node->next = list->head;   // the new head of the list is the new node  list->head = node;  // if list was empty, foot is still NULL, and   // head and foot should point to the same node  // since the list has only one element now  if (list->foot == NULL) {    list->foot = node;   }}void freeList (List *list) {   Node *copyNode;   Node *currentNode;   assert(list);   currentNode = list->head;   while (currentNode) {      currentNode = currentNode->next;      copyNode = currentNode;      free (currentNode);      currentNode = copyNode;   }}void printList (List *list) {  Node *currentNode;  // make sure list is not NULL  assert (list);  // keep track of the current node we're printing  currentNode = list->head;  printf ("{");  while (currentNode) {    // print data of current node    printf ("%c", currentNode->data);    // move currentNode to the next node    currentNode = currentNode->next;    // printf ',' only if this was not the last node    if (currentNode) {      printf (", ");    }  }  printf ("}\n");  }int lengthOfList (List *list) {   Node *currentNode;   assert(list);   currentNode = list->head;   int counter=0;   while (currentNode) {      counter++;      currentNode = currentNode->next;   }   printf ("The list is of length: %d\n",counter);return (0);}