BEI_TIAN_XUAN

BEI_TIAN_XUAN 查看完整档案

填写现居城市  |  填写毕业院校  |  填写所在公司/组织填写个人主网站
编辑
_ | |__ _ _ __ _ | '_ \| | | |/ _` | | |_) | |_| | (_| | |_.__/ \__,_|\__, | |___/ 个人简介什么都没有

个人动态

BEI_TIAN_XUAN 提出了问题 · 2月25日

C语言链队销毁问题(求求帮帮孩子)

那位大佬知道啊!我调试一直不行,那位好心得大佬帮帮忙啊!我实在是不行了,孩子求求了。出问题的代码

出问题的代码:

Status DestroyQueue(LinkQueue Q)  //从队头结点依次销毁
{
    while (Q.front)
    {
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }
    return OK;
}

运行后如下:
运行后如下:
全部源码:

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define FALSE 0
typedef int ElemType;
typedef int Status;
typedef struct QNode
{
    ElemType data;
    struct QNode* next;
}QNode, *Queueptr;
typedef struct
{
    Queueptr front;  //队头指针
    Queueptr rear;   //队尾指针
}LinkQueue;
Status InitQueue(LinkQueue* Q);  //初始化
Status InsertQueue(LinkQueue* Q, ElemType e); //入队
Status DeletQueue(LinkQueue* Q, ElemType* e); //出队
Status GetHead(LinkQueue Q);   //取头结点元素
Status DestroyQueue(LinkQueue Q);  //销毁
int main()
{
    LinkQueue Q;
    ElemType e, a;
    int n;
    if (InitQueue(&Q) == 1)
        printf("构建链队成功!\n");
    printf("入队元素个数:");
    scanf("%d", &n);
    printf("请输入入队元素:");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &e);
        InsertQueue(&Q, e);
    }
    printf("入队成功!\n");
    a=GetHead(Q);
    if (a != 0)
        printf("队头元素为:%d\n", a);
    else
        printf("队空!无头元素!\n");
     if (DeletQueue(&Q, &e) == 0)
        printf("队为空!无法出队!\n");
    else
    printf("出队元素为:%d\n", e);
    DestroyQueue(Q);
    printf("销毁完毕!");
    return 0;
}
Status InitQueue(LinkQueue* Q)
{
    Q->front = Q->rear = (Queueptr)malloc(sizeof(QNode));
    if (!Q->front)
        exit(0);
    Q->front->next  = NULL;
    return OK;
}
Status InsertQueue(LinkQueue* Q, ElemType e)
{
    Queueptr P;
    P = (Queueptr)malloc(sizeof(QNode));
    if (!P)
        exit(0);
    P->data = e;
    Q->rear->next = P;
    Q->rear = P;
    return OK;
}
Status GetHead(LinkQueue Q)
{
    if (Q.front == Q.rear)
        return FALSE;
    return (Q.front->next->data);
}
Status DeletQueue(LinkQueue* Q, ElemType* e)
{
    Queueptr P;
    if (Q->front == Q->rear)   //队空
        return FALSE;
    P = Q->front->next;
    *e = P->data;
    Q->front->next = P->next;
    if (Q->rear == P)   //当删除到尾结点时
        Q->rear = Q->front;
    free(P);
    return OK;
}
Status DestroyQueue(LinkQueue Q)  //从队头结点依次销毁
{
    while (Q.front)
    {
        Q.rear = Q.front->next;
        free(Q.front);
        Q.front = Q.rear;
    }
    return OK;
}

关注 1 回答 0

BEI_TIAN_XUAN 回答了问题 · 2月25日

C语言链栈,栈顶元素返回问题

订正后的源码;

