我尝试使用链表实现队列数据结构。 在函数 Enqueue 中,我需要返回前指针和后指针的值,并且由于函数不能返回两个值,因此我使用了指向指针的指针:前指针和后指针。 运算符“->”似乎不起作用,我收到以下错误代码:
main.c:67:18: error: ‘*rear’ is a pointer; did you mean to use ‘->’?
*rear->next=temp;
^~
->
main.c: In function ‘dequeue’:
main.c:78:14: error: ‘*front’ is a pointer; did you mean to use ‘->’?
x=*front->data;
^~
->
main.c:80:19: error: ‘*front’ is a pointer; did you mean to use ‘->’?
*front=*front->next;//correctly moving front pointer
^~
->
任何帮助将不胜感激。
//implementing queue
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node* next;
} *node;
//declaring functions
void createqueue();
void enqueue(node *front,node * rear, int x);
void dequeue(node *front, node * rear);
void display(node front);
void main(){
node f=NULL;//pointer to a node
node r=NULL;//pointer to a node
char cont;
cont='y';
do{
int x;
int ch;
printf("\n\nMain Menu:\n\n1.Create a Queue\n2.Enqueue an element onto the queue\n"
"3.Dequeue an element of the queue\n4.Display the queue\n5.Quit\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case(1):
createqueue();
break;
case(2):
printf("Enter element : ");
scanf("%d",&x);
enqueue(&f,&r,x);
break;
case(3)://
dequeue(&f, &r);
break;
case(4):
display(f);
break;
case(5)://Quit
exit(0);
}
printf("\nEnter 'y' or 'Y' to continue : ");
scanf(" %c",&cont);
}while(cont=='y'||cont=='Y');
}
void createqueue(){
printf("Queue created!\n");
}
//the return values cannot be 2 in no. so we will have a pointer to the rear and front variables which are themselves pointers
void enqueue(node *front,node *rear,int x){//and not Enqueue(Node * front,Node * rear, int x){
node temp=malloc(sizeof(struct node));
if(temp!=NULL){
temp->data=x;
temp->next=NULL;
//if both front and rear are null then both will change//temp and rear=null case:
if (*front==NULL && *rear==NULL)
*front=*rear=temp;//if there was only one element, the above case will happen for adding that first element and from the second element on, the else conditional would hold true
//not empty queue case
else{
*rear->next=temp;
*rear=temp;
}
}
else
printf("Unable to allocate memory for the node\n");
}
void dequeue(node *front,node *rear){
int x;
node t;
if(*front!=NULL){//list not empty
x=*front->data;
t=*front;//t stores the address of the first node for freeing up later
*front=*front->next;//correctly moving front pointer
free(t);//free(address) is the syntax. free(**front) is not same as free(address of node) rather it is free(node) itself.
if(*front==NULL)//if now after removing one element the list gets empty, then
*rear=NULL;//otherwise rear will still be pointing to the address just freed
printf("%d dequeued\n",x);
}
else
printf("The list is empty!");
}
void display(node front){
node t=front;
int count=1;
if(front!=NULL){
while(t!=NULL){
printf("%d. %d\n",count++,t->data);
t=t->next;
}
}
else
printf("List is empty!");
}
答案 0 :(得分:1)
这是运算符优先级的问题:
*rear->next=temp
表示 *(rear->next)=temp
但你需要的是:
(*rear)->next=temp
所以你必须明确使用括号。
答案 1 :(得分:0)
这是初学者犯的一个非常微妙的错误。 *rear->next=temp 是错误的用法 (*后)->下一个=温度;是正确的用法。 其他作业也是如此。 这遵循运算符的优先级。
答案 2 :(得分:-1)
自从我使用 C++ 以来已经有 20 年了,但如果我看到你的错误,我会尝试
*front = front->next
因为看起来您想将传入指针的值更改为列表中的下一项。
答案 3 :(得分:-1)
也许你的函数声明应该看起来更像
void Function(node *front, node *rear);
有时修复语法中的小错误可以消除许多其他错误。