doublueList.h
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
typedef int LTDataType;
typedef struct DList
{
LTDataType data;
struct DList *next;
struct DList *prev;
}LTNode;
LTNode* init();//创建哨兵位 (自定义头节点)
LTNode* createNode(LTDataType x);//创建新节点
void pushFront(LTNode* head,LTDataType x);//头插
void popFront(LTNode* head);
void pushBack(LTNode* head,LTDataType x);//尾插
void popBack(LTNode* head);
void findTarget(LTNode* head,LTDataType x);
void insert(LTNode* pos,LTDataType x);
void delete(LTNode* pos);
int size(LTNode* head);
void destory(LTNode* head);
void printfDouble(LTNode* head);//打印
//队列:先进先出,后进后出
typedef int QDataType;
// (单链表)链式结构:表示队列
typedef struct QueueNode
{
QDataType data;
struct QueueNode *next;
}QueueNode;
//队列
typedef struct Queue
{
QueueNode *head;//队头
QueueNode *tail;//对尾
}Queue;
void initQueue(Queue *que);
bool emptyQueue(Queue *que);
void pushQueue(Queue *que,QDataType data);//入队 -->尾插
void popQueue(Queue *que,QDataType data);//出队 -->头删
QDataType getFront(Queue *que);//获取队列头部元素
QDataType getBack(Queue *que);//获取队列队尾元素
int QueueSize(Queue *que);
void destoryQueue(Queue *que);
doublueList.c
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
#include "doubleList.h"
LTNode* createNode(LTDataType x)
{
LTNode* newCode = (LTNode*)malloc(sizeof(LTNode));
newCode->data = x;
newCode->next = NULL;
newCode->prev = NULL;
return newCode;
}
LTNode* init()
{
LTNode* head = createNode(-1);
head->prev = head;
head->next = head;
return head;
}
//LTNode* pos传进来的是 当前链表的首地址 ,如何你建立了哨兵 那就是哨兵的地址
void insert(LTNode* pos,LTDataType x)
{
assert(pos);
LTNode* newNode = createNode(x);
LTNode* pos_prev = pos->prev;
newNode->prev = pos_prev;
newNode->next = pos;
pos_prev->next = newNode;
pos->prev = newNode;
}
void delete(LTNode* pos)
{
assert(pos);
assert(pos!=pos->next);
pos->next->prev = pos->prev;
pos->prev->next = pos->next;
free(pos);
}
void pushFront(LTNode* head,LTDataType x)
{
insert(head->next,x);
}
void pushBack(LTNode* head,LTDataType x)
{
insert(head,x);
}
void popFront(LTNode* head)
{
delete(head->next);
}
void popBack(LTNode* head)
{
delete(head->prev);
}
int size(LTNode* head)
{
LTNode* curr = head->next;
int size = 0;
while(curr != head)
{
size++;
curr = curr->next;
}
return size;
}
void destory(LTNode* head)
{
LTNode* curr = head->next;
while(curr != head)
{
LTNode* curr_next = curr->next;
free(curr);
curr = curr_next;
}
free(head);
}
void printfDouble(LTNode* head)
{
assert(head);
int x = 0;
while(head)
{
if(x == 2)
{
break;
}
if(head->data == -1)
{
x++;
}
else
{
printf("%d ",head->data);
}
head = head->next;
}
printf("/n");
getchar();
}
/***********************************************/
void initQueue(Queue *que)
{
assert(que);
que->head = que->tail = NULL;
}
bool emptyQueue(Queue *que)
{
assert(que);
return que->head == NULL && que->tail == NULL;
}
void pushQueue(Queue *que,QDataType data)
{
assert(que);
QueueNode *newNode = (QueueNode*)malloc(sizeof(QueueNode));
if(newNode == NULL)
{
}
else
{
newNode->next = NULL;
newNode->data = data;
}
if(que->head == NULL)
{
que->head = que->tail = newNode;
}
else
{
que->tail->next = newNode;
que->tail = que->tail->next;
}
}
void popQueue(Queue *que,QDataType data)
{
assert(que);
assert(emptyQueue(que));
if(que->head->next == NULL)
{
free(que->head);
que->head = que->tail= NULL;
}
else
{
QueueNode* twice = que->head->next;
free(que->head);
que->head = twice;
}
}
QDataType getFront(Queue *que)
{
assert(que);
assert(!emptyQueue(que));
return que->head->data;
}
QDataType getBack(Queue *que)
{
assert(que);
assert(!emptyQueue(que));
return que->tail->data;
}
int QueueSize(Queue *que)
{
assert(que);
QueueNode* curr = que->head;
int size = 0;
while(curr)
{
curr = curr->next;
size++;
}
return size;
}
void destoryQueue(Queue *que)
{
assert(que);
QueueNode* curr = que->head;
while(curr)
{
QueueNode *del = curr;
curr = curr->next;
free(del);
}
que->head = que->tail = NULL;
}
void printfQueue(Queue *que)
{
assert(que);
QueueNode* curr = que->head;
while(curr)
{
printf("%d ",curr->data);
curr = curr->next;
}
}
doublueText.c
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include "doubleList.h"
/*
lomo_t@lomo-unix:~/rubbish_box/list_box$ gcc -o doubleText doubleList.c doubleText.c
lomo_t@lomo-unix:~/rubbish_box/list_box$ ./doubleText
destory --> size : 3
QDataType --> getFront : 2
*/
LTNode* SL = NULL;
Queue* QU = NULL;
void Text()
{
/*SL = init();
insert(SL,1);
insert(SL,3);
insert(SL,5);
pushFront(SL,2);
printfDouble(SL);
popFront(SL);
printfDouble(SL);
printf("size : %d \n",size(SL));
destory(SL);*/
//printf("destory --> size : %d \n",size(SL));
QU = (Queue*)malloc(sizeof(Queue));
initQueue(QU);
pushQueue(QU,2);
pushQueue(QU,4);
pushQueue(QU,6);
QDataType x = getFront(QU);
printf("destory --> size : %d \n",QueueSize(QU));
printf("QDataType --> getFront : %d \n",x);
}
int main()
{
Text();
return 0;
}
标签:gcc,curr,--,void,head,next,LTNode,que,双链
From: https://www.cnblogs.com/kato-T/p/17387987.html