#include<stdio.h>
#include<stdlib.h>
typedef int Status;
typedef int ElemType;
#define TRUE 1;
#define ERROR 0;
typedef struct Node
{
    ElemType data;
    struct Node* next;
}StackNode,*StackList;
Status InitStack(StackList S);    //初始化(无头结点)
Status Push(StackList S, ElemType e);  //头插法
Status Pop(StackList S, ElemType* a);   //弹出
int main()
{
    int n;
    ElemType e, a;
    StackList S;
    S = (StackList)malloc(sizeof(StackNode));
    if (InitStack(S) == 1)
    {
        printf("恭喜!构建空链栈成功!\n");
    }
    printf("请输入插入元素个数:");
    scanf("%d", &n);
    printf("请输入插入栈的元素:");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &e);
        Push(S, e);
    }
        printf("恭喜你!插入成功!\n");  
    if (Pop(S, &a) == 1)
    {
        printf("恭喜你!出栈成功!\n");
        printf("删除栈顶元素:%d\n", a);
    }

    return 0;
}
Status InitStack(StackList S)
{
    S = NULL;

    return TRUE;
}
Status Push(StackList S, ElemType e)
{
    StackList P;
    P = (StackList)malloc(sizeof(StackNode));
    P->data = e;
    P->next = S->next;
    S->next = P; //修改栈顶指针
    return TRUE;
}
Status Pop(StackList S, ElemType* a)
{
    StackList P;
    P = S->next;
    if (S == NULL)
        return ERROR;
    S->next = P->next;
    *a = P->data;
    free(P);
    return TRUE;
}

关注 1 回答 2

BEI_TIAN_XUAN 发布了文章 · 2月24日

C语言实现循环(顺序)队列的基本操作

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
#define TRUE 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct
{
    ElemType *base;  //数组基地址,动态分配内存
    int front;   //头指针
    int rear;      //尾指针
}Queue;
Status InitQueue(Queue* Q);   //初始化
Status InsertQueue(Queue* Q, ElemType e);// 入队
Status DeleteQueue(Queue* Q, ElemType* e); //出队
Status LengthQueue(Queue* Q);  //获取队长
void DstoryQueue(Queue *Q); //销毁队
int main()
{
    Queue Q;
    ElemType e;
    int n, a;
    if (InitQueue(&Q) == 1)
        printf("创建循环队列成功!\n");
    printf("输入插入队尾元素个数:");
    scanf("%d", &n);
    printf("输入插入队尾元素:");
    if (n != 0)      //判断队空
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &e);
            a = InsertQueue(&Q, e);

        }
    }
    else
        a = -1;
    if (a == 1) {
        printf("插入成功!\n");
        printf("队长为:%d\n", LengthQueue(&Q));
    }
        if (a==0)
        printf("队已满!无法继续插入了!\n");
    if (DeleteQueue(&Q, &e) == 1)
    {
        printf("删除队首元素为:%d\n", e);
    }
    else
        printf("队为空!无法继续删除了!\n");
    printf("删除后队长为:%d\n", LengthQueue(&Q));
    DstoryQueue(&Q);
    printf("销毁完毕!");

    return 0;
}
Status InitQueue(Queue* Q)
{
    Q->base = (Queue*)malloc(sizeof(ElemType) * MaxSize);
    if (!Q->base)
        exit(0);
    Q->front = Q->rear = 0;

    return TRUE;
}
Status InsertQueue(Queue *Q, ElemType e)
{
    if ((Q->rear + 1) % MaxSize == Q->front)
        return ERROR;
    Q->base[Q->rear] = e;
    Q->rear = (Q->rear + 1) % MaxSize;

    return TRUE;
}
Status DeleteQueue(Queue* Q, ElemType* e)
{
    if (Q->front == Q->rear)
        return ERROR;
    *e = Q->base[Q->front];
    Q->front = (Q->front + 1) % MaxSize;

    return TRUE;
}
Status LengthQueue(Queue* Q)
{
    return ((Q->rear-Q->front+MaxSize)%MaxSize);
}
void DstoryQueue(Queue* Q)
{
    free(Q->base);
    Q->base = NULL;
    Q->front = Q->rear = 0;
}
查看原文

赞 0 收藏 0 评论 0

BEI_TIAN_XUAN 提出了问题 · 2月23日

C语言链栈,栈顶元素返回问题

求求各位大佬了,为什么a返回的是地址啊!

