数据库结构(C语言版)

创建一个线性表LA顺序结构,并构造该顺序表,其元素与一维数组a[]={16,6,2,9,11,8,1}元素相同。

img

参考:

#include <stdio.h>
#include <stdlib.h>
typedef int elementtype;
//定义链队列结点类型
typedef struct node {
    elementtype data;
    struct node* next;
} Qnode;
//定义链队列类型
typedef struct {
    Qnode *front, *rear;
} Linkqueue;
//初始化链队列
void init_linkqueue(Linkqueue** Q)
{
    (*Q) = (Linkqueue*)malloc(sizeof(Linkqueue));
    (*Q)->front = (Qnode*)malloc(sizeof(Qnode));
    (*Q)->rear = (Qnode*)malloc(sizeof(Qnode));
    (*Q)->front->next = (*Q)->rear->next = NULL;
}
//入队列
void in_linkqueue(Linkqueue** Q, elementtype x)
{
    Qnode* s = (Qnode*)malloc(sizeof(Qnode));
    s->data = x;
    s->next = NULL;
    //如果当前队列是空队列,第一个元素入队列时需要修改头指针
    if((*Q)->front->next == NULL)
        (*Q)->front->next = s;
    //将算法补充完成,实现入队
    (*Q)->rear->next = s;
    (*Q)->rear = s;
}
//出队列
void out_linkqueue(Linkqueue** Q, elementtype* x)
{
    Qnode* p;
    if((*Q)->front->next == NULL)
        printf("对列已空,没有元素可以出队列!\n");
    //将算法补充完成,实现入队
    p = (*Q)->front;
    p->data = *x;
    (*Q)->front = p->next;
    if((*Q)->rear == p)
        (*Q)->rear = NULL;
}
//输出队列中的元素
void print_linkqueue(Linkqueue* Q)
{
    Qnode* p = Q->front->next;
    if(Q->front->next == NULL)
        printf("此对列为空,无元素可以输出!\n");
    else {
        while(p != NULL) {
            printf("%d ", p->data);
            p = p->next;
        }
    }
}
int main()
{
    Linkqueue* q;
    int i;
    elementtype a[]={16,6,2,9,11,8,1};
    init_linkqueue(&q); //调用初始化函数
    for(i=0;i<(int)(sizeof(a)/sizeof(a[0]));i++)
        in_linkqueue(&q,a[i]); //初始化数据
    print_linkqueue(q); //调用输出函数
    printf("\n");
}