队列
队列是一种特殊的线性表,它只允许在表的前端(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;
}