[数据结构]对链表、栈、队列的总结(通俗易懂,很好追风)

[数据结构]对链表、栈、队列的总结http://www.cnblogs.com/racaljk/p/7822306.html

1.定义

前面已经已经说过了这三种结构之间有联系,这里特意总结一下

首先我们考虑一下三种结构定义:

  1. //链表  
  2. struct node{  
  3.   struct node *next;  
  4.   int data;  
  5. };  
  6.   
  7. //栈  
  8. struct node{  
  9.   int data;  
  10.   struct node *next;  
  11. };  
  12.   
  13. struct stack{  
  14.   struct node *top;  
  15.   struct node *buttom;  
  16. };  
  17.   
  18. //队列  
  19. struct node{  
  20.   int data;  
  21.   struct node *next;  
  22. };  
  23.   
  24. struct queue{  
  25.   struct node *front;  
  26.   struct node *rear;  
  27. }  

你应该发现了一些异常,为什么链表只有node的定义?再来细想一下这三种模型,我们会发现链表其实就是由节点组成的,而栈和队列我们把它视作一个容器,然后可以向里面放node,我们的链表也有头指针和尾指针,我们完全可以这样定义:
struct linkedlist{

struct  node *head;

struct node *tail;

};

这样没有任何问题,只是大家都已经习惯了使用之前的方法,因为我们能发现头指针已经弱化为了节点,就像这样:

struct node *head;

struct node *A=new node;

struct node *B=new node;

A->next=B;//这里不考虑对AB进行赋值

B->next=NULL;

head = A;//notice


2.插入

这里让我们回想一下插入:

  1. //链表插入(由于链表两头都可插入,这里选择了尾部插入)  
  2. struct node *n=new node;  
  3. tail->next=n  
  4. n->next=NULL;  
  5. tail=n;  
  6.   
  7. //队列插入(队列只允许rear插入)  
  8. struct node *n=new node;  
  9. queue->rear->next=n;  
  10. n->next=NULL;  
  11. queue->rear=n;  
  12.   
  13.   
  14. //栈插入  
  15. struct node *n=new node;  
  16. n->next=stack->top;  
  17. stack->top=n;  

提醒一点,队列需要判断是否为空。

从上面你又能发现先链表和队列的插入惊人的相似,而栈有些不同,原因你把这些数据结构图在脑中里面想想就能明白了,队列和链表节点都是横着放,而栈是竖着的,所以栈插入一个节点必然next会指向一个节点而队列和链表由于在尾巴上插入所以next指向NULL


3.删除

接着考虑删除操作:

  1. //链表  
  2. struct node *temp=head;  
  3. head=head->next;  
  4. delete temp;  
  5. //队列(同样需要考虑队列是否为空)  
  6. struct node *temp=queue->front;  
  7. queue->front=queue->front->next;  
  8. delete temp;  
  9. //栈(需要考虑是否为空栈)  
  10. struct node *temp=stack->top;  
  11. stack->top=temp->next;  
  12. delete temp;  


种种表面,这三种结构间存在一些联系,如果能考虑到这里那说明学得不错,因为我们的科学家把他们分为了线性结构,当然剩下的树和图就是非线性结构

作者:racaljk


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值