面试笔记5栈和队列

本文深入探讨了数据结构中两种基本类型——栈和队列的实现与应用。栈是一种先进后出(FILO)的数据结构,主要通过头部进行元素的添加和移除。队列则是先进先出(FIFO),在尾部添加元素,在头部移除元素。文章提供了链式存储的栈和队列的详细代码实现,包括插入、删除、检查是否为空等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、栈(stack):先进后出的线性表,只能在一端做插入和移除,比如对于数组(顺序栈)必须在位置为0和最后一个位置进行插入和删除,对于一个链表(链式栈)来说:做头部插入时,从头部插入(push),从前面取出(pop)。链式栈和顺序栈都是比较常用的栈。

程序:

链式存储
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct CharNode
{
    char ch;
    struct CharNode *pstNext;
    
};
struct MyStack
{
  int len;
  struct CharNode *pstFirst;
};
int PushStack(struct MyStack *pStack,char ch);
int PopStack(struct MyStack *pStack);//一旦把结点弹出,可以直接销毁结点,或者返回节点的地址
int ClearStack(struct MyStack *pStack);//清空这个链表


int IsEmptyStack(strucy Mystack *pStack);//是不是空表
int IsFullStack(struct MyStack *pStack);//判断是不是满
int PrintStack(struct MyStack *pStack);//把链表的内容进行打印


int main(int argc,char argv[])
{
   return 0;
}

int PushStack(struct MyStack *pStack,char ch);
{
struct CharNode *pstNew=NULL:
if(NULL==pstNew)
{
return -1;
    

}
if(IsFullStack(pStack))
{
return -1;
}

pstNew=(CharNode *)malloc(sizeof(struct CharNode));
if(pstNew==NULL)
{
   return -1;
}

pstNew->ch=ch;
pstNew->pstNext=NULL;
pstNew->pstNext=pStack->pstFist;
pStack->pstFirst=pstNew;
return 0;
pStack->len++;
}

int PopStack(struct MyStack *pStack)//弹出链表的第一个结点
{
struct CharNode *pstNode=NULL;
if(pStack==NULL)
{
   return -1;
}
 if(IsEmptyStack(pStack))
 {
   return -1;
 }
   pstNode=pStack->pstFirst;
 pStack->pstFirst=pstNode->pstNext;
 
 free(pstNode);
 pstNode->pstNext=NULL;

   pStack->len--;   
   return 0;
}
int ClearStack(struct MyStack *pStack);
{    struct CharNode *pstNode=NULL;
     struct CharNode *pstNew=NULL;
     
    if(pStack==NULL)
    {
        return -1;
        
    }
    pstNode=pStack->pstFirst;
    
    while(pstNode!=NULL)
    
    { pstNew=pstNode->pstNext;
      free(pstNode);
      pstNode=pstNew;
      
    }
      pStack->pstFirst=NULL;    
    pStack->len=0
    return -1;
    
}

int IsEmptyStack(struct Mystack *pStack)
{
   if(NULL==pStack)//看其是否是空
     {return 1;}
     
    if(pStack->pstFirst==NULL)
    {
       return 1;
    }
     else
     {
       return 0;
     }
  

}

int IsFullStack(struct MyStack *pStack)
{  
    if(pStack==NULL)
    {return 0;}
    if(pStack->len>=M)
    {
    return 1;
    }
    else
    return 0;

}

int PrintStack(struct MyStack *pStack)
{

  char *pout=NULL;
  struct CharNode *pstNode=NULL;
  pstNode=pStack->pstFirst;
 if(pStack==NULL)
 {
 return -1;
 }
 if(pStack->len<=0)
 {return -1;}
 pout=(char *)malloc(sizeof(pStack->len+1));
 if(pout==NULL)
 {return -1;}
 memset(pout,0,pStack->len+1);
 i=pStack->len-1;
while(pstNode!=NULL)
  {
      pout[i]=pstNode->ch;
      i--;
      pstNode=pstNode->pstNext;
      
  }
 printf("The content is %s\n",pout);
 
return 0;


}


 

2、队列(queue):先进先出的线性表,只能在一端进行插入另一端进行移除。(很少用顺序存储(比较繁琐),一般用链式存储)

链式队列:银行服务窗口排队,两头通的管子,先进的先出。即只能在队列的一端插入(Inqueue),另一端移除(DeQueue)。第一个结点移除,最后一个结点删除。

程序:单向链表:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct ClientNode
{
   int no;
   struct ClientNode *pstNext;


};
struct BankQueue
{
   int len;
   struct ClientNode *pstFirst; 
   struct ClientNode *pstLast;
};


int DestroyBankQueue(struct BankQueue *pstHead)//整个链表销毁成功,头结点没有动态分配所以不需要销毁
{
    struct ClientNode *pstNode=NULL;
    struct ClientNode *pstNext=NULL;
    if(pstHead==NULL)
    {
    return -1;
    }
      pstNode=pstHead->pstFirst;
      if(pstNode!=NULL)
      {
        pstNext=pstNode->pstNext;
        free(pstNode);
        pstNode=pstNext;
      }
return 0;
}

int EnQueueClient(struct BankQueue *pstHead,int no)//插入一个结点,从尾部插入一个节点
    {
    
      struct ClientNode *pstNew=NULL;
      if(pstHead==NULL||no<=0)
      {
         return -1;
      }
      pstNew =(struct ClientNode *)malloc(sizeof(struct ClientNode));
      if(NULL==pstNew)
      {return -1;}
       pstNew->no=no;
       pstNew->pstNext=    NULL;
       if(pstHead->pstLast!=NULL)
           pstHead->pstLast->pstNext=pstNew;
           pstHead->pstLast=pstNew;
        else//表是空表
        {
           pstHead->pstLast=pstNew;
           pstHead->pstFirst=pstNew;
        }
        pstHead->len++;
        return 0;
   
    }


   
struct ClientNode *DeQueueClient(struct BankQueue *pstHead)//移除一个链表,从头部移除一个结点
{  struct ClientNode *pstNode=NULL;
    
  
   if(NULL==pstHead)
     {return -1;}
     
     
     pstNode=pstHead->pstFirst;
     if(pstNode==NULL)
       {return NULL;}
       if(pstNode==pstHead->pstLast)//头部和尾部只有一个结点
       {
       pstHead->pstFirst=NULL;
       pstHead->pstLast=NULL;
       
       
       }
       else//表是空表
     {   
         pstHead->pstFirst=pstNode->pstNext;
     }
         pstNode->pstNext=NULL;
         
         pstHead->len--;
         return pstNode;
       
}


int main(int argc,char *argv[])
{
int quit=1;
int op=0;
int lastno=1;
struct BankQueue bq={0};
struct ClientNode *pstNode=NULL;
while(quit)
{
  op=DisplayMainUI();
  switch(op)
  {
     case 1:
          EnQueueClient(&bq,lastno);
          lastno++;
          break;
      case 2:
          pstNode=DeQueueClient(&bq);
          while(pstNode!=NULL)
          {
            free(pstNode);
            pstNode=NULL;
            break;
          }
          
      case 3:
      
      case 4:
      
     
  }
  


}
return 0;
}
int DisplayMainUI()
{

 

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值