#include<stdio.h>
#include<stdlib.h>
typedef int Status;
typedef int ElemType;
#define TRUE 1;
#define ERROR 0;
typedef struct Node
{
    ElemType data;
    struct Node* next;
}StackNode,*StackList;
Status InitStack(StackList S);    //初始化(无头结点)
Status Push(StackList S, ElemType e);  //头插法
Status Pop(StackList S, ElemType* a);   //弹出
int main()
{
    int n;
    ElemType e, a;
    StackList S;
    S = (StackList)malloc(sizeof(StackNode));
    if (InitStack(S) == 1)
    {
        printf("恭喜!构建空链栈成功!\n");
    }
    printf("请输入插入元素个数:");
    scanf("%d", &n);
    printf("请输入插入栈的元素:");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &e);
        Push(S, e);
    }
        printf("恭喜你!插入成功!\n");  
    if (Pop(S, &a) == 1)
    {
        printf("恭喜你!出栈成功!\n");
        printf("删除栈顶元素:%d\n", a);
    }

    return 0;
}
Status InitStack(StackList S)
{
    S = NULL;

    return TRUE;
}
Status Push(StackList S, ElemType e)
{
    StackList P;
    P = (StackList)malloc(sizeof(StackNode));
    P->data = e;
    P->next = S->next;
    S = P; //修改栈定指针

    return TRUE;
}
Status Pop(StackList S, ElemType *a)
{
    StackList P;
    P = S;
    if (P == NULL)
        return ERROR;
    *a = S->data;
    S = S->next;
    free(P);
    
    return TRUE;
}

关注 1 回答 2

BEI_TIAN_XUAN 提出了问题 · 2月23日

C语言链栈,栈顶元素返回问题

求求各位大佬了,为什么a返回的是地址啊!

#include<stdio.h>
#include<stdlib.h>
typedef int Status;
typedef int ElemType;
#define TRUE 1;
#define ERROR 0;
typedef struct Node
{
    ElemType data;
    struct Node* next;
}StackNode,*StackList;
Status InitStack(StackList S);    //初始化(无头结点)
Status Push(StackList S, ElemType e);  //头插法
Status Pop(StackList S, ElemType* a);   //弹出
int main()
{
    int n;
    ElemType e, a;
    StackList S;
    S = (StackList)malloc(sizeof(StackNode));
    if (InitStack(S) == 1)
    {
        printf("恭喜!构建空链栈成功!\n");
    }
    printf("请输入插入元素个数:");
    scanf("%d", &n);
    printf("请输入插入栈的元素:");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &e);
        Push(S, e);
    }
        printf("恭喜你!插入成功!\n");  
    if (Pop(S, &a) == 1)
    {
        printf("恭喜你!出栈成功!\n");
        printf("删除栈顶元素:%d\n", a);
    }

    return 0;
}
Status InitStack(StackList S)
{
    S = NULL;

    return TRUE;
}
Status Push(StackList S, ElemType e)
{
    StackList P;
    P = (StackList)malloc(sizeof(StackNode));
    P->data = e;
    P->next = S->next;
    S = P; //修改栈定指针

    return TRUE;
}
Status Pop(StackList S, ElemType *a)
{
    StackList P;
    P = S;
    if (P == NULL)
        return ERROR;
    *a = S->data;
    S = S->next;
    free(P);
    
    return TRUE;
}

关注 1 回答 2

BEI_TIAN_XUAN 发布了文章 · 2月21日

