数据结构 代码关键部分

线性表:

  插入:list->data[i+1] == list->data[i]; 所有元素后移

  删除: list->data[i] == liat->data[i+1];元素前移

 单链表:

  头插:newNode->data = data;  newNode->next = *head;  *head = newNode;

  尾插:

newNode->data = data;
Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode;  

双链表:

  删除指定元素

if (curr->prev != NULL) {
        curr->prev->next = curr->next;
    } else {
        *head = curr->next;
    }
    if (curr->next != NULL) {
        curr->next->prev = curr->prev;
    }

循环单链表:

  插入:tail->next = newNode;    newNode->next = *head;

  删除中间或尾部节点:prev->next = curr->next;

循环双链表:

  头部插入:

     newNode->next = *head;
        newNode->prev = (*head)->prev;
        (*head)->prev->next = newNode;
        (*head)->prev = newNode;
    }
    *head = newNode;

  尾部插入:

*head = newNode;
} else {
newNode->next = *head;
newNode->prev = (*head)->prev;
(*head)->prev->next = newNode;
(*head)->prev = newNode;
}

  删除头结点

Node* temp = *head;     
*head = (*head)->next; (*head)->prev = temp->prev; temp->prev->next = *head; } free(temp);

  删除尾节点

     Node* temp = (*head)->prev;
temp->prev->next = *head; (*head)->prev = temp->prev; } free(temp);

栈:

   入栈:stack->top++;   stack->data[stack->top] = value;

  出栈:value = stack->data[stack->top];    stack->top--;

  更新指定位置的元素:stack->data[index] = value;

  按值查找:

  for (i = stack->top; i >= 0; i--) {
        if (stack->data[i] == value) {
            printf("元素 %d 在栈中的位置为:%d\n", value, i);
            return i;
        }
    }

队列:

   判满:(queue->rear + 1) % MAX_SIZE == queue->front;

  入队:queue->rear = (queue->rear + 1) % MAX_SIZE;     queue->data[queue->rear] = value;

  出队:queue->front = (queue->front + 1)%MAX_SIZE;

  按值查找:

    while (i != queue->rear) {
        if (queue->data[i] == value) {
            printf("元素 %d 在队列中的位置为:%d\n", value, index);
            return index;
        }

        i = (i + 1) % MAX_SIZE;
        index++;
    }

串:

   添加字符到串尾:string->data[string->length] = ch;  string->data[string->length + 1] = '\0';  string->length++;

  删除串尾字符:string->data[string->length - 1] = '\0';  string->length--;

括号匹配:

   入栈:stack->data[++stack->top] = element;

  出栈:stack->data[stack->top--];

  判断匹配:

  

int isMatchingPair(char left, char right) {
    if (left == '(' && right == ')')
        return 1;
    else if (left == '[' && right == ']')
        return 1;
    else if (left == '{' && right == '}')
        return 1;
    else
        return 0;
}

int isBalanced(char* expression) {
    Stack stack;
    int i = 0;

    initialize(&stack, 100);

    while (expression[i] != '\0') {
        if (expression[i] == '(' || expression[i] == '[' || expression[i] == '{') {
            push(&stack, expression[i]);
        } else if (expression[i] == ')' || expression[i] == ']' || expression[i] == '}') {
            if (isEmpty(&stack) || !isMatchingPair(pop(&stack), expression[i])) {
                return 0;
            }
        }
        i++;
    }

    if (isEmpty(&stack)) {
        return 1;
    } else {
        return 0;
    }
}

中缀表达式的计算:

KMP:

 

posted @ 2023-07-12 20:40  nullIsland01  阅读(32)  评论(0)    收藏  举报