试图限制C中的列表

试图限制C中的列表,c,C,现在我正试图建立一个简单的程序作为我的大学作业,这应该是一个简单的代码,但我现在正在学习c,我在学习它的过程中遇到了一个非常糟糕的时刻,无论如何,这段代码应该能够在控制台上添加,删除,显示和清除一些数字,一个列表(我不知道英语中是否这样称呼),但有一件事我做不到,我一直在研究并试图弄清楚,那就是如何在这个列表上设置一个限制,确切地说是20个数字 #include <stdio.h> #include <stdlib.h> struct node { int nu

现在我正试图建立一个简单的程序作为我的大学作业,这应该是一个简单的代码,但我现在正在学习c,我在学习它的过程中遇到了一个非常糟糕的时刻,无论如何,这段代码应该能够在控制台上添加,删除,显示和清除一些数字,一个列表(我不知道英语中是否这样称呼),但有一件事我做不到,我一直在研究并试图弄清楚,那就是如何在这个列表上设置一个限制,确切地说是20个数字

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int num;
    struct node *prox;
};
typedef struct node Fila;

int t;

int menu(void);
void opcao(Fila *f, int op);
void inicia(Fila *f);
int vazia(Fila *f);
Fila *aloca();
void insere(Fila *f);
Fila *retira(Fila *f);
void exibe(Fila *f);
void libera(Fila *f);
void liberar_mem(Fila *f);

int main(void)
{
    Fila *f = (Fila *)malloc(sizeof(Fila));
    if (!f)
    {
        printf("Sem memoria disponivel!\n");
        exit(1);
    }
    else
    {
        inicia(f);
        int opt;

        do
        {
            opt = menu();
            opcao(f, opt);
        } while (opt);

        free(f);
        return 0;
    }
}

int menu(void)
{
    int opt;

    printf("Escolha a opcao\n");
    printf("0. Sair\n");
    printf("1. Zerar solicitacoes\n");
    printf("2. Exibir solicitacoes\n");
    printf("3. Inserir o numero da solicitacao\n");
    printf("4. Remover solicitacao\n");
    printf("Opcao: ");
    scanf("%d", &opt);

    return opt;
}

void opcao(Fila *f, int op)
{
    Fila *tmp;
    switch (op)
    {
    case 0:
        liberar_mem(f);
        break;

    case 1:
        libera(f);
        inicia(f);
        break;

    case 2:
        exibe(f);
        break;

    case 3:
        insere(f);
        break;

    case 4:
        tmp = retira(f);
        if (tmp != NULL)
        {
            printf("Solicitacao removida: %3d\n\n", tmp->num);
            free(tmp);
        }
        break;

    default:
        printf("Comando invalido\n\n");
    }
}

void inicia(Fila *f)
{
    f->prox = NULL;
    t = 0;
}

int vazia(Fila *f)
{
    if (f->prox == NULL)
        return 1;
    else
        return 0;
}

Fila *aloca()
{
    Fila *novo = (Fila *)malloc(sizeof(Fila));
    if (!novo)
    {
        printf("Sem memoria disponivel!\n");
        exit(1);
    }
    else
    {
        printf("Insira o numero da solicitacao: ");
        scanf("%d", &novo->num);
        return novo;
    }
}

void insere(Fila *f)
{
    Fila *novo = aloca();
    novo->prox = NULL;

    if (vazia(f))
        f->prox = novo;
    else
    {
        Fila *tmp = f->prox;

        while (tmp->prox != NULL)
            tmp = tmp->prox;

        tmp->prox = novo;
    }
    t++;
}

Fila *retira(Fila *f)
{
    if (f->prox == NULL)
    {
        printf("Lista de solicitacoes ja esta vazia\n");
        return NULL;
    }
    else
    {
        Fila *tmp = f->prox;
        f->prox = tmp->prox;
        t--;
        return tmp;
    }
}

void libera(Fila *f)
{
    if (f->prox == NULL)
    {
        printf("Lista de solicitacoes ja esta vazia\n");
        Fila *proxNode, *atual;
        atual = f->prox;

        while (atual != NULL)
        {
            proxNode = atual->prox;
            free(atual);
            atual = proxNode;
        }
    }
    else
    {
        if (!vazia(f))
        {
            Fila *proxNode, *atual;
            atual = f->prox;

            while (atual != NULL)
            {
                proxNode = atual->prox;
                free(atual);
                atual = proxNode;
            }
        }
    }
}

void exibe(Fila *f)
{
    if (vazia(f))
    {
        printf("Nenhuma solicitacao cadastrada!\n\n");
        return;
    }

    Fila *tmp;
    tmp = f->prox;
    printf("Fila :");
    while (tmp != NULL)
    {
        printf("%5d", tmp->num);
        tmp = tmp->prox;
    }
    printf("\n        ");
    int count;
    for (count = 0; count < t; count++)
        printf("  ^  ");
    printf("\nOrdem:");
    for (count = 0; count < t; count++)
        printf("%5d", count + 1);

    printf("\n\n");
}

void liberar_mem(Fila *FILA)
{
    if (!vazia(FILA))
    {
        Fila *proxNode, *atual;

        atual = FILA->prox;
        while (atual != NULL)
        {
            proxNode = atual->prox;
            free(atual);
            atual = proxNode;
        }
    }
}
#包括
#包括
结构节点
{
int-num;
结构节点*prox;
};
typedef结构节点Fila;
int t;
int菜单(无效);
无效操作(Fila*f,内部操作);
无效信息(Fila*f);
瓦齐亚国际酒店(菲拉*f);
菲拉*阿洛卡();
空心镶嵌(Fila*f);
菲拉*视网膜(菲拉*f);
无效退出(Fila*f);
自由无效(Fila*f);
无效自由人(Fila*f);
内部主(空)
{
Fila*f=(Fila*)malloc(sizeof(Fila));
如果(!f)
{
printf(“Sem memoria disponivel!\n”);
出口(1);
}
其他的
{
伊尼西亚(f);
int-opt;
做
{
opt=菜单();
opcao(f,opt);
}while(opt);
免费(f);
返回0;
}
}
int菜单(无效)
{
int-opt;
printf(“Escolha a opcao”);
printf(“0.Sair\n”);
printf(“1.Zerar征求意见书”);
printf(“2.Exibir requestacoes\n”);
printf(“3.请求书编号”);
printf(“4.删除请求”);
printf(“Opcao:”);
scanf(“%d”和&opt);
返回opt;
}
无效操作(Fila*f,内部操作)
{
Fila*tmp;
开关(op)
{
案例0:
自由党成员(f);
打破
案例1:
自由党(f);
伊尼西亚(f);
打破
案例2:
exibe(f);
打破
案例3:
插图(f);
打破
案例4:
tmp=视网膜(f);
如果(tmp!=NULL)
{
printf(“请求删除:%3d\n\n”,tmp->num);
免费(tmp);
}
打破
违约:
printf(“伤残军人协会”);
}
}
无效信息(Fila*f)
{
f->prox=NULL;
t=0;
}
瓦齐亚国际酒店(菲拉*f)
{
如果(f->prox==NULL)
返回1;
其他的
返回0;
}
菲拉*阿洛卡()
{
菲拉*诺沃=(菲拉*)马洛克(西泽夫(菲拉));
如果(!novo)
{
printf(“Sem memoria disponivel!\n”);
出口(1);
}
其他的
{
printf(“请求书编号”);
scanf(“%d”,&novo->num);
返回诺沃;
}
}
空心镶嵌(Fila*f)
{
菲拉*诺沃=阿洛卡();
novo->prox=NULL;
if(瓦齐亚(f))
f->prox=novo;
其他的
{
Fila*tmp=f->prox;
同时(tmp->prox!=NULL)
tmp=tmp->prox;
tmp->prox=novo;
}
t++;
}
菲拉*视网膜(菲拉*f)
{
如果(f->prox==NULL)
{
printf(“请求权清单”;
返回NULL;
}
其他的
{
Fila*tmp=f->prox;
f->prox=tmp->prox;
t--;
返回tmp;
}
}
自由无效(Fila*f)
{
如果(f->prox==NULL)
{
printf(“请求权清单”;
Fila*proxNode,*atual;
atual=f->prox;
while(atual!=NULL)
{
proxNode=atual->prox;
免费(免费);
atual=proxNode;
}
}
其他的
{
如果(!vazia(f))
{
Fila*proxNode,*atual;
atual=f->prox;
while(atual!=NULL)
{
proxNode=atual->prox;
免费(免费);
atual=proxNode;
}
}
}
}
无效退出(Fila*f)
{
if(瓦齐亚(f))
{
printf(“Nenhuma requestacao cadastrada!\n\n”);
返回;
}
Fila*tmp;
tmp=f->prox;
printf(“Fila:”);
while(tmp!=NULL)
{
printf(“%5d”,tmp->num);
tmp=tmp->prox;
}
printf(“\n”);
整数计数;
用于(计数=0;计数prox;
while(atual!=NULL)
{
proxNode=atual->prox;
免费(免费);
atual=proxNode;
}
}
}

您可以使用全局变量跟踪列表的当前长度,并在有人试图将列表扩展到超出限制时返回错误消息

然而,虽然全局变量对于简单的程序来说很好,但对于大型程序来说,尽可能少地依赖它们被认为是一种好的方式

因此,我建议使用“列表头”对象。例如

#define MAX_LIST_LENGTH 20

struct list_header {
    Fila *start_of_list;
    unsigned int current_length;
};

“在此列表中设置一个限制,正好是20个数字”。难道你不能只设置一个计数器,在添加时递增,在删除时递减吗?如果达到限制,则添加将返回“对不起,已满”?对不起,但是我应该如何在我的代码上使用这个列表头?我很困惑应该把它放在哪里,以及如何使用它,如果可能的话,你能帮助我吗?当然,没问题。你的所有函数都在上面修改
Fila
对象,你应该给它们传递一个指向
结构列表头的指针。看看这个例子: