Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <stdio.h>
- #include <stdlib.h>
- struct student {
- student *nextStudent;
- student *prevStudent;
- int studentAge;
- char *studentName;
- int studentGrade;
- };
- char* setStudentName(char* name) {
- int i = 0;
- if (!(name = (char*)malloc(sizeof(char)))) {
- printf("Memory allocation problem\n");
- exit(1);
- }
- while (1) {
- name[i++] = (char)getchar();
- if (name[i - 1] == '\n') {
- name[i - 1] = '\0';
- break;
- }
- if (!(name = (char*)realloc(name, i + 2))) {
- printf("Memory allocation problem\n");
- exit(1);
- }
- }
- i = 0;
- if (name[0] == '\0') {
- printf("Incorrect name,reenter!\n");
- rewind(stdin);
- setStudentName(name);
- }
- while (name[i] != '\0') {
- if (!(name[i] >= 'a' && name[i] <= 'z' || name[i] >= 'A' && name[i] <= 'Z')) {
- printf("Incorrect information in name,reenter!\n");
- free(name);
- rewind(stdin);
- setStudentName(name);
- }
- i++;
- }
- return name;
- }
- student* addElementInHead(student** head) {
- student *currentElement;
- if (!(currentElement = (student*)malloc(sizeof(student)))) {
- printf("Memory allocation problem\n");
- exit(1);
- }
- printf("Enter student name\n");
- rewind(stdin);
- currentElement->studentName = setStudentName(currentElement->studentName);
- printf("Enter student age\n");
- while ((scanf_s("%d", ¤tElement->studentAge)) != 1 || currentElement->studentAge <= 0) {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- }
- printf("What the grade of student?\n");
- while ((scanf_s("%d", ¤tElement->studentGrade)) != 1 || currentElement->studentGrade <= 0 || currentElement->studentGrade >= 7) {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- }
- currentElement->nextStudent = *head;
- (*head)->prevStudent = currentElement;
- currentElement->prevStudent = NULL;
- *head = currentElement;
- return *head;
- }
- student* addElementInTail(student** tail) {
- student *currentElement;
- if (!(currentElement = (student*)malloc(sizeof(student)))) {
- printf("Memory allocation problem\n");
- exit(1);
- }
- printf("Enter student name\n");
- rewind(stdin);
- currentElement->studentName = setStudentName(currentElement->studentName);
- printf("Enter student age\n");
- while ((scanf_s("%d", ¤tElement->studentAge)) != 1 || currentElement->studentAge <= 0) {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- }
- printf("What the grade of student?\n");
- while ((scanf_s("%d", ¤tElement->studentGrade)) != 1 || currentElement->studentGrade <= 0 || currentElement->studentGrade >= 7) {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- }
- currentElement->prevStudent = *tail;
- (*tail)->nextStudent = currentElement;
- currentElement->nextStudent = NULL;
- *tail = currentElement;
- return *tail;
- }
- student* setList(student** head, student** tail) {
- student *currentElement;
- if (!(currentElement = (student*)malloc(sizeof(student)))) {
- printf("Memory allocation problem\n");
- exit(1);
- }
- printf("Enter student name\n");
- rewind(stdin);
- currentElement->studentName = setStudentName(currentElement->studentName);
- printf("Enter student age\n");
- while ((scanf_s("%d", ¤tElement->studentAge)) != 1 || currentElement->studentAge <= 0) {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- }
- printf("What the grade of student?\n");
- while ((scanf_s("%d", ¤tElement->studentGrade)) != 1 || currentElement->studentGrade <= 0 || currentElement->studentGrade >= 7) {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- }
- *head = *tail = currentElement;
- (*head)->nextStudent = (*tail)->prevStudent = NULL;
- return *head;
- }
- void showStudent(student* head) {
- student *tmpElement = head;
- while (tmpElement) {
- printf("Student name : %s\n", tmpElement->studentName);
- printf("Student age : %d\n", tmpElement->studentAge);
- printf("Student grade : %d\n", tmpElement->studentGrade);
- tmpElement = tmpElement->nextStudent;
- }
- }
- student* deleteAllElements(student** head,student** tail) {
- while (*head != NULL) {
- *head = (*head)->nextStudent;
- }
- return *head;
- }
- int NumberOfElements(student* head) {
- student *tmpElement;
- int studentCounter = 0;
- tmpElement = head;
- while (tmpElement != NULL) {
- studentCounter++;
- tmpElement = tmpElement->nextStudent;
- }
- return studentCounter;
- }
- bool isEmpty(student* head) {
- student *tmpElement;
- int elementCounter = 0;
- tmpElement = head;
- while (tmpElement != NULL) {
- elementCounter++;
- tmpElement = tmpElement->nextStudent;
- }
- if (elementCounter == 0)
- return true;
- else return false;
- }
- student* deleteHead(student** head) {
- *head = (*head)->nextStudent;
- if (*head != NULL) {
- (*head)->prevStudent = NULL;
- }
- return *head;
- }
- student* deleteTail(student** tail) {
- *tail = (*tail)->prevStudent;
- if (*tail != NULL) {
- (*tail)->nextStudent = NULL;
- }
- return *tail;
- }
- int searchElement(student* head) {
- student *tmpElement = head;
- int age;
- while ((scanf_s("%d", &age)) != 1 || age <= 0) {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- }
- while (tmpElement != NULL) {
- if (tmpElement->studentAge == age) {
- break;
- }
- else {
- tmpElement = tmpElement->nextStudent;
- }
- }
- if (tmpElement == NULL) {
- printf("No element's like this,reenter!\n");
- rewind(stdin);
- searchElement(head);
- }
- return age;
- }
- student* deleteElement(int newElement, student** head, student** tail) {
- student *tmpElement = *head, *currentElement = *head;
- while (currentElement->studentAge != newElement) {
- currentElement = currentElement->nextStudent;
- }
- if (currentElement == *head || currentElement == *tail) {
- if (currentElement == *head) {
- *head = (*head)->nextStudent;
- if (*head != NULL) {
- (*head)->prevStudent = NULL;
- }
- }
- else {
- *tail = (*tail)->prevStudent;
- if (*tail != NULL) {
- (*tail)->nextStudent = NULL;
- }
- }
- }
- else {
- while (tmpElement->nextStudent != currentElement) {
- tmpElement = tmpElement->nextStudent;
- }
- tmpElement->nextStudent = currentElement->nextStudent;
- if (currentElement->nextStudent != NULL) {
- currentElement->nextStudent->prevStudent = tmpElement;
- }
- }
- return *head;
- }
- void showMinAndMaxElements(student* head) {
- student *tmpElement = head;
- int min, max;
- min = tmpElement->studentAge;
- max = tmpElement->studentAge;
- while (tmpElement->nextStudent != NULL) {
- if (tmpElement->studentAge < min) {
- min = tmpElement->studentAge;
- }
- tmpElement = tmpElement->nextStudent;
- }
- tmpElement = head;
- while (tmpElement != NULL) {
- if (tmpElement->studentAge > max) {
- max = tmpElement->studentAge;
- }
- tmpElement = tmpElement->nextStudent;
- }
- printf("Minimal element in list - %d\nMaximal element in list - %d\n", min, max);
- }
- student* deleteAllElementsWithThisValue(int newElement, student** head, student** tail) {
- student *tmpElement = *head, *currentElement = *head;
- while (currentElement != NULL && head != NULL) {
- tmpElement = *head;
- if (currentElement->studentAge == newElement) {
- if (currentElement == *head || currentElement == *tail) {
- if (currentElement == *head) {
- *head = (*head)->nextStudent;
- if (*head != NULL) {
- (*head)->prevStudent = NULL;
- }
- }
- else {
- *tail = (*tail)->prevStudent;
- if (*tail != NULL) {
- (*tail)->nextStudent = NULL;
- }
- }
- }
- else {
- while (tmpElement->nextStudent != currentElement) {
- tmpElement = tmpElement->nextStudent;
- }
- tmpElement->nextStudent = currentElement->nextStudent;
- if (currentElement->nextStudent != NULL) {
- currentElement->nextStudent->prevStudent = tmpElement;
- }
- }
- }
- currentElement = currentElement->nextStudent;
- }
- return *head;
- }
- student* replaceValueOfElements(int newElement, int newValue, student* head) {
- student *currentElement = head;
- while (currentElement != NULL) {
- if (currentElement->studentAge == newElement) {
- currentElement->studentAge = newValue;
- }
- currentElement = currentElement->nextStudent;
- }
- return head;
- }
- student* removeDuplicateElements(student** head, student** tail) {
- bool flag = true;
- student *tmpElement = *head, *currentElement = *head, *tmpElement2 = *head;
- if (*head != *tail) {
- while (currentElement->nextStudent != NULL) {
- tmpElement = currentElement->nextStudent;
- while (tmpElement != NULL) {
- if (tmpElement->studentAge == currentElement->studentAge) {
- if (tmpElement == *head || tmpElement == *tail) {
- if (tmpElement == *head) {
- *head = (*head)->nextStudent;
- if (*head != NULL) {
- (*head)->prevStudent = NULL;
- }
- }
- else
- {
- *tail = (*tail)->prevStudent;
- if (*tail != NULL) {
- (*tail)->nextStudent = NULL;
- }
- }
- flag = false;
- }
- else {
- tmpElement2 = *head;
- flag = false;
- while (tmpElement2->nextStudent != tmpElement)
- tmpElement2 = tmpElement2->nextStudent;
- tmpElement2->nextStudent = tmpElement->nextStudent;
- if (tmpElement->nextStudent != NULL) {
- tmpElement->nextStudent->prevStudent = tmpElement2;
- }
- }
- }
- tmpElement = tmpElement->nextStudent;
- }
- if (flag == false) {
- if (currentElement == *head || currentElement == *tail) {
- if (currentElement == *head) {
- *head = (*head)->nextStudent;
- if (*head != NULL) {
- (*head)->prevStudent = NULL;
- }
- }
- else {
- *tail = (*tail)->prevStudent;
- if (*tail != NULL) {
- (*tail)->nextStudent = NULL;
- }
- }
- }
- else {
- tmpElement2 = *head;
- while (tmpElement2->nextStudent != currentElement)
- tmpElement2 = tmpElement2->nextStudent;
- tmpElement2->nextStudent = currentElement->nextStudent;
- if (currentElement->nextStudent != NULL) {
- currentElement->nextStudent->prevStudent = tmpElement2;
- }
- }
- }
- if (currentElement != NULL) {
- currentElement = currentElement->nextStudent;
- }
- if (currentElement == NULL || *head == *tail) {
- break;
- }
- flag = true;
- }
- }
- return *head;
- }
- int main() {
- student *head = NULL, *tail = NULL;
- int typeOfOption = 0, element, newValue, newElement, numberOfStudents;
- bool isElement = false;
- printf("Choose option : \n1)Set list of students\n2)Add element in head\n3)Add element in tail\n4)Show elements\n5)Delete all elements\n6)Number of element's in list\n7)Is empty?\n8)Delete 1st element\n9)Delete last element\n10)Search 1 element\n11)Delete element\n12)Search minimal and maximal element\n13)Delete all elements with this value\n14)Replace value of all choosen elements\n15)Remove duplicate elements\n16)Exit\n");
- while (typeOfOption != 16) {
- printf("Enter option number\n");
- while ((scanf_s("%d", &typeOfOption)) != 1 || typeOfOption <= 0 || typeOfOption >= 17) {
- rewind(stdin);
- printf("Incorrect,reenter!\n");
- }
- switch (typeOfOption) {
- case 1: {
- if (isEmpty(head) == true) {
- head = setList(&head, &tail);
- }
- else {
- printf("Your already have list\n");
- }
- break;
- }
- case 2: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- head = addElementInHead(&head);
- }
- break;
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- tail = addElementInTail(&tail);
- }
- break;
- }
- case 3: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- tail = addElementInTail(&tail);
- }
- break;
- }
- case 4: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- showStudent(head);
- }
- break;
- }
- case 5: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- head = deleteAllElements(&head, &tail);
- }
- break;
- }
- case 6: {
- printf("Elements in your list - %d\n", numberOfStudents = NumberOfElements(head));
- break;
- }
- case 7: {
- if ((isEmpty(head)) == true) {
- printf("Your list is empty\n");
- }
- else {
- printf("Your list is not empty\n");
- }
- break;
- }
- case 8: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- head = deleteHead(&head);
- }
- break;
- }
- case 9: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- tail = deleteTail(&tail);
- }
- break;
- }
- case 10: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- printf("Enter value of element you want to search\n");
- newElement = searchElement(head);
- isElement = true;
- }
- break;
- }
- case 11: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else if (isElement == true) {
- head = deleteElement(newElement, &head, &tail);
- printf("Element deleted\n");
- }
- else {
- printf("No element's to search,type 10 on keyboard to choose what to find\n");
- }
- isElement = false;
- break;
- }
- case 12: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- showMinAndMaxElements(head);
- }
- break;
- }
- case 13: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else if(isElement == true){
- head = deleteAllElementsWithThisValue(newElement, &head, &tail);
- }
- else {
- printf("No element's to search,type 10 on keyboard to choose what to find\n");
- }
- isElement = false;
- break;
- }
- case 14: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else if(isElement == true){
- printf("Enter new value for elements\n");
- while ((scanf_s("%d", &newValue)) != 1) {
- printf("Incorrect information,reenter!\n");
- rewind(stdin);
- }
- head = replaceValueOfElements(newElement, newValue, head);
- }
- else {
- printf("No element's to search,type 10 on keyboard to choose what to find\n");
- }
- isElement = false;
- break;
- }
- case 15: {
- if (isEmpty(head) == true) {
- printf("List not created\n");
- }
- else {
- head = removeDuplicateElements(&head, &tail);
- }
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement