📄 stackandqueue.cpp
字号:
#include "stackandqueue.h"
#include <conio.h>
#include "iostream.h"
//ham khoi tao danh sach
void InitList(LIST &l)
{
l.phead=NULL;
l.ptail=NULL;
}
//ham kiem tra danh sach rong
int IsEmptyList(LIST l)
{
if(l.phead==NULL)
return 0;
return 1;
}
//tao mot node moi
node *makenode(int a)
{
node *newnode;
newnode=new node;//cap phat
if(newnode==NULL)
{
cout<<"\n khong du bo nho:";
return NULL;
}
newnode->info=a;
newnode->pnext=NULL;
return newnode;
}
//ham chen phan tu dau tien
void InsertHead(LIST &l,int x)
{
node *newnode;
newnode=makenode(x);
if(l.phead==NULL)
{
l.phead=newnode;
l.ptail=l.phead;
}
else
{
newnode->pnext=l.phead;
l.phead=newnode;
}
}
//ham them vao cuoi cung
void InsertTail(LIST &l,int x)
{
node *newnode;
newnode=makenode(x);
if(l.phead==NULL)
{
l.phead=newnode;
l.ptail=l.phead;
}
else
{
l.ptail->pnext=newnode;
l.ptail=newnode;
}
}
//ham xoa phan tu dau tien trong danh sach
void RemoveFirst(LIST &l)
{
node *p;
if(l.phead!=NULL)
{
p=l.phead;
if(p->pnext==NULL)//kiem tra co 1 node
l.phead=l.ptail=NULL;
l.phead=p->pnext;
if(p->pnext==l.ptail)//kiem tra co hai node
l.ptail=l.phead;
delete(p);
}
else return;
}
//ham khoi tao stack
void InitStack(LIST &stack)
{
InitList(stack);
}
//ham kiem tra danh sach rong
int IsEmptyStack(LIST stack)
{
return IsEmptyList(stack);
}
//them mot phan tu vao ngan xep
void Push(LIST &stack,int x)
{
InsertHead(stack,x);
}
//ham lay thong tin o dinh ngan xep
int Top(LIST stack)
{
if(IsEmptyStack(stack)!=0)
return stack.phead->info;
return NULL;
}
//lay thong tin va huy phan tu o dinh ngan xep
int Pop(LIST &stack)
{
if(IsEmptyStack(stack)!=0)
{
int v=stack.phead->info;
RemoveFirst(stack);
return v;
}
return NULL;
}
//tao hang doi rong
void InitQueue(LIST &queue)
{
InitList(queue);
}
//kiem tra han doi rong
int IsEmptyQueue(LIST queue)
{
return IsEmptyList(queue);
}
//them mot phan tu vao cuoi hang doi
void EnQueue(LIST &queue,int x)
{
InsertTail(queue,x);
}
//lay thong tin phan tu o dau hang doi
int Front(LIST queue)
{
if(IsEmptyQueue(queue)!=0)
return queue.phead->info;
return NULL;
}
//Lay thong tin va huy pha ntu o dau hang doi
int DeQueue(LIST &queue)
{
if(IsEmptyQueue(queue)!=0)
{
int v;
v=queue.phead->info;
RemoveFirst(queue);
return v;
}
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -