数据结构(三)——双链表、链式栈、链式队列 及实现

一、双链表

在单链表的基础上再增加一个指向它前驱的指针,就构成了双链表。

所以双链表有三个变量:数据信息info、前驱指针llink、后继指针rlink。

 

二、双链表操作和实现

   由于双链表也为单链表的一种变型,一些相似的操作就没一一列举,可以参考数据结构(四)——单链表 、带头结点的单链表、循环链表 及其实现

 

1、数据结构

2、在i位置插入结点

3、在y元素后插入结点

4、删除值为x的结点

1、数据结构

[cpp]  view plain ?
  1. typedef int datatype;  
  2.   
  3. typedef struct dlink_node{  
  4.     datatype info;  
  5.     dlink_node* llink;  
  6.     dlink_node* rlink;  
  7. }dnode;  


2、在i位置插入结点

[cpp]  view plain ?
  1. include"linklist.h"  
  2.   
  3. node* insert_link_list_index(node *head,int index,datatype x){  
  4.     if(index<0){  
  5.         printf("index error\n");  
  6.         exit(1);  
  7.     }  
  8.     if(index == 0){         //在链表首插入  
  9.         node *q = (node*) malloc(sizeof(node));  
  10.         q->info = x;  
  11.         if(!head){          //空表的情况  
  12.             q->llink = NULL;  
  13.             q->rlink = NULL;  
  14.             head = q;  
  15.             return head;  
  16.         }  
  17.         head->llink = q;    //非空链表首插入  
  18.         q->rlink = head;  
  19.         q->llink = NULL;  
  20.         head = q;  
  21.         return head;  
  22.     }  
  23.     else{  
  24.         node *ptr = find_node(head,index-1);  
  25.         node* q = (node*)malloc(sizeof(node));  
  26.         q->info = x;  
  27.         if(ptr->next){      //链表中插入    
  28.             p->rlink = ptr->rlink;  
  29.             ptr->rlink->llink = p;  
  30.             ptr->rlink = p;  
  31.             p->llink = ptr  
  32.             return head;  
  33.         }  
  34.         ptr->rlink = p;      //链表尾插入  
  35.         p->llink = ptr;  
  36.         p->rlink = NULL;  
  37.         return head;  
  38.     }  
  39. }  

3、在y元素之后插入

[cpp]  view plain ?
  1. #include"linklist.h"  
  2.   
  3. node* intsert_node_yx(node *head,datatype x,datatype y){  
  4.     node *q=find_node(head,y);  
  5.     if(!q){  
  6.         printf("not found the node %d\n");  
  7.         return head;  
  8.     }  
  9.     node *p = (node*)malloc(sizeof(node));  
  10.     p->info = x;  
  11.     if(!q->rlink){            //最后一个结点  
  12.         q->rlink = p;  
  13.         p->llink = q;  
  14.         p->rlink = NULL;  
  15.         return head;  
  16.     }  
  17.     p->rlink = q->rlink;     //中间结点   
  18.     q->rlink->llink = p;  
  19.     q->rlink = p;  
  20.     p->llink = q;  
  21.     return head;  
  22. }  


4、删除值为x的结点

[cpp]  view plain ?
  1. #include"linklist.h"  
  2.   
  3. node* del_link_list_node(node* head,datatype x){  
  4.     if(!head){  
  5.         printf("the list is empty\n");  
  6.         return head;  
  7.     }  
  8.     node* ptr=head;  
  9.     while(!ptr && ptr->info != x){  
  10.         ptr=ptr->rlink;  
  11.     }  
  12.     if(!ptr){            //未找到数据  
  13.         printf("no data\n");  
  14.     }else if(ptr == head && ptr->rlink){  //第一个就是,还有后继  
  15.         head=ptr->rlink;                 
  16.         ptr->rlink->llink = NULL;     //因为这步所以要判断是否有后继  
  17.     }else if(ptr == head && !ptr->rlink){ //第一个就是,只有一个元素  
  18.         head = NULL;  
  19.     }   
  20.     }else if(!ptr->rlink){          //链表的最后一个  
  21.         ptr->llink->rlink = NULL;  
  22.     }else{  
  23.         ptr->llink->rlink = ptr->rlink;   //链表中间  
  24.         ptr->rlink->llink = ptr->llink;  
  25.     }  
  26.     free(ptr);  
  27.     return head;  
  28. }  

三、链式栈

    栈的链式存储称为链式栈。它的插入和删除规定在单链表的同一端进行,栈顶指针用top表示。

1、输出各个结点的值

2、取得栈顶元素

3、入栈

4、出栈

 

