Home > Back-end >  Data structure in the queue
Data structure in the queue

Time:11-19

Why the program runs to the Insert () function of Q - & gt; Rear - & gt; Next=node; This line will be an error, is roughly said next not storing the addresses of the node
#include
#include
Typedef struct QueueNode
{
The int Data;
QueueNode * next;
} QueueNode;

Typedef struct QueueLink
{
QueueNode * front;
QueueNode * rear;
int length;
} QueueLink;

Void Init (QueueLink * Q);
Void Insert (QueueLink * Q);
Void the Delete (QueueLink * Q);
Void Print (QueueLink * Q);

Int main ()
{
QueueLink q;
int i;
Printf (" 1. Initialization \ n ");
Printf (" 2. The team \ n ");
Printf (" 3. The team \ n ");
Printf (" 4. Output \ n ");
Printf (" please enter the option: ");
While (the scanf (" % d ", & amp; I)==1)
{
The switch (I)
{
Case 1: Init (& amp; Q);
printf("\n");
break;

Case 2: Insert (& amp; Q);
printf("\n");
break;

Case 3: Delete (& amp; Q);
printf("\n");
break;

Case 4: Print (& amp; Q);
printf("\n");
break;

Default: printf (" the free option!!!!!! \n");
break;
}
Printf (" 1. Initialization \ n ");
Printf (" 2. The team \ n ");
Printf (" 3. The team \ n ");
Printf (" 4. Output \ n ");
Printf (" please enter the option: ");
}
getchar();
getchar();
return 0;
}

Void Init (QueueLink * Q)
{
QueueNode * head;
Head=(QueueNode *) malloc (sizeof (QueueNode));//head to head node, the most at the beginning of the front and rear are pointing to it
Q=(QueueLink *) malloc (sizeof (QueueLink));
Q - & gt; The front=Q - & gt; Rear=head;
Q - & gt; Length=0;
Printf (" initialization success!!!!!! \n");
}

Void Insert (QueueLink * Q)
{
int data;
QueueNode * node;
The node=(QueueNode *) malloc (sizeof (QueueNode));
Printf (" please to nodes in the input data: ");
The scanf (" % d ", & amp; data);
Node - & gt; Data=https://bbs.csdn.net/topics/data;
Node - & gt; Next=NULL;
Q - & gt; Rear - & gt; Next=node;
Q - & gt; Rear=node;
//Q - & gt; Length++;
Printf (" team success!!!!!! \n");
}

Void the Delete (QueueLink * Q)
{
int data;
QueueNode * p;
If (Q - & gt; The front==Q - & gt; Rear)
{
Printf (" the queue is empty, no team element!!! \n");
}
The else
{
P=Q - & gt; The front - & gt; Next;//will be "to the team elements" address temporary exist p
Q - & gt; The front - & gt; Next=p - & gt; Next;//front next store domain "element to the team" the address of the next element

If ((Q - & gt; Rear)==p)//if the rear to find elements, states that "be delete elements" is the last element, point at this time you should let the rear head node
{
Q - & gt; The front=Q - & gt; The rear;
}
Free (p);
}
}

Void Print (QueueLink * Q)
{
If (Q - & gt; The front==Q - & gt; Rear)
{
Printf (" the queue is empty, no team element!!! \n");
}
The else
{
QueueNode * p=Q - & gt; The front - & gt; Next;
While (p!=NULL)
{
Printf (" % d ", p - & gt; The Data);
P=p - & gt; Next;
}
printf("\n");
}
}

CodePudding user response:

Thank invited,

First you need to clear, the member selection operator - & gt; Requires two operands, left operand must be a point to structure, joint type of pointer; The name of the right operand must be a member, the member selection operator - & gt; To get the specified member, and it is an lvalue,

Come to see you again the expression of the Q - & gt; Rear - & gt; Next=node

In this case, Q type is a pointer to the structure type QueueLink, so Q - & gt; The rear get Q points to the structure member variables rear,

Similarly, the Q - & gt; Rear - & gt; Next is the Q - & gt; The rear points to the structure member variables next,

Member variable type is a pointer to QueueNode next, the node type is a pointer to the QueueNode, assignment is allowed,

Therefore, the problem is not in the Q - & gt; Rear - & gt; Next=node, but because of who I am variables q rear member was not found,

CodePudding user response:

Add, because of the rear member variables q points to the structure of the variable does not exist,

CodePudding user response:

 # include & lt; Stdio. H> 
#include

Typedef struct QueueNode
{
The int Data;
QueueNode * next;
} QueueNode;

Typedef struct QueueLink
{
QueueNode * front;
QueueNode * rear;
int length;
} QueueLink;

Void Init (QueueLink * Q);
Void Insert (QueueLink * Q);
Void the Delete (QueueLink * Q);
Void Print (QueueLink * Q);

Int main ()
{
QueueLink q;
int i;
Printf (" 1. Initialization \ n ");
Printf (" 2. The team \ n ");
Printf (" 3. The team \ n ");
Printf (" 4. Output \ n ");
Printf (" please enter the option: ");
While (the scanf (" % d ", & amp; I)==1)
{
The switch (I)
{
Case 1: Init (& amp; Q);
printf("\n");
break;

Case 2: Insert (& amp; Q);
printf("\n");
break;

Case 3: Delete (& amp; Q);
printf("\n");
break;

Case 4: Print (& amp; Q);
printf("\n");
break;

Default: printf (" the free option!!!!!! \n");
break;
}
Printf (" 1. Initialization \ n ");
Printf (" 2. The team \ n ");
Printf (" 3. The team \ n ");
Printf (" 4. Output \ n ");
Printf (" please enter the option: ");
}
getchar();
getchar();
return 0;
}

Void Init (QueueLink * Q)
{
QueueNode * head;
Head=(QueueNode *) malloc (sizeof (QueueNode));//head to head node, the most at the beginning of the front and rear are pointing to it
if (! The head)
return;
//Q=(QueueLink *) malloc (sizeof (QueueLink));
Q - & gt; The front=Q - & gt; Rear=head;
Q - & gt; Length=0;
Printf (" initialization success!!!!!! \n");
}

Void Insert (QueueLink * Q)
{
int data;
QueueNode * node;
The node=(QueueNode *) malloc (sizeof (QueueNode));
if (! The node)
return;
Printf (" please to nodes in the input data: ");
The scanf (" % d ", & amp; data); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related