Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct list_node
- {
- char* dataPtr;
- struct list_node* next;
- }ListNode;
- typedef struct list
- {
- ListNode* head;
- ListNode* tail;
- }List;
- typedef struct student
- {
- List first; int grade;
- } Student;
- /* declare about functions */
- void makeEmptyList(List* lst);
- void insertDataToEndList(List* lst,char ch);
- char* allocateNewData(char ch);
- ListNode* allocateNewCell(char* data);
- void addNewCellToBegining(List* lst, ListNode* cell);
- void addNewCellToEnd(List* lst, ListNode* cell);
- Student unScramble(List lst);
- void deallocateCell(List lst, ListNode* curr, ListNode* prev); //dellete a cell from the list
- void delleteCellFromInner(List lst,ListNode* curr, ListNode* prev);
- void delleteCellFromEnd(List lst, ListNode* curr, ListNode* prev);
- void printStudent(Student* student);
- int main(void)
- {
- List lst;
- Student student;
- char ch;
- makeEmptyList(&lst);
- printf("Please enter the scrambled student:\n");
- ch = (char)getchar();
- while(ch != '\n')
- {
- insertDataToEndList(&lst, ch);
- ch = (char)getchar();
- }
- student = unScramble(lst);
- printStudent(&student);
- }
- void printStudent(Student* student)
- {
- ListNode* curr;
- curr=student->first.head;
- printf("The grade is : %d \n",student->grade);
- printf("The name is : ");
- while(curr)
- {
- printf("%c",curr->dataPtr[0]);
- curr=curr->next;
- }
- }
- Student unScramble(List lst) //this function is unscramble the list
- {
- Student s1;
- ListNode* curr; //holds the current cell
- ListNode* prev; //holds the prev cell
- int num;
- num=0;
- curr=lst.head;
- prev=NULL;
- while(curr!=NULL) //while curr isnt NULL
- {
- if(curr->dataPtr[0]>='0' && curr->dataPtr[0]<='9') //if the char is any number
- {
- num=num*10+((int)(curr->dataPtr[0]-'0'));
- }
- else
- {
- prev=curr;
- curr=curr->next;
- }
- }
- s1.grade=num;
- s1.first=lst;
- return s1;
- }
- void deallocateCell(List lst, ListNode* curr, ListNode* prev) //dellete a cell from the list
- {
- if(lst.head==lst.tail) //the list has only one cell
- {
- free(curr->dataPtr);
- free(curr);
- curr=NULL;
- }
- else if(lst.tail==curr)
- {
- delleteCellFromEnd(lst,curr,prev);
- }
- else
- {
- delleteCellFromInner(lst,curr,prev);
- }
- }
- void delleteCellFromEnd(List lst, ListNode* curr, ListNode* prev) //dellete cell from the end of the list
- {
- prev->next=NULL;
- free(curr->dataPtr);
- free(curr);
- lst.tail=prev;
- curr=NULL;
- }
- void delleteCellFromInner(List lst,ListNode* curr, ListNode* prev) //dellete cell from inner place
- {
- ListNode* save=curr;
- prev->next=curr->next;
- curr=curr->next;
- free(save->dataPtr);
- free(save);
- }
- void insertDataToEndList(List* lst,char ch) //insert data to the list
- {
- ListNode* cell; //holds new cell
- char* data; //hold string
- data=allocateNewData(ch);
- cell=allocateNewCell(data);
- if (lst->head==NULL) //if lst is empty
- addNewCellToBegining(lst,cell);
- else
- addNewCellToEnd(lst,cell);
- }
- void addNewCellToEnd(List* lst, ListNode* cell) //add new cell to the end of the lst
- {
- lst->tail->next=cell;
- lst->tail=cell;
- }
- void addNewCellToBegining(List* lst, ListNode* cell) //add new cell to the begining of the lst
- {
- lst->head=lst->tail=cell;
- }
- ListNode* allocateNewCell(char* data) //allocation new cell
- {
- ListNode* cell; //holds the cell
- cell=(ListNode*)malloc(sizeof(ListNode)); //allocate ListNode
- if(cell==NULL) //if allocation faild
- {
- //exit
- }
- cell->dataPtr=data;
- cell->next=NULL;
- return cell;
- }
- char* allocateNewData(char ch) //allocate string
- {
- char* string; //holds string
- string=(char*)malloc(2*sizeof(char)); //allocate string
- if(string==NULL) //if allocation faild
- {
- //exit
- }
- string[0]=ch;
- string[1]='\0';
- return string;
- }
- void makeEmptyList(List* lst) //make empty list
- {
- lst->head=lst->tail=NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement