1. 程式人生 > >C 資料結構佇列和棧基本操作

C 資料結構佇列和棧基本操作

佇列

佇列是一種特殊的線性表,它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作。佇列是一種操作受限制的線性表。

與現實中的排隊類似,進行插入操作只能在隊尾,進行刪除操作只能在隊頭。佇列是一種先進先出的線性表。

C實現佇列,需要定義一個結點結構,一個含指向首結點和尾結點指標的結構(比連結串列多一個指向首尾的結構)。 佇列的首指標指向第一個元素,佇列的尾指標指向最後一個元素,都是有資料的。

指標方向由隊首指向隊尾。

//定義佇列中結點結構,同連結串列
typedef struct node{
    int data;
    struct node * next;
}node;

//定義佇列的首尾結點指標結構
typedef struct queue{
    node* head;
    node* rear;
}queue;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//定義佇列中結點結構,同連結串列
typedef struct node{
    int data;
    struct node * next;
}node;

//定義佇列的首尾結點指標結構
typedef struct queue{
    node* head;
    node* rear;
}queue;

//入隊操作(隊尾)
queue* insert_(queue* HQ, int value){
    node* p = (node*)malloc(sizeof(node));
    p->data = value;
    p->next = NULL;
    if(HQ->rear==NULL){
        HQ->rear = p;
        HQ->head = p;
    }
    else{
        HQ->rear->next = p;
        HQ->rear = p;
    }
    return HQ;
}

//出隊操作(隊首)
queue* del_(queue*HQ){
    if(HQ->head==NULL){
        return HQ;
    }
    node* p = HQ->head;
    if(HQ->head==HQ->rear){
        HQ->head =NULL;
        HQ->rear = NULL;
    }
    else{
        HQ->head = HQ->head->next;
        printf("%s %d\n","chu dui ",p->data);
        free(p);
    }
    
    return HQ;
}

//佇列長度
int length_queue(queue* HQ){
    int length = 1;
    if(HQ->head == NULL){
        return 0;
    }
    node* p = HQ->head;
    while(p!=HQ->rear){
        p = p->next;
        length++;
    }
    return length;
}

//列印佇列
void print_qnode(queue* HQ){
    node* p = HQ->head;
    while(p!=HQ->rear){
        printf("%d\n",p->data);
        p=p->next;
    }
    printf("%d\n",p->data);
}

//銷燬佇列
void destory_queue(queue* HQ){
    node * p = HQ->head;
    if(HQ->head!=NULL){
        while(p!=HQ->rear){
            node* p_next= p->next;
            free(p);
            p = p_next;
        }
        free(p);
    }
    free(HQ); 
}

void main(){
    queue * H = (queue*)malloc(sizeof(queue));
    H = insert_(H,3);  //插入
    H = insert_(H,7);  //插入
    print_qnode(H);    //列印  
    printf("%d\n",length_queue(H)); //長度
    H = del_(H);       //出隊
    print_qnode(H);    //列印
    del_queue(H);      //銷燬佇列
    return ;
}


棧是一種特殊的線性表,規定它的插入運算,刪除運算均線上性表的同一端進行,進行插入和刪除的哪一端稱為棧頂,另一端稱為棧底。

棧的實現跟佇列類似,有一個結點結構,一個棧底棧頂結構,指標由棧底指向棧頂。

typedef struct node{
    int data;
    struct node* next;
}node;

typedef struct stack{
    node* top;
    node* buttom;
}stack;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


typedef struct node{
    int data;
    struct node* next;
}node;

typedef struct stack{
    node* top;
    node* buttom;
}stack;


//入棧操作,由棧底指向棧頂,在棧頂出入棧
stack* in_stack(stack* S, int value){
    node* p = (node*)malloc(sizeof(node));
    p->data = value;
    p->next = NULL;
    if(S->buttom==NULL){
        S->buttom = p;
        S->top = p;
        S->top->next = NULL;
    }
    else{
        S->top->next = p;
        S->top=p;
    }
    return S;
}

//出棧操作,從棧底部指標往上移動到棧頂,移除棧頂
stack* out_stack(stack* S){
    node* p = S->top; node* b = S->buttom;
    if(S->buttom==NULL){
        return S;
    }
    if(S->top == S->buttom){
        S->top=S->buttom=NULL;
    }
    else{
        while(b->next!=S->top){
        b = b->next;
        }
        p = b->next;
        printf("%s,%d\n","chu zhan: ",p->data);
        b->next = NULL;
        free(p);
        S->top = b;         
    }    
    return S;    
}

//列印棧
void print_sta(stack* S){
    node* p = S->buttom;
    if(S->buttom==NULL){
        return;
    }
    while(p!=S->top){
        printf("%d\n",p->data);
        p = p->next;
    }
    printf("%d\n",p->data);    
}


void main(){
    stack *Sta = (stack*)malloc(sizeof(stack));
    Sta = in_stack(Sta,3); //入棧
    Sta = in_stack(Sta,5); //入棧
    Sta = in_stack(Sta,9); //入棧
    Sta = in_stack(Sta,13); //入棧
    Sta = out_stack(Sta); //出棧
    print_sta(Sta);  //列印棧
    return;
}