Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的pop中间元素 所以当我被这个问题困住的时候,我正在做我的作业。我们在C语言中使用stack[struct],希望弹出堆栈的中间元素,我编写了常用的push和pop函数,以及print和isEmpty函数。我只是需要一些问题的帮助 我们需要从列表中弹出中间元素_C_Algorithm_Stack - Fatal编程技术网

C语言中的pop中间元素 所以当我被这个问题困住的时候,我正在做我的作业。我们在C语言中使用stack[struct],希望弹出堆栈的中间元素,我编写了常用的push和pop函数,以及print和isEmpty函数。我只是需要一些问题的帮助 我们需要从列表中弹出中间元素

C语言中的pop中间元素 所以当我被这个问题困住的时候,我正在做我的作业。我们在C语言中使用stack[struct],希望弹出堆栈的中间元素,我编写了常用的push和pop函数,以及print和isEmpty函数。我只是需要一些问题的帮助 我们需要从列表中弹出中间元素,c,algorithm,stack,C,Algorithm,Stack,下面给出了所需输入和输出的示例 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] 我的代码在这里 #include <stdio.h> #include <stdlib.h> //self-referenced structure struct s

下面给出了所需输入和输出的示例

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]
  • 我的代码在这里

    #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;
    }
    
    #包括
    #包括
    //自引用结构
    结构堆栈节点{
    int数据;
    结构startNode*nextPtr;
    };
    typedef结构stackNode stackNode;
    typedef StackNode StackNodePtr;
    //原型
    无效推送(StackNodePtr*topttr,int-info);
    int pop(StackNodePerpt*TopTR);
    无效打印堆栈(StackNodePtr currentPtr);
    void isEmpty(StackNodePtr*toptr);
    内部主(空){
    StackNodePtr stackPtr=NULL;
    整数计数=0;
    字符值;
    字符输入;
    printf(“%s”,“堆栈[]=[”);
    scanf(“%s\n”,输入);
    while(输入!=“]”){
    scanf(“%c,”,&value);
    计数++;
    推送(&stackPtr,值);
    }
    如果(计数%2==0){
    计数=计数/2;
    }
    否则{
    ++计数;
    计数=计数/2;
    }
    卖出(“\n”);
    printf(“堆栈[]=”;
    printStack()
    }
    无效推送(StackNodePtr*topttr,int-info){
    StackNodePtr newPtr=malloc(sizeof(StackNode));
    如果(newPtr!=NULL){
    newPtr->data=info;
    newPtr->nextPtr=*topttr;
    *topttr=newPtr;
    }
    否则{
    printf(“%d未插入,无内存\n”,info);
    }
    }
    int pop(StackNodePtr*topPtr){
    StackNodePtr temptr=*topttr;
    int popValue=(*topttr)->数据;
    *topPtr=(*topPtr)->nextPtr;
    返回值;
    }
    无效打印堆栈(StackNodePtr currentPtr){
    如果(currentPtr==NULL){
    puts(“堆栈为空\n”);
    }否则{
    printf(“%s”和“[”);
    while(currentPtr!=NULL){
    printf(“%d”,当前ptr->data);
    currentPtr=currentPtr->nextPtr;
    }
    printf(“%s”和“]”);
    }
    }
    void isEmpty(StackNodePtr-toptr){
    返回topttr==NULL;
    }
    
  • 编辑:

    //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","]");
        }
    }
    
    //作者:Shivam taneja
    #包括
    #包括
    #包括
    //自引用结构
    结构堆栈节点{
    int数据;
    结构stackNode*nextPtr;
    };
    typedef结构stackNode stackNode;
    typedef StackNode*StackNodePtr;
    //原型
    无效推送(StackNodePtr*topttr,int-info);
    int pop(StackNodePtr*topPtr,int iter);
    无效打印堆栈(StackNodePtr currentPtr);
    内部主(空){
    StackNodePtr stackPtr=NULL;
    整数计数=0;
    字符值;
    printf(“%s”,“堆栈[]=[”);
    scanf(“%c,”,&value);
    而(!(值=']')){
    printf()
    scanf(“%c”和“值”);
    计数++;
    推送(&stackPtr,值);
    }
    如果(计数%2==0){
    计数=计数/2;
    }
    否则{
    ++计数;
    计数=计数/2;
    }
    卖出(“\n”);
    pop(和堆栈PTR、计数);
    printf(“堆栈[]=”;
    打印堆栈(stackPtr);
    }
    无效推送(StackNodePtr*topttr,int-info){
    StackNodePtr newPtr=malloc(sizeof(StackNode));
    如果(newPtr!=NULL){
    newPtr->data=info;
    newPtr->nextPtr=*topttr;
    *topttr=newPtr;
    }
    否则{
    printf(“%d未插入,无内存\n”,info);
    }
    }
    int pop(StackNodePtr*topPtr,int iter){
    StackNodePtr temptr=*topttr;
    while(iter!=0){
    国际热核聚变实验堆;
    *topPtr=(*topPtr)->nextPtr;
    }
    int popValue=(*topttr)->数据;
    返回值;
    }
    无效打印堆栈(StackNodePtr currentPtr){
    如果(currentPtr==NULL){
    puts(“堆栈为空\n”);
    }否则{
    printf(“%s”和“[”);
    while(currentPtr!=NULL){
    printf(“%d”,当前ptr->data);
    currentPtr=currentPtr->nextPtr;
    }
    printf(“%s”和“]”);
    }
    }
    

  • 由于您使用的是列表,因此必须与使用数组不同,但以下是使用数组的方法:

    [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
    ,然后您可以返回current的值。

    我没有使用列表,整个代码没有按照我认为的方式工作。我的想法是使用scanf获取数字,然后从字符串中提取每个数字并推送到堆栈中,然后count将完成其工作并弹出中间的数字