您的位置:首页 > 其它

链表和队列

2016-05-26 13:38 218 查看
单链表

(1) 带头结点:head始终不等于NULL, head->next等于NULL的时候链表为空;

(2) 不带头结点:head等于NULL时,链表为空;

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


双链表

(1) 单/双链表为空时,head->next==NULL;

typedef struct DoubleLinkNode
{
int data;
struct DoubleLinkNode *prior;
struct DoubleLinkNode *next;
};


循环单链表

(1) 带头结点:为空:head==head->next;

(2) 不带头结点:为空:head==NULL;

循环双链表

(1) 带头结点:为空:head->next==NULL&&head->prior==NULL:

(2) 不带头结点:为空:head==NULL;

静态链表:使用数组来实现;

顺序栈

(1) 定义

typedef struct SequenceStack
{
int data[SIZE_MAX];
int top;
};


(2) 栈空判断

int isEmpty(SequenceStack st)
{
if (st.top == -1)
{
return 1;
}
return 0;
}


(3) 进栈

int push(SequenceStack &st, int x)
{
if (st.top == SIZE_MAX-1)
{
return 0;
}
++(st.top);
st.data[st.top] = x;
return 1;
}


(4) 出栈

int pop(SequenceStack &st, int &x)
{
if (st.top == -1)
{
return 0;
}
x = st.data[st.top];
--(st.top);
return 1;
{
}
}


链栈

(1) 定义

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


(2) 栈空

int isEmpty(LinkStack *lst)
{
if (lst->next == null)
{
return 1;
}
return 0;
}


(3) 进栈

void push(LinkStack *lst, int x)
{
LinkStack *p;
p = (LinkStack*)malloc(sizeof(LinkStack));
p->next = NULL;
//头插法
p->data = x;
p->next = lst->next;
lst->next = p;
}


(4) 出栈

int pop(LinkStack *lst, int &x)
{
LinkStack *p;
if (lst->next == NULL)
{
return 0;
}

p = lst->next;
x = p->data;
lst->next = p->next;
free(p);
return 1;
}


顺序队

(1) 定义

typedef struct SequenceQueue
{
int data[SIZE_MAX];
int front;
int rear;
};


(2) 循环队列

队空:queue.rear == queue.front

队满:(queue.rear+1)%max_size == queue.front

(3) 队空

int isEmpty(SequeenceQueue &queue)
{
if (queue.front == queue.rear)
{
return 1;
}
return 0;
}


(4) 入队

int enQueue(SequenceQueue &queue, int x)
{
if ((queue+1)%max_size==queue.front)
{
return 0;
}
queue.rear = (queue.rear + 1) % max_size;
queue.data[queue.rear] = x;
return 1;
}


(5) 出队

int deQueue(SequenceQueue &queue, int &x)
{
if (queue.front == queue.rear)
{
return 0;
}
queue.front = (queue.front + 1) % max_size;
x = queue.data[queue.front];
return 1;
{
}
}


链队

(1) 定义

typedef struct QueueNode
{
int data;
struct QueueNode *next;
};
typedef struct LinkQueue
{
struct LinkQueue *front;
struct LinkQueue *next;
};


(2)队空

int isEmpty(LinkQueue *lqueue)
{
if (lqueue.rear == NULL || lqueue.front == NULL)
{
return 1;
}
return 0;
}


(3) 入队

void enQueue(LinkQueue *lqueue, int x)
{
QueueNode *p;
p = (QueueNode*)malloc(sizeof(QueueNode));
p->data = x;
p->next = NULL;
//队列为空,新结点为队首结点,也是队尾结点
if (lqueue->rear == NULL)
{
lqueue->front = lqueue->rear = p;
}
else
{
lqueue->rear->next = p;
lqueue->rear = p;
}
}


(4) 出队

int deQueue(LinkQueue *lqueue, int &x)
{
QueueNode *p;
if (lqueue->rear==NULL)
{
return 0;
}
else
{
p = lqueue->front;
}
if (lqueue->front == lqueue->rear)
{
lqueue->front = lqueue->rear = NULL;
}
else
{
lqueue->front = lqueue->front->next;
}
x = p->data;
free(p);
return 1;
{
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表 队列