1、输出各个结点的值

[cpp]  view plain ?
  1. #include"linklist.h"  
  2.   
  3. void display_link_list(node *stack){  
  4.     if(!stack){  
  5.         printf("the list is empty!\n");  
  6.     }else{  
  7.         int i;  
  8.         node *ptr = stack;  
  9.         while(ptr){  
  10.             printf("5%d",ptr->info);  
  11.             ptr = ptr->next;  
  12.         }  
  13.     }  
  14. }  


2、取得栈顶元素

[cpp]  view plain ?
  1. #include"linklist.h"  
  2.   
  3. node* get_top(node *stack){  
  4.     if(!stack){  
  5.         printf("the stack is empty\n");  
  6.         return stack;  
  7.     }  
  8.     return stack->info;  
  9. }  


3、入栈

[cpp]  view plain ?
  1. #include"linklist.h"  
  2.   
  3. node* push(node* stack,datatype x){  
  4.     node *p = (node*)malloc(sizeof(node));  
  5.     p->info = x;  
  6.     p->next = stack;  //当stack为空时,则有p->next = NULL  
  7.     stack = p;  
  8.     return p;  
  9. }  

4、出栈

[cpp]  view plain ?
  1. #include"linklist.h"  
  2.   
  3. node* pop(node *stack,datatype *x){  
  4.     if(!stack){  
  5.         printf("the stack is empty\n");  
  6.         return stack;  
  7.     }  
  8.     node *p = stack;  
  9.     stack=stack->next;  
  10.     *x = p->info;  
  11.     free(p);  
  12.     return stack;  
  13. }  


四、链式队列

  以队列形式存储的链式队列,插入和删除在单链表的不同端进行,队首和队尾指针存在一个数据结构中。

 

1、结构定义

2、建立一个空队列

3、判断是否为空

4、取得首节点值

5、输出各个结点

6、入列

7、出列

1、结构定义

[cpp]  view plain ?
  1. typedef int datatype;  
  2.   
  3. typedef struct link_queue_node{  
  4.     datatype info;  
  5.     struct link_queue_node* next;  
  6. }node;  
  7.   
  8. typedef struct queue{  
  9.     node* front;  
  10.     node* rear;  
  11. }queue;  


2、建立一个空队列

[cpp]  view plain ?
  1. #include"linkqueue.h"  
  2.   
  3. queue* init_link_queue(){  
  4.     queue *q = (queue*) malloc(sizeof(queue));  
  5.     q->front = NULL;  
  6.     q->rear = NULL;  
  7.     return q;  
  8. }  


3、判断是否为空

[cpp]  view plain ?
  1. #include"linkqueue.h"  
  2.   
  3. int is_empty_link_queue(queue *q){  
  4.     return q->front? 0:1;  
  5. }  

4、取得首节点值

[cpp]  view plain ?
  1. #include"linkqueue.h"  
  2.   
  3. datatype* get_head(queue *q){  
  4.     if(!q){  
  5.         printf("the queue is empty\n");  
  6.         exit(1);  
  7.     }  
  8.     return q->front->info;  
  9. }  


5、输出各个结点

[cpp]  view plain ?
  1. #include"linkqueue.h"  
  2.   
  3. void display_link_queue(queue *q){  
  4.     if(!q){       
  5.         printf("the queue is empty!\n");          
  6.     }else{  
  7.         node *p = q->front;  
  8.         while(p){  
  9.             printf("%5d",p->info);  
  10.             p=p->next;  
  11.         }  
  12.     }  
  13. }  

6、入列

[cpp]  view plain ?
  1. #include"linkqueue.h"  
  2.   
  3. queue* en_list_queue(queue *q,datatype x){  
  4.     node* p = (node*)malloc(sizeof(node));  
  5.     p->info = x;  
  6.     p->next = NULL;  
  7.     if(!q->front){  
  8.         q->front = p;  
  9.         q->rear = p;  
  10.     }else{  
  11.         rear->next = p;  
  12.         rear = p;  
  13.     }  
  14.     return q;  
  15. }  


7、出列

[cpp]  view plain ?
  1. #include"linkqueue.h"  
  2.   
  3. queue* del_link_queue(queue *q,datatype *x){  
  4.     if(!q->front){  
  5.         printf("the queue is empty\n");  
  6.         return q;  
  7.     }  
  8.     node *p = q->front;  
  9.     if(q->front == q->rear){  
  10.         q->front=q->rear=NULL;  
  11.     }else{  
  12.         q->front = p->next;  
  13.     }  
  14.     *x = p->info;  
  15.     free(p);  
  16.     return q;  
  17. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值