Advertisement
Guest User

Untitled

a guest
May 6th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct list_node
  4. {
  5.     char*  dataPtr;
  6.     struct list_node*  next;
  7. }ListNode;
  8.  
  9. typedef struct list
  10. {
  11.     ListNode* head;
  12.     ListNode* tail;
  13. }List;
  14. typedef struct  student
  15. {
  16.     List first;
    int grade;
  17. } Student;
  18. /* declare about functions */
  19. void makeEmptyList(List* lst);
  20. void insertDataToEndList(List* lst,char ch);
  21. char* allocateNewData(char ch);
  22. ListNode* allocateNewCell(char* data);
  23. void addNewCellToBegining(List* lst, ListNode* cell);
  24. void addNewCellToEnd(List* lst, ListNode* cell);
  25. Student unScramble(List lst);
  26. void deallocateCell(List lst, ListNode* curr, ListNode* prev);  //dellete a cell from the list
  27. void delleteCellFromInner(List lst,ListNode* curr, ListNode* prev);
  28. void delleteCellFromEnd(List lst, ListNode* curr, ListNode* prev);
  29. void printStudent(Student* student);
  30.  
  31. int main(void)
  32. {
  33.     List lst;
  34.     Student student;
  35.     char ch;
  36.    
  37.     makeEmptyList(&lst);
  38.    
  39.     printf("Please enter the scrambled student:\n");
  40.    
  41.     ch = (char)getchar();
  42.     while(ch != '\n')
  43.     {
  44.         insertDataToEndList(&lst, ch);
  45.         ch = (char)getchar();
  46.     }
  47.    
  48.     student = unScramble(lst);
  49.    
  50.     printStudent(&student);
  51.    
  52. }
  53.  
  54.  
  55.  
  56. void printStudent(Student* student)
  57. {
  58.     ListNode* curr;
  59.     curr=student->first.head;
  60.     printf("The grade is : %d \n",student->grade);
  61.     printf("The name is : ");
  62.     while(curr)
  63.     {
  64.         printf("%c",curr->dataPtr[0]);
  65.         curr=curr->next;
  66.     }
  67. }
  68.  
  69.  
  70. Student unScramble(List lst)   //this function is unscramble the list
  71. {
  72.     Student s1;
  73.     ListNode* curr;   //holds the current cell
  74.     ListNode* prev;  //holds the prev cell
  75.     int num;
  76.    
  77.     num=0;
  78.     curr=lst.head;
  79.     prev=NULL;
  80.     while(curr!=NULL)   //while curr isnt NULL
  81.     {
  82.         if(curr->dataPtr[0]>='0' && curr->dataPtr[0]<='9')  //if the char is any number
  83.         {
  84.             num=num*10+((int)(curr->dataPtr[0]-'0'));
  85.            
  86.         }
  87.         else
  88.         {
  89.             prev=curr;
  90.             curr=curr->next;
  91.            
  92.         }
  93.     }
  94.     s1.grade=num;
  95.     s1.first=lst;
  96.    
  97.     return s1;
  98. }
  99.  
  100. void deallocateCell(List lst, ListNode* curr, ListNode* prev)  //dellete a cell from the list
  101. {
  102.     if(lst.head==lst.tail)  //the list has only one cell
  103.     {
  104.         free(curr->dataPtr);
  105.         free(curr);
  106.         curr=NULL;
  107.     }
  108.     else if(lst.tail==curr)
  109.     {
  110.         delleteCellFromEnd(lst,curr,prev);
  111.        
  112.     }
  113.     else
  114.     {
  115.         delleteCellFromInner(lst,curr,prev);
  116.        
  117.     }
  118.    
  119. }
  120.  
  121.  
  122. void delleteCellFromEnd(List lst, ListNode* curr, ListNode* prev) //dellete cell from the end of the list
  123. {
  124.     prev->next=NULL;
  125.     free(curr->dataPtr);
  126.     free(curr);
  127.     lst.tail=prev;
  128.     curr=NULL;
  129. }
  130.  
  131. void delleteCellFromInner(List lst,ListNode* curr, ListNode* prev)  //dellete cell from inner place
  132. {
  133.     ListNode* save=curr;
  134.     prev->next=curr->next;
  135.     curr=curr->next;
  136.     free(save->dataPtr);
  137.     free(save);
  138.    
  139. }
  140.  
  141.  
  142. void insertDataToEndList(List* lst,char ch)  //insert data to the list
  143. {
  144.     ListNode* cell;  //holds new cell
  145.     char* data;     //hold string
  146.    
  147.     data=allocateNewData(ch);
  148.    
  149.     cell=allocateNewCell(data);
  150.    
  151.     if (lst->head==NULL)  //if lst is empty
  152.         addNewCellToBegining(lst,cell);
  153.    
  154.     else
  155.         addNewCellToEnd(lst,cell);
  156. }
  157.  
  158.  
  159. void addNewCellToEnd(List* lst, ListNode* cell)  //add new cell to the end of the lst
  160. {
  161.     lst->tail->next=cell;
  162.     lst->tail=cell;
  163.    
  164. }
  165.  
  166.  
  167. void addNewCellToBegining(List* lst, ListNode* cell)  //add new cell to the begining of the lst
  168. {
  169.     lst->head=lst->tail=cell;
  170. }
  171.  
  172.  
  173. ListNode* allocateNewCell(char* data)  //allocation new cell
  174. {
  175.     ListNode* cell;  //holds the cell
  176.     cell=(ListNode*)malloc(sizeof(ListNode));  //allocate ListNode
  177.     if(cell==NULL)  //if allocation faild
  178.     {
  179.         //exit
  180.     }
  181.     cell->dataPtr=data;
  182.     cell->next=NULL;
  183.    
  184.     return cell;
  185. }
  186.  
  187.  
  188. char* allocateNewData(char ch)  //allocate string
  189. {
  190.     char* string;  //holds string
  191.     string=(char*)malloc(2*sizeof(char));  //allocate string
  192.     if(string==NULL)  //if allocation faild
  193.     {
  194.         //exit
  195.     }
  196.     string[0]=ch;
  197.     string[1]='\0';
  198.     return string;
  199.    
  200. }
  201.  
  202.  
  203. void makeEmptyList(List* lst)  //make empty list
  204. {
  205.     lst->head=lst->tail=NULL;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement