【问题标题】:Implementing simple queue using linked list使用链表实现简单队列
【发布时间】:2013-05-03 23:49:48
【问题描述】:

我正在尝试实施一个队列,让人们排队等候洗手间,但问题是洗手间是供男性和女性使用的,但是当男性在场时,女性无法进入,而当女性在场时,男性无法进入。这是我的问题。 (它是我的一个课程的演示,它没有评分,它几乎没有学术性)

我可以将人插入浴室并插入队列(当一个女性试图进入并且浴室里有一个男人时,她会被添加到队列中)但我不能将人从队列中取出然后插入到队列中当他们有资格进入浴室时。这是我的代码。

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

struct Node
{
    int Data;
    struct Node* next;
}*rear, *front;

void delQueue()
{
    struct Node *temp, *var=rear;
    if(var==rear)
    {
        rear = rear->next;
        free(var);
    }
    else
    printf("\nQueue Empty");
}

void push(int value)
{
    struct Node *temp;
    temp=(struct Node *)malloc(sizeof(struct Node));
    temp->Data=value;
    if (front == NULL)
    {
        front=temp;
        front->next=NULL;
        rear=front;
    }
    else
    {
        front->next=temp;
        front=temp;
        front->next=NULL;
    }
}

void display()
{ 
    struct Node *var=rear;
    if(var!=NULL)
    {
        printf("\nElements in queue are:  ");
        while(var!=NULL)
        {
            printf("\t%d",var->Data);
            var=var->next;
        }
    printf("\n");
    } 
    else
    printf("\nQueue is Empty\n");
}

int main() { 

int man_busy = 0;
int woman_busy = 0;
int input = 0;
int i = 0;

printf("\n(1) Man enters\n");
printf("(2) Woman enters\n");
printf("(3) Man leaves\n");
printf("(4) Woman leaves\n");


    printf("\nEmpty!\n");


    for(i=0; i<20; i++) {

        scanf("%d", &input);

        if(input == 1){
            if(woman_busy > 0){
                printf("Man Can't enter when women are present\n");
                printf("You will have to wait in the queue\n");
                push(input);
                display();
            }   
            else if(woman_busy == 0) {
                man_busy = man_busy + 1;    
                printf("Occupied By Man: %d\n", man_busy);
            }
        }
        else if(input == 2) {
            if(man_busy > 0){
                printf("Woman Can't enter when men are present\n");
                printf("You will have to wait in the queue\n");
                push(input);
                display();
            }   
            else if(man_busy == 0) {
                woman_busy = woman_busy + 1;    
                printf("Occupied By Woman: %d\n", woman_busy);
            }
        }
        else if(input == 3) {
            man_busy = man_busy - 1;
            if (man_busy == 0 && woman_busy == 0){
                printf("Empty!\n");
                delQueue();
                display();
            }
            else if (man_busy < 0) {
                printf("Invalid command!\n");
                man_busy = man_busy + 1;
            }           
            else {
                printf("Occupied By Man: %d\n", man_busy);
            }
        }
        else if(input == 4) {
            woman_busy = woman_busy - 1;
            if (man_busy == 0 && woman_busy == 0) {
                printf("Empty!\n");
                delQueue();
                display();
            }
            else if (woman_busy < 0) {
                printf("Invalid command!\n");
                    woman_busy = woman_busy + 1;
            }
            else {
                printf("Occupied By Woman: %d\n", woman_busy);
            }
        }
    }
        return 0;

}

【问题讨论】:

  • 如果一个男人已经在浴室里,男人会跳到排队等候的女人前面吗?
  • 是的,如果浴室里已经有男人,男人会跳线进入,这两种方式都有效

标签: c linked-list queue


【解决方案1】:

你需要一个例程 dequeue(我推荐函数名 enqueue 和 dequeue,因为 push /pop 命名法用于堆栈)。

当您遇到条件“浴室为空”时,如果队列不为空,则需要将第一个元素类型的所有元素出列(即所有男性或所有女性,具体取决于是男性还是女性)队列)并将它们放在浴室内。当浴室空了时重复此操作。

【讨论】:

  • pop() 和 push() 适用于队列
【解决方案2】:

如果您想从队列中删除人员您我必须砍掉所需的节点并粘贴列表并在需要时再次设置“后”和“前”。为此,您必须跟踪前一个节点。

  • 案例#1:

1&lt;-2&lt;-3-&lt;4 Rear:1 Front:4

我们要砍掉 3,前一个节点是 2。

1&lt;-2&lt;- |3| -&lt;4 Rear:1 Front:4

然后将previous-&gt;next 粘贴到chopped_off-&gt;next

1&lt;-2&lt;-----4 Rear:1 Front:4

此外,如果“后”或“前”指向所需的元素,则不需要粘合任何东西。

  • 案例#2:

1&lt;-2&lt;-3-&lt;4 Rear:1 Front:4

我们要砍掉1,前面没有节点!

|1| &lt;-2&lt;-3-&lt;4 Rear:1 Front:4

重置后方

2&lt;-3-&lt;4 Rear:2 Front:4

浴室可以容纳无限多的人?如果是,您将始终切断 rear 指向的元素。并且不需要保存前一个节点。这将非常容易,因为您必须冲刷整个队列,因为每时每刻都只有男性或女性在队列中,当浴室空无一人时,他们都会走进来。


  • 如果您正在处理某种菜单选择,请不要使用 if-elseif-else。如果有很多选择 你在伤害别人。了解 switch-case 语句。它们是为这样的事情而设计的。这将使您的工作更轻松。
  • 您的变量名称应以小写字母开头。这就是惯例,如果你不遵守它可能会让人感到困惑。
  • 最好在每次操作后深入了解整个队列。
  • 为了调试目的,最好实现一些可以区分队列中不同男性和女性的东西。您想检查是否从队列中删除了正确的人。
  • #include &lt;time.h&gt;?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多