Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- #include <string.h>
- struct dllNode {
- int data;
- struct dllNode* next;
- struct dllNode* prev;
- };
- struct dll {
- struct dllNode* first;
- struct dllNode* last;
- };
- struct minmaxdll {
- struct dllNode *minValue;
- struct dllNode *maxValue;
- };
- struct node {
- char payload;
- struct node *next;
- };
- struct stack {
- struct node* root;
- };
- dllNode* newDoublyLinkedListNode(int data) {
- dllNode *node = (dllNode*)calloc(1, sizeof(dllNode));
- node->data = data;
- node->prev = NULL;
- node->next = NULL;
- return node;
- }
- dll * emptyDoublyLinkedList() {
- dll *list = (dll*)calloc(1, sizeof(dll));
- list->first = NULL;
- list->last = NULL;
- return list;
- }
- void pushToStart(dll *list, int data) {
- if (list->first == NULL) {
- dllNode *newNode = newDoublyLinkedListNode(data);
- list->first = newNode;
- list->last = newNode;
- }
- else {
- dllNode* node = list->first;
- dllNode *newNode = newDoublyLinkedListNode(data);
- newNode->next = node;
- if (node->prev == NULL) {
- list->first = newNode;
- }
- else {
- newNode->prev = node->prev;
- node->prev->next = newNode;
- }
- node->prev = newNode;
- }
- }
- void pushToEnd(dll* list, int data) {
- if (list->last == NULL) {
- dllNode *newNode = newDoublyLinkedListNode(data);
- list->first = newNode;
- list->last = newNode;
- }
- else {
- dllNode* node = list->last;
- dllNode *newNode = newDoublyLinkedListNode(data);
- newNode->prev = node;
- if (node->next == NULL) {
- list->last = newNode;
- }
- else {
- newNode->next = node->next;
- node->next->prev = newNode;
- }
- node->next = newNode;
- }
- }
- void dllNodePrint(dllNode *node) {
- printf("%d\n", node->data);
- if (node->next != NULL) {
- dllNodePrint(node->next);
- }
- }
- void dllPrint(dll *list) {
- puts("\n");
- if (list->first != NULL) {
- dllNodePrint(list->first);
- }
- }
- void dllNodeRemove(dllNode *node) {
- if (node->next != NULL) {
- dllNodeRemove(node->next);
- }
- free(node);
- }
- void dllRemoveAll(dll* list) {
- if (list->first != NULL) {
- dllNodeRemove(list->first);
- }
- list->first = NULL;
- list->last = NULL;
- }
- int dllSize(dll *list) {
- int size = 0;
- dllNode *node = list->first;
- while (node != NULL) {
- size++;
- node = node->next;
- }
- return size;
- }
- bool dllIsEmpty(dll *list) {
- if (list->first == NULL && list->last == NULL) {
- return true;
- }
- return false;
- }
- bool deleteFirst(dll *list) {
- if (list->first == NULL) {
- return false;
- }
- if (list->first->next == NULL) {
- list->first = NULL;
- list->last = NULL;
- free(list->first);
- return true;
- }
- list->first = list->first->next;
- list->first->prev = NULL;
- return true;
- }
- bool deleteLast(dll *list) {
- if (list->last == NULL) {
- return false;
- }
- if (list->last->prev == NULL) {
- list->first = NULL;
- list->last = NULL;
- return true;
- }
- list->last = list->last->prev;
- list->last->next = NULL;
- return true;
- }
- bool dllFind(dll *list, int value) {
- if (list->first == NULL) {
- return false;
- }
- dllNode *node = list->first;
- while (node != NULL) {
- if (node->data == value) {
- return true;
- }
- node = node->next;
- }
- return false;
- }
- minmaxdll * newMinMaxHolder(dllNode *min, dllNode *max) {
- minmaxdll *holder = (minmaxdll*)calloc(1, sizeof(minmaxdll));
- holder->maxValue = max;
- holder->minValue = min;
- return holder;
- }
- minmaxdll * findMinMax(dll *list) {
- if (list->first == NULL) {
- return NULL;
- }
- dllNode *min = list->first;
- dllNode *max = list->first;
- dllNode *node = list->first;
- while (node != NULL) {
- if (node->data > max->data) {
- max = node;
- }
- if (node->data < min->data) {
- min = node;
- }
- node = node->next;
- }
- return newMinMaxHolder(min, max);
- }
- bool dllRemoveWith(dll *list, int value) {
- dllNode *node = list->first;
- while (node != NULL) {
- if (node->data == value) {
- if (node == list->last && node == list->first) {
- list->last = list->first = NULL;
- return true;
- }
- if (node->prev != NULL) {
- node->prev->next = node->next;
- if (node == list->last)
- list->last = node->prev;
- }
- if (node->next != NULL) {
- node->next->prev = node->prev;
- if (node == list->first)
- list->first = node->next;
- }
- return true;
- }
- node = node->next;
- }
- return false;
- }
- bool dllRemoveAllWith(dll *list, int value) {
- bool flag = false;
- dllNode *node = list->first;
- while (node != NULL) {
- if (node->data == value) {
- if (node == list->last && node == list->first) {
- list->last = list->first = NULL;
- flag = true;
- }
- if (node->prev != NULL) {
- node->prev->next = node->next;
- if (node == list->last)
- list->last = node->prev;
- }
- if (node->next != NULL) {
- node->next->prev = node->prev;
- if (node == list->first)
- list->first = node->next;
- }
- flag = true;
- }
- node = node->next;
- }
- return flag;
- }
- bool dllReplaceAll(dll *list, int value, int newValue) {
- dllNode *node = list->first;
- bool flag = false;
- while (node != NULL) {
- if (node->data == value) {
- node->data = newValue;
- flag = true;
- }
- node = node->next;
- }
- return flag;
- }
- bool dllIsSymmetric(dll *list) {
- dllNode *first = list->first;
- dllNode *second = list->last;
- if (first->data != second->data) {
- return false;
- }
- while (first != NULL && second != NULL && first != second && first->next != second) {
- if (first->data != second->data){
- return false;
- }
- first = first->next;
- second = second->prev;
- }
- return true;
- }
- int enterInt() {
- int number = 0;
- do {
- fflush(stdin);
- } while (!scanf("%d", &number));
- return number;
- }
- node * newNode(char value) {
- node *n = (node*)calloc(1, sizeof(node));
- if (n != NULL) {
- n->next = NULL;
- n->payload = value;
- }
- return n;
- }
- stack * newStack() {
- stack *s = (stack*)calloc(1, sizeof(stack));
- if (s != NULL) {
- s->root = NULL;
- }
- return s;
- }
- void stackPush(stack *stack, char payload) {
- if (stack == NULL) return;
- if (stack->root == NULL) {
- stack->root = newNode(payload);
- }
- else {
- node *n = newNode(payload);
- if (n == NULL) return;
- n->next = stack->root;
- stack->root = n;
- }
- }
- char stackPop(stack *stack, int *failFlag) {
- if (stack == NULL || stack->root == NULL) {
- if (failFlag != NULL) failFlag[0] = 1;
- return 0;
- }
- char ret = stack->root->payload;
- stack->root = stack->root->next;
- return ret;
- }
- char stackPeek(stack *stack, int *failFlag) {
- if (stack == NULL || stack->root == NULL) {
- if (failFlag != NULL) failFlag[0] = 1;
- return 0;
- }
- return stack->root->payload;
- }
- int isValid(char c) {
- return (c >= 97 && c <= 122) || // [a-z]
- (c >= 65 && c <= 90) || // [A-Z]
- (c >= 40 && c <= 43) || //[ ( ) + * ]
- c == 94 || // ^
- c == 45 || // -
- c == 61 || // =
- c == 47; // /
- }
- int priority(char ch) {
- switch (ch) {
- case '^':
- return 4;
- case '*': case'/':
- return 3;
- case '+': case '-':
- return 2;
- case '(':
- return 1;
- default:
- return 0;
- }
- }
- char * enterInfix(char *message) {
- int index = 0;
- char *infix = (char*)calloc(1, sizeof(char));
- if (infix == NULL) return NULL;
- printf("%s", message);
- char c;
- while ((c = getchar()) != -1) {
- if (c == '\n') {
- infix[index] = '\0';
- break;
- }
- else if (isValid(c)) {
- infix[index++] = c;
- }
- else {
- printf("Invalid symbol found on index %d - '%c'\n", index, c);
- return NULL;
- }
- }
- if (index == 0 || infix[index - 1] != '=') {
- return NULL;
- }
- for (int i = 1; i < index; i++) {
- if (infix[i] == infix[i - 1]) {
- return NULL;
- }
- }
- return infix;
- }
- char* infixToPostfix(char* infix, int length) {
- stack *stack = newStack();
- char* pnString = (char*)calloc(length + 1, sizeof(char));
- int index = 0;
- int pnIndex = 0;
- int errorFlag = 0;
- if (pnString == NULL) {
- return NULL;
- }
- while (infix[index] != '=') {
- char c = infix[index];
- if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
- pnString[pnIndex] = infix[index];
- pnIndex++;
- }
- else {
- if (infix[index] == ')') {
- if (stack->root == NULL) {
- return NULL;
- }
- errorFlag = 0;
- while (stackPeek(stack, &errorFlag) != '(' && !errorFlag) {
- pnString[pnIndex++] = stackPop(stack, NULL);
- }
- stackPop(stack, NULL);
- }
- else {
- if (infix[index] == '(') {
- stackPush(stack, infix[index]);
- length -= 2;
- }
- else {
- if (infix[index] == '^' ||
- infix[index] == '+' ||
- infix[index] == '-' ||
- infix[index] == '*' ||
- infix[index] == '/') {
- if (stack->root == NULL) {
- stackPush(stack, infix[index]);
- }
- else {
- if (stack->root != NULL && (priority(stackPeek(stack, NULL)) >= priority(infix[index]))) {
- pnString[pnIndex++] = stackPop(stack, NULL);
- stackPush(stack, infix[index]);
- }
- else {
- stackPush(stack, infix[index]);
- }
- }
- }
- }
- }
- }
- index++;
- }
- while (stack->root != NULL) {
- if (stackPeek(stack, NULL) == '(') {
- return NULL;
- }
- pnString[pnIndex++] = stackPop(stack, NULL);
- }
- pnString[pnIndex++] = '=';
- pnString[pnIndex] = '\0';
- return pnString;
- }
- void printForm(char* postfix) {
- int i = 0;
- printf("Postfix form: \n");
- while (postfix[i] != '\0') {
- printf("%c", postfix[i]);
- i++;
- }
- printf("\n");
- }
- int main() {
- printf("Enter part of program\n1 - Work with a doubly linked list\n2 - Polish notation \n");
- char choice;
- fflush(stdin);
- scanf("%c", &choice);
- fflush(stdin);
- while (true){
- if (choice == '1') {
- dll* list = emptyDoublyLinkedList();
- while (true) {
- printf("\n\n\nMenu: \n");
- printf("1 - Add to start\n2 - Add to end\n3 - Print all elements\n4 - Remove all elements\n5 - List size\n6 - Is list empty?\n7 - Remove first\n8 - Remove last\n");
- printf("9 - Find\n10 - Find max and min values\n11 - Remove by value\n12 - Remove all by value\n13 - Edit\n14 - Is list symmetric\n15 - Exit the program\nyour choice: ");
- int option = enterInt();
- switch (option) {
- case 1: {
- printf("Enter the number you want add to list\n");
- int number = enterInt();
- pushToStart(list, number);
- break;
- }
- case 2: {
- printf("Enter the number you want add to list\n");
- int number = enterInt();
- pushToEnd(list, number);
- break;
- }
- case 3: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- dllPrint(list);
- break;
- }
- case 4: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- dllRemoveAll(list);
- printf("Completed successfully\n");
- break;
- }
- case 5: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- printf("List size: %d\n", dllSize(list));
- break;
- }
- case 6: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- }
- else {
- printf("List isn't empty\n");
- }
- break;
- }
- case 7: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- if (deleteFirst(list)) {
- printf("Deleted successfully\n");
- }
- else {
- printf("List is empty\n");
- }
- break;
- }
- case 8: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- if (deleteLast(list)) {
- printf("Deleted successfully\n");
- }
- else {
- printf("List is empty\n");
- }
- break;
- }
- case 9: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- int value = enterInt();
- if (dllFind(list, value)){
- printf("Value was found\n");
- }
- else{
- printf("Search unsuccessful\n");
- }
- break;
- }
- case 10: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- minmaxdll *minmax = findMinMax(list);
- if (minmax == NULL) {
- printf("Search has failed. Please, check list\n");
- break;
- }
- printf("Max: %d\nMin: %d\n", minmax->maxValue->data, minmax->minValue->data);
- break;
- }
- case 11: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- int value = enterInt();
- if (dllRemoveWith(list, value)){
- printf("Remove successfully done\n");
- break;
- }
- }
- case 12: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- int value = enterInt();
- if (dllRemoveAllWith(list, value)){
- printf("Remove successfully done\n");
- break;
- }
- }
- case 13: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- int value = enterInt();
- int newValue = enterInt();
- if (dllReplaceAll(list, value, newValue)){
- printf("Replace successfully done\n");
- break;
- }
- break;
- }
- case 14: {
- if (dllIsEmpty(list)) {
- printf("List is empty\n");
- break;
- }
- if (dllIsSymmetric(list)) {
- printf("List is symmetric\n");
- break;
- }
- else{
- printf("List isn't symmetric\n");
- break;
- }
- }
- case 15: {
- return 0;
- }
- }
- }
- }
- else if (choice == '2') {
- char* classicExpression = enterInfix("Enter infix form: ");
- if (classicExpression == NULL) {
- printf("Invalid input\n");
- return 0;
- }
- int length = strlen(classicExpression);
- printForm(infixToPostfix(classicExpression, length));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement