Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        3年前关闭。
                                                                                            
                
        
我在C上有此队列代码。它可能表示一个FIFO,但我不知道如何为每个新的不同条目插入“密码”,例如标识符。有人有什么主意吗?

有我的代码:

#define true 1
#define false 0

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

struct queue_point
{
int element;
struct queue_point* n;
};

struct queue_point *f_ptr = NULL;

int is_que(void);
void add_ele(int);
int remove_ele(void);
void show_ele();

int main(void)
{
int ele,choice,j;

printf("1 To insert an element");
printf("\n2 To remove an element");
printf("\n3 To display all the elements");
printf("\n4 Exit\n");
while(1)
{
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
{
  printf("\nElement to be inserted: ");
  scanf("%d",&ele);
  add_ele(ele);
  break;
}

case 2:
{
  if(!is_que())
  {
    j=remove_ele();
    printf("\n%d is removed from the queue",j);
    printf("\n");
  }
  else
  {
    printf("\nQueue is Empty.\n");
  }
break;
}

case 3:
show_ele();
break;

case 4:
exit(1);
break;

default:
printf("\nInvalid choice.\n");
break;
}

}

}

/* Function to check if the queue is empty*/
int is_que(void)
{
if(f_ptr==NULL)
return true;
return false;
}

/* Function to add an element to the queue*/
void add_ele(int element)
{
/*dynamically allocate the memory*/
struct queue_point *queue = (struct queue_point*)malloc(sizeof(struct queue_point));

queue->element = element;
queue->n = NULL;
if(f_ptr==NULL)
{
f_ptr = queue;
}
else
{
  struct queue_point* ptr;
  ptr = f_ptr;
  for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
  ptr->n = queue;
}
}

/* Function to remove an element from the queue*/
int remove_ele()
{
struct queue_point* queue=NULL;
if(is_que()==false)
{
int j = f_ptr->element;
queue=f_ptr;
f_ptr = f_ptr->n;

/*If you have allocated a memory block with the functions malloc(), calloc()
or realloc() then you need to free the previously allocated memory.*/

free (queue);
return j;
}
else
{
printf("\nQueue is empty.\n");
return -9999;
}
}

/* Function to display the queue*/
void show_ele()
{
struct queue_point *ptr=NULL;
ptr=f_ptr;
if(is_que())
{
printf("\nQUEUE is Empty.\n");
return;
}
else
{
printf("\nElements present in Queue are: ");
while(ptr!=NULL)
{
printf("%d\t",ptr->element);
ptr=ptr->n;
}
printf("\n");
}
}

最佳答案

您可以像这样修改您的结构:



struct queue_point
{
  int element;
  int identifier;
  struct queue_point* n;
};


如果您希望使用字符串作为标识符,则可以编写char identifer [SIZE_OF_YOUR_CHOICE]。

关于c - 如何给C中的FIFO队列中的元素提供标识? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36109154/

10-16 04:54
查看更多