数据结构链表下

来源:互联网 发布:如何制作淘宝宝贝长图 编辑:程序博客网 时间:2024/06/06 02:44

如何用链表实现队列:

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


#define OK 1
#define ERROR 0
typedef int data_t;


typedef struct node_t//普通单链表节点
{
data_t data;
struct node_t *next;


} linknode_t, *linklist_t;


typedef struct//链式队列的头结点
{
linklist_t front, rear;
} linkqueue_t;


linkqueue_t *CreateEmptyLinkqueue()//创建空队列
{
linkqueue_t *queue;


queue = (linkqueue_t *)malloc(sizeof(linkqueue_t));
if (NULL == queue)
{
perror("Create Empty LinkQueue Error");
return NULL;
}
queue->rear = queue->front = NULL;


return queue;
}


int ClearLinkqueue(linkqueue_t *queue)//清空队列
{
linknode_t *node_remove;


node_remove = queue->front;
while (NULL != node_remove)
{
queue->front = queue->front->next;
free (node_remove);
node_remove = queue->front;
}
queue->front = NULL;
queue->rear = NULL;
return OK;
}


int DestroyLinkqueue(linkqueue_t *queue)//销毁队列
{
if (queue)
{
ClearLinkqueue(queue);
free(queue);
return OK;
}
else
{
printf("DestroyLinkqueue Error\n");
return ERROR;
}
}
int EmptyLinkqueue(linkqueue_t *queue)//判定队列是否为空
{
if (!queue)
{
printf("EmptyLinkqueue Error\n");
return -1;
}
return queue->front == NULL ? OK : ERROR;
}


int EnQueue(linkqueue_t *queue, data_t x)//入队
{
linknode_t *node_new;


if (!queue)
{
printf("EnQueue Error\n");
return ERROR;
}
node_new = (linknode_t *)malloc(sizeof(linknode_t));
node_new->data = x;
node_new->next = NULL;


if(EmptyLinkqueue(queue)==OK)
{
queue->front = queue->rear = node_new;
}
else
{
queue->rear->next = node_new;
queue->rear = node_new;
}


return OK;
}


int DeQueue(linkqueue_t *queue, data_t *x)//出队
{
linknode_t *node_remove;


if(!queue)
{
printf("DeQueue Error\n");
return ERROR;
}
if(EmptyLinkqueue(queue)==OK)
    {
        printf("queue is Empty\n");
        return ERROR;
    }
node_remove = queue->front;


queue->front = node_remove->next;


if (NULL == queue->front)
queue->rear = NULL;
*x = node_remove->data;


free(node_remove);
return OK;
}


int VisitQueue(linkqueue_t *queue)//遍历队列
{
linknode_t *node;


printf("aueue = {");


node = queue->front;
if (NULL == node) {
printf("}\n");
return OK;
}
while (NULL != node) {
printf("%d,", node->data);
node = node->next;
}
printf("\b}\n");


return OK;
}
int main()//主函数
{
/*
linkqueue_t *queue = (linkqueue_t*)malloc(sizeof(linkqueue_t));
data_t data;
int i;
EnQueue(queue,20);
EnQueue(queue,30);
DeQueue(queue,&data);
printf("data is %d\n",data);
DeQueue(queue,&data);
printf("data is %d\n",data);
DeQueue(queue,&data);
printf("data is %d\n",data);
for(i=0;i<20;i++)
{
EnQueue(queue,i);
}
VisitQueue(queue);
for(i=0;i<25;i++)//应打印出5个Error
{
DeQueue(queue,&data);
printf("data is %d\n",data);
}
if(DestroyLinkqueue(queue)==OK)
{
printf("DestroyLinkqueue success\n");
}
return 0;
*/
}


如何用链表实现栈:

栈的特点先进后出,因此我们选用链表的头插法,即可实现链式栈:


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


#define OK 1
#define ERROR 0
typedef int data_t;
typedef struct StackNode
{
data_t data;
struct StackNode *next;
}LinkStack;
LinkStack *CreateEmptyStack()//创建栈
{
LinkStack *s;
s = (LinkStack*)malloc(sizeof(LinkStack));
if(s==NULL)
{
perror("Create Empty Stack Error");
exit(0);
}
s->next=NULL;
return s;
}
int ClearLinkStack(LinkStack *stack)//清空栈
{
if(!stack)
return ERROR;
LinkStack *node;
while(stack->next!=NULL)
{
node=stack->next;
stack->next=node->next;
free(node);
}
return OK;
}
int DestroyLinkStack(LinkStack *stack)//销毁栈
{
if(stack)
ClearLinkStack(stack);
free(stack);
stack=NULL;
return OK;
}
int EmptyLinkStack(LinkStack *stack)//判断栈是否为空
{
if(stack==NULL)
{
printf("Empty Error\n");
return -1;
}
else
{
return NULL==stack->next?OK:ERROR;
}
}
int PushStack(LinkStack *s,data_t e)//压栈
{
if(s==NULL)
{
printf("PushStack Error\n");
return ERROR;
}
LinkStack *p=(LinkStack*)malloc(sizeof(LinkStack));
p->data=e;
p->next=s->next;
s->next=p;
return OK;
}
int PopStack(LinkStack *s,data_t *e)//弹栈
{
LinkStack *p;
if(s==NULL)
{
printf("PopStack Error\n");
return ERROR;
}
if(EmptyLinkStack(s)==OK)
{
printf("The Stack is Empty\n");
return ERROR;
}
*e=s->next->data;
p=s->next;
s->next=s->next->next;
free(p);
return OK;
}


int main()
{
/*
data_t data;
LinkStack *stack = CreateEmptyStack();
PushStack(stack,20);
PushStack(stack,30);
PopStack(stack,&data);
printf("Pop:%d\n",data);
PopStack(stack,&data);
printf("Pop:%d\n",data);
PopStack(stack,&data);
printf("Pop:%d\n",data);
PushStack(stack,20);
PushStack(stack,30);
if(DestroyLinkStack(stack)==OK)
{
printf("Desroy LinkStack Success\n");
}
return 0;
*/
}