队列的结构体,插入及删除(绝对正确)

#include 
 
  
using namespace std;
 
  
typedef struct student
{
    int data;
    struct student *next;
}node;
 
  
typedef struct linkqueue
{
    node *first,*rear;
}queue;
 
  
queue *insert(queue *HQ, int x)
{
    node *s;
    s = (node*)malloc(sizeof(node));
    s->data = x;
    s->next = NULL;
 
  
    if(HQ->rear == NULL)
    {
        HQ->first = s;
        HQ->rear = s;
    }
    else
    {
        HQ->rear->next = s;
        HQ->rear = s;
    }
    return(HQ);
}
 
  
//队列出列
queue *del(queue *HQ)
{
    node *p; int x;
    if(HQ->first == NULL)
    {
        printf("\n yichu");
    }
    else
    {
        x = HQ->first->data;
        p = HQ->first;
 
  
        if(HQ->first == HQ->rear)
        {
            HQ->first = NULL;
            HQ->rear = NULL;
        }
        else
        {
            HQ->first = HQ->first->next;
            free(p);
        }
        return HQ;
 
  
    }
}
 
  
int main()
{
    queue *temp;
    temp = insert(temp,22);
    temp = insert(temp,23);
    temp = insert(temp,23);
    temp = insert(temp,23);
    temp = insert(temp,23);
    temp = insert(temp,23);
    node *tempNode;
    tempNode = temp->first;
    while(tempNode!= temp->rear)
    {
        printf("%d ",tempNode->data);
        tempNode = tempNode->next;
    }
 
  
    printf("\n");
    temp = del(temp);
    tempNode = temp->first;
    while(tempNode != temp->rear)
    {
        printf("%d ",tempNode->data);
        tempNode = tempNode->next;
    }
    printf("\n");
    cout << "Hello World!" << endl;
    return 0;
}

你可能感兴趣的:(C++)