C语言实现顺序栈的基本操作举例

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define MaxSize 100
#define true 1
#define error 0
#define OK 1
#define OVERFLOW -2
typedef int ElemType;
typedef struct {    //顺序栈结构(动态)
    ElemType* top;
    ElemType* base;
    int stacksize;
}Sqstack;
int InitStack(Sqstack* s);   //初始化(在C语言中,形参和实参是单向传递,但可用指针(*)传递地址,让形参影响实参,达到双向传递)
int EmptyStack(Sqstack* s);
int Push(Sqstack* s,ElemType e); //插入元素(在c++中,可以用(&)引用型参数实现双向传递)
int StackLength(Sqstack* s);  //元素长度
int StackPrint(Sqstack* s);  //打印元素
int Pop(Sqstack* s); //删除栈顶元素
int DestroyStack(Sqstack* s);  //销毁栈
int main()
{
    int n;
    ElemType e, a;
    Sqstack *s;
    s = (Sqstack*)malloc(sizeof(Sqstack));  //敲黑板重点!指针要有地址!记住分配地址!
    InitStack(s);
    printf("请输入插入元素个数:");
    scanf("%d", &n);
    printf("请输入插入元素:");
    for (int i = 0; i < n; i++) {
        scanf("%d", &e);
        Push(s, e);
    }
    if (EmptyStack(s) == 1)
        printf("**啊!***空表**\n");
    printf ("元素长度:%d\n",StackLength(s));
    printf("打印元素为:");
    StackPrint(s); 
    printf("\n");
    a = Pop(s);   //我在这里翻了车,前面Pop(s)没赋值a,导致Pop(s)引用两次,引发错误。
    if (a == -2)
        printf("栈底溢出!\n");
    else
        printf("删除栈顶元素为:%d\n", a);
    printf("删除后:");
    StackPrint(s);
    printf("\n");
    system("pause");
    printf("销毁栈中.........\n");
    Sleep(3000);
    system("cls");
    if (DestroyStack(s) == 1)
        printf("销毁成功!");
    else
        printf("销毁失败!");

    return 0;
}
int InitStack(Sqstack* s)
{
    s->base = (ElemType*)malloc(sizeof(ElemType) * MaxSize);
    if (!s->base)
        return error;
    s->top = s->base;
    s->stacksize = MaxSize;
    return OK;
}
int EmptyStack(Sqstack* s)
{
    if (s->base == s->top)
        return true;
    else
        return error;
}
int Push(Sqstack* s, ElemType e)
{
    if (s->top - s->base >= MaxSize)
        return OVERFLOW;
    if (!s->base)
        return error;
    *(s->top) = e;
    s->top++;
    return OK;
}

int StackLength(Sqstack* s)
{
    if (s->base == s->top)
        return error;
    else
        return ((s->top) - (s->base));
}
int StackPrint(Sqstack* s)
{
    if (s->base == NULL)
        return error;
    if (s->base == s->top)
        printf("没有元素!");
    ElemType* p;
    p = s->base;
    while (p <(s->top))
    {
        printf("%d ", *p);
        p++;
    }
    return OK;
}
int Pop(Sqstack* s)
{
    ElemType a;
    if (s->base == s->top)
        return OVERFLOW;
    else {
        a = *(s->top-1 );
        s->top--;
    }
    return a;
}
int DestroyStack(Sqstack* s)
{
    free(s);
    s->base = s->top = NULL;
    s->stacksize = 0;
    return OK;
}
查看原文

赞 0 收藏 0 评论 0

BEI_TIAN_XUAN 发布了文章 · 2月21日

C语言版极简推箱子(数组版)

源码如下:

#include<stdio.h>
#include<stdlib.h>
#define ROWS 11
#define COLS 11
int people_cols = 2;
int people_rows = 4;
char map[ROWS][COLS] = {
    "##########",
    "#  ####  #",
    "#        #",
    "# X####  #",
    "# 0      #",
    "#####    #",
    "#  ####  #",
    "#        #",
    "#   ######",
    "#         ",
    "##########"
};
void showMap();
char enterDirection();
void moveToUp();
void moveToDown();
void moveToLeft();
void moveToRight();
int main()
{

    int flag = 1;


    
    while (flag==1) {
        system("cls");
        showMap();
        if (map[9][9] == 'X') {
            printf("恭喜你,你通关游戏了,你的智商已经超过3岁小孩了!\nGood!\n");
            break;
        }
        char dir = enterDirection();
        printf("你输入的方向是:%c\n", dir);
        switch (dir)
        {
        case 'a':
        case 'A':
            moveToLeft();
            break;
        case's':
        case 'S':
            moveToDown();
            break;
        case'w':
        case 'W':
            moveToUp();
            break;
        case'd':
        case 'D':
            moveToRight();
            break;
        case'q':
        case 'Q':
            printf("你的智商不行啊!这连3岁小孩都会!loser!"),
                flag = 0;
            break;
        }

    }

    return 0;
}

