Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using  namespace  std;
  5. const int SIZE = 7;
  6. typedef struct lnode
  7. {
  8.     char data;
  9.     struct lnode* next;
  10. }LNODE;
  11.  
  12. typedef struct list
  13. {
  14.     LNODE* head;
  15.     LNODE* tail;
  16. }LIST;
  17.  
  18. typedef struct student
  19. {
  20.     LIST first;
  21.     int grade;
  22. } STUDENT;
  23.  
  24.  
  25. void makeEmptyList(LIST &lst);
  26. bool isListEmpty(LIST lst);
  27. LNODE* createNewListNode(int data, LNODE* next);
  28. void insertNodeToEndList(LIST &lst, LNODE* tail);
  29. void insertDataToEndList(LIST &lst, int data);
  30. void addElementToListEnd(LIST &lst, int data);
  31. void printList(LIST lst);
  32. void removeNode(LIST &lst, LNODE *node, LNODE* prev);
  33. STUDENT unScramble(LIST lst);
  34.  
  35. void main()
  36. {
  37.     LIST lst;
  38.     char data;
  39.     makeEmptyList(lst);
  40.     for (int i = 1; i <= SIZE; i++)
  41.     {
  42.         cout << "Please enter the data: ";
  43.         cin >> data;
  44.         addElementToListEnd(lst, data);
  45.         cout << endl;
  46.     }
  47.     STUDENT student = unScramble(lst);
  48.     cout << "The student name is: ";
  49.     printList(student.first);
  50.     cout << endl << "The student grade avarage is: " << student.grade << endl;
  51.     system("pause");
  52. }
  53.  
  54. STUDENT unScramble(LIST lst)
  55. {
  56.     int grade = 0;
  57.     STUDENT student;
  58.     student.first = lst;
  59.     LNODE *curr = lst.head,*prev = NULL, *saver;
  60.     while (curr != NULL)
  61.     {
  62.         if (isalpha(curr->data))
  63.         {
  64.             prev = curr;
  65.             curr = curr->next;
  66.         }
  67.         else
  68.         {
  69.             grade = grade * 10;
  70.             grade = grade + (curr->data - '0');
  71.             saver = curr;
  72.             curr = curr->next;
  73.             removeNode(student.first, saver, prev);
  74.         }
  75.     }
  76.     student.grade = grade;
  77.     return student;
  78. }
  79.  
  80. void removeNode(LIST &lst, LNODE *node, LNODE* prev)
  81. {
  82.     if (lst.head == node && lst.tail == node)
  83.         lst.head = lst.tail = NULL;
  84.     else if (lst.head == node)
  85.         lst.head = lst.head->next;
  86.     else if (lst.tail == node)
  87.     {
  88.         lst.tail = prev;
  89.         prev->next = NULL;
  90.     }
  91.     else
  92.         prev->next = node->next;
  93.     delete node;
  94. }
  95. void makeEmptyList(LIST &lst)
  96. {
  97.     lst.head = NULL;
  98.     lst.tail = NULL;
  99. }
  100. void insertDataToEndList(LIST &lst, int data)
  101. {
  102.     LNODE* newtail;
  103.     newtail = createNewListNode(data, NULL);
  104.     insertNodeToEndList(lst, newtail);
  105. }
  106. void addElementToListEnd(LIST &lst, int data)
  107. {
  108.     LNODE* newNode;
  109.     newNode = createNewListNode(data, NULL);
  110.     insertNodeToEndList(lst, newNode);
  111. }
  112. void printList(LIST lst)
  113. {
  114.     LNODE* curr;
  115.     curr = lst.head;
  116.     while (curr != NULL)
  117.     {
  118.         cout << curr->data << " ";
  119.         curr = curr->next;
  120.     }
  121. }
  122. void insertNodeToEndList(LIST &lst, LNODE* tail)
  123. {
  124.     if (isListEmpty(lst) == true)
  125.     {
  126.         lst.head = lst.tail = tail;
  127.     }
  128.     else
  129.     {
  130.         lst.tail->next = tail;
  131.         lst.tail = tail;
  132.     }
  133.     tail->next = NULL;
  134. }
  135. bool isListEmpty(LIST lst)
  136. {
  137.     bool empty;
  138.     if (lst.head == NULL)
  139.         empty = true;
  140.     else
  141.         empty = false;
  142.     return empty;
  143. }
  144. LNODE* createNewListNode(int data, LNODE* next)
  145. {
  146.     LNODE* res;
  147.     res = new LNODE;
  148.     res->data = data;
  149.     res->next = next;
  150.     return res;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement