C中的流行中间元素

时间:2018-11-18 12:28:36

标签: c algorithm stack

  1. 所以当我陷入这个问题时,我正在做作业。我们在C语言中使用stack [struct],并且想要弹出堆栈的中间元素,我编写了通用的push和pop函数,以及print和isEmpty函数。我只需要一些问题的帮助。
  2. 我们需要从列表中弹出中间元素。
  3. 所需输入和输出示例如下

    Input:  Stack[] = [1, 2, 3, 4, 5]
    Output: Stack[] = [1, 2, 4, 5]
    
    Input: Stack[] = [[1, 2, 3, 4, 5, 6]
    Output: Stack[] = [1, 2, 4, 5, 6]
    
  4. 我的代码在这里

    #include <stdio.h>
    #include <stdlib.h>
    //self-referenced structure
    struct stackNode {
        int data;
        struct startNode *nextPtr;
    };
    typedef struct stackNode StackNode;
    typedef StackNode StackNodePtr;
    
    //prototypes
    void push(StackNodePtr *topPtr, int info);
    int pop(StackNodePtr *topPtr);
    void printStack(StackNodePtr currentPtr);
    void isEmpty(StackNodePtr *topPtr);
    int main(void){
        StackNodePtr stackPtr = NULL;
        int count= 0;
        char value;
        char input;
        printf("%s","Stack[] = [");
        scanf("%s\n", input );
    
        while(input != "]"){
            scanf("%c, ",&value);
            count++;
            push(&stackPtr, value);
        }
        if(count%2==0){
            count = count / 2;
        }
        else {
            ++count;
            count = count / 2;
        }
        puts("\n");
        printf("Stack[] = ");
        printStack()
    }
    
    void push(StackNodePtr *topPtr,int info){
        StackNodePtr newPtr = malloc(sizeof(StackNode));
    
        if (newPtr != NULL){
            newPtr->data = info;
            newPtr->nextPtr = *topPtr;
            *topPtr = newPtr;
        }
        else {
            printf("%d not inserted, no memory\n", info);
        }
    }
    int pop(StackNodePtr *topPtr){
        StackNodePtr tempPtr = *topPtr;
        int popValue = (*topPtr)->data;
        *topPtr =(*topPtr)->nextPtr;
        return popValue;
    }
    void printStack(StackNodePtr currentPtr){
        if(currentPtr == NULL) {
            puts("The stack is empty\n");
        } else {
            printf("%s","[");
            while(currentPtr != NULL) {
                printf("%d, ",currentPtr->data );
                currentPtr = currentPtr->nextPtr;
            }
            printf("%s","]");
        }
    }
    
    void isEmpty(StackNodePtr topPtr){
        return topPtr == NULL;
    }
    
  5. 编辑:

    //Author: Shivam taneja
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //self-referenced structure
    struct stackNode {
        int data;
        struct stackNode *nextPtr;
    };
    typedef struct stackNode StackNode;
    typedef StackNode *StackNodePtr;
    
    //prototypes
    void push(StackNodePtr *topPtr, int info);
    int pop(StackNodePtr *topPtr, int iter);
    void printStack(StackNodePtr currentPtr);
    int main(void){
        StackNodePtr stackPtr = NULL;
        int count = 0;
        char value;
        printf("%s","Stack[] = [");
        scanf("%c, ", &value);
        while(!(value == ']')) {
            printf()
            scanf("%c",&value);
            count++;
            push(&stackPtr, value);
        }
        if(count%2==0){
            count = count / 2;
        }
        else {
            ++count;
            count = count / 2;
        }
        puts("\n");
        pop(&stackPtr, count);
        printf("Stack[] = ");
        printStack(stackPtr);
    }
    
    void push(StackNodePtr *topPtr,int info){
        StackNodePtr newPtr = malloc(sizeof(StackNode));
    
        if (newPtr != NULL){
            newPtr->data = info;
            newPtr->nextPtr = *topPtr;
            *topPtr = newPtr;
        }
        else {
            printf("%d not inserted, no memory\n", info);
        }
    }
    int pop(StackNodePtr *topPtr, int iter){
        StackNodePtr tempPtr = *topPtr;
        while(iter != 0){
            iter--;
            *topPtr =(*topPtr)->nextPtr;
        }
        int popValue = (*topPtr)->data;
        return popValue;
    }
    void printStack(StackNodePtr currentPtr){
        if(currentPtr == NULL) {
            puts("The stack is empty\n");
        } else {
            printf("%s","[");
            while(currentPtr != NULL) {
                printf("%d, ",currentPtr->data );
                currentPtr = currentPtr->nextPtr;
            }
            printf("%s","]");
        }
    }
    

1 个答案:

答案 0 :(得分:0)

由于使用的是列表,因此与数组相比,必须做的不同,但是对数组进行操作的方法如下:

[0 | 1 | 2 | 3 | ? | ...]

假设您要弹出元素1。

然后保留当前堆栈指针的地址,移至要弹出的指针,将其弹出,然后将所有元素移到最前面,然后将指针移回到上一个指针-1。

结果:

[0 | 2 | 3 | ? | ...]

您需要执行完全相同的操作,但要使用列表。

您的堆栈如下所示:

0->1->2->3->?

您要弹出1,您要做的是转到节点1,将其弹出,然后将所有元素移到最前面:

0->2->3->?

让我们看看如果仅删除1会发生什么: 0->? 2->3->?

列表被切断,您需要重新加入。

要重新加入,您需要知道previous节点和next节点,然后,就像previous.next = next一样简单。

由于我们不能简单地往回看,因此在遍历列表时需要跟踪上一个节点,因此,在跟踪上一个节点的同时找到所需的元素,然后只需从previous.next = current重新连接连接即可,然后将其返回到previous.next = current.next