void showMap() {
    for (int i = 0; i < ROWS; i++) {
        printf("%s\n", map[i]);
    }
}
char enterDirection() {
    printf("请输入小人的前进方向(X代表箱子,0代表人) w.上 s.下 a.左 d.右 q.退出游戏(回车确认)\n");
    char dir;
    rewind(stdin);
    dir = getchar();

    return dir;
}
void moveToUp()
{
    int next_rows=people_rows-1;
    int next_cols=people_cols;
    if (map[next_rows][next_cols] == ' ') {
        map[people_rows][people_cols] = ' ';
        map[next_rows][next_cols] = '0';
    }
    if (map[next_rows][next_cols] == 'X' && map[next_rows - 1][next_cols] == ' ') {
        map[people_rows][people_cols] = ' ';
        map[next_rows][next_cols] = '0';
        map[next_rows - 1][next_cols] = 'X';
    }
    people_cols = next_cols;
    people_rows = next_rows;
    
}
void moveToDown() {
    int next_rows = people_rows+1 ;
    int next_cols = people_cols;
    if (map[next_rows][next_cols] == ' ') {
        map[people_rows][people_cols] = ' ';
        map[next_rows][next_cols] = '0';
    }
    if (map[next_rows][next_cols] == 'X' && map[next_rows +1][next_cols] == ' ') {
        map[people_rows][people_cols] = ' ';
        map[next_rows][next_cols] = '0';
        map[next_rows +1][next_cols] = 'X';
    }
    people_cols = next_cols;
    people_rows = next_rows;
}
void moveToLeft() {
    int next_rows = people_rows ;
    int next_cols = people_cols-1;
    if (map[next_rows][next_cols] == ' ') {
        map[people_rows][people_cols] = ' ';
        map[next_rows][next_cols] = '0';
    }
    if (map[next_rows][next_cols] == 'X' && map[next_rows][next_cols - 1] == ' ') {
        map[people_rows][people_cols] = ' ';
        map[next_rows][next_cols] = '0';
        map[next_rows][next_cols - 1] = 'X';
    }
    people_cols = next_cols;
    people_rows = next_rows;
}
void moveToRight() {
    int next_rows = people_rows ;
    int next_cols = people_cols+1;
    if (map[next_rows][next_cols] == ' ') {
        map[people_rows][people_cols] = ' ';
        map[next_rows][next_cols] = '0';
    }
    if (map[next_rows][next_cols] == 'X' && map[next_rows ][next_cols+1] == ' ') {
        map[people_rows][people_cols] = ' ';
        map[next_rows][next_cols] = '0';
        map[next_rows ][next_cols+1] = 'X';
    }
    people_cols = next_cols;
    people_rows = next_rows;
}

菜鸟一枚。。。。。。。

查看原文

赞 0 收藏 0 评论 0

BEI_TIAN_XUAN 关注了用户 · 2月20日

薯条 @shutiao_5fd752222e7b7

个人微信公众号:「薯条的自我修养」,欢迎关注

关注 1138

BEI_TIAN_XUAN 关注了用户 · 2月20日

青南 @qingnan

微软最有价值专家(MVP)。已出版图书《Python 爬虫开发,从入门到实战》、《左手 MongoDB,右手 Redis——从入门到商业实战》。独立开发维护开源项目 GNE(获得近2000 Star)。

关注 934

BEI_TIAN_XUAN 关注了用户 · 2月20日

Java技术栈 @javastack

Java技术栈以 Java 后端技术为主,包括 Java 核心技术、多线程编程、Spring Boot、Spring Cloud、缓存、消息队列、架构设计等各种技术干货、Java 面试题、各种学习资料等。微信公众号:Java技术栈(id:javastack),搜索关注吧!

关注 9392

认证与成就

  • 获得 1 次点赞
  • 获得 4 枚徽章 获得 0 枚金徽章, 获得 0 枚银徽章, 获得 4 枚铜徽章

擅长技能
编辑

(゚∀゚ )
暂时没有

开源项目 & 著作
编辑

(゚∀゚ )
暂时没有

注册于 2月20日
个人主页被 86 人浏览