Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- const int SIZE = 7;
- typedef struct lnode
- {
- char data;
- struct lnode* next;
- }LNODE;
- typedef struct list
- {
- LNODE* head;
- LNODE* tail;
- }LIST;
- typedef struct student
- {
- LIST first;
- int grade;
- } STUDENT;
- void makeEmptyList(LIST &lst);
- bool isListEmpty(LIST lst);
- LNODE* createNewListNode(int data, LNODE* next);
- void insertNodeToEndList(LIST &lst, LNODE* tail);
- void insertDataToEndList(LIST &lst, int data);
- void addElementToListEnd(LIST &lst, int data);
- void printList(LIST lst);
- void removeNode(LIST &lst, LNODE *node, LNODE* prev);
- STUDENT unScramble(LIST lst);
- void main()
- {
- LIST lst;
- char data;
- makeEmptyList(lst);
- for (int i = 1; i <= SIZE; i++)
- {
- cout << "Please enter the data: ";
- cin >> data;
- addElementToListEnd(lst, data);
- cout << endl;
- }
- STUDENT student = unScramble(lst);
- cout << "The student name is: ";
- printList(student.first);
- cout << endl << "The student grade avarage is: " << student.grade << endl;
- system("pause");
- }
- STUDENT unScramble(LIST lst)
- {
- int grade = 0;
- STUDENT student;
- student.first = lst;
- LNODE *curr = lst.head,*prev = NULL, *saver;
- while (curr != NULL)
- {
- if (isalpha(curr->data))
- {
- prev = curr;
- curr = curr->next;
- }
- else
- {
- grade = grade * 10;
- grade = grade + (curr->data - '0');
- saver = curr;
- curr = curr->next;
- removeNode(student.first, saver, prev);
- }
- }
- student.grade = grade;
- return student;
- }
- void removeNode(LIST &lst, LNODE *node, LNODE* prev)
- {
- if (lst.head == node && lst.tail == node)
- lst.head = lst.tail = NULL;
- else if (lst.head == node)
- lst.head = lst.head->next;
- else if (lst.tail == node)
- {
- lst.tail = prev;
- prev->next = NULL;
- }
- else
- prev->next = node->next;
- delete node;
- }
- void makeEmptyList(LIST &lst)
- {
- lst.head = NULL;
- lst.tail = NULL;
- }
- void insertDataToEndList(LIST &lst, int data)
- {
- LNODE* newtail;
- newtail = createNewListNode(data, NULL);
- insertNodeToEndList(lst, newtail);
- }
- void addElementToListEnd(LIST &lst, int data)
- {
- LNODE* newNode;
- newNode = createNewListNode(data, NULL);
- insertNodeToEndList(lst, newNode);
- }
- void printList(LIST lst)
- {
- LNODE* curr;
- curr = lst.head;
- while (curr != NULL)
- {
- cout << curr->data << " ";
- curr = curr->next;
- }
- }
- void insertNodeToEndList(LIST &lst, LNODE* tail)
- {
- if (isListEmpty(lst) == true)
- {
- lst.head = lst.tail = tail;
- }
- else
- {
- lst.tail->next = tail;
- lst.tail = tail;
- }
- tail->next = NULL;
- }
- bool isListEmpty(LIST lst)
- {
- bool empty;
- if (lst.head == NULL)
- empty = true;
- else
- empty = false;
- return empty;
- }
- LNODE* createNewListNode(int data, LNODE* next)
- {
- LNODE* res;
- res = new LNODE;
- res->data = data;
- res->next = next;
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement