Reproduced by Luofang Senior Sister and some things on the Internet
There are empty nodes in the linked list
Stack does not need to be empty
Linear table
The definition and characteristics
of a linear table An ordered sequence composed of n elements with the same data characteristics is called a linear table. In particular, when n=0, it is an empty table.
Features: (for non-empty linear tables or linear structures)
(1) There is only one data element called the "first"
(2) There is only one data element The data element called the "last"
(3) Except for the first one, each data in the structure has only one predecessor
(4) Except for the last one, each data in the structure has only one successor
Sequence table
Code statement :
#define Maxsize 100 //线性表可能达到的最大长度
typedef struct
{
ElemType *elem; //存储空间的基地址
int length; //当前长度
}Sqlist; //顺序表的数据结构类型为Sqlist
initialization
Status InitList(Sqlist &L)
{//构造一个空的顺序表L
L.elem =new ElemType[Maxsize]; //为顺序表分配一个大小为Maxsize的数组空间
if(!L.elem) exit(OVERFLOW); //存储分配失败退出
L.length=0; //空表长度为0
}
Value
Status GetELem(Sqlist &L,int i)
{
if(i<1||i>L.length) return ERROR; //判断i取值是否合理
e=L.elem[i-1];
return OK;
}
Find
int Locate(Sqlist &L,ElemType e)
{
for(int i=0;i<L.length;i++)
{
if(L.elem[i]==e)
return i+1;
}
return 0;
}
insert
Status ListInsert(SqList &L,int i,int e)
{
if(i<1||i>L.length+1) return ERROR;
if(L.lengrh==Maxszie) return ERROR;
for(int j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];
}
L.elem[i-1]=e;
L.length++;
return OK;
}
delete
Status ListDelete(Sqlist &L,int i)
{
if(i<1||i>L.length) return ERROR;
for(int j=i;j<=L.length-1;j++)
{
L.elem[j-1]=L.elem[j];
}
--L.length;
return OK;
}
Single list
statement
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
initialization
Status InitList(LinkList &L)
{
L=new LNode; //生成新的结点作为头结点,用指针L指向头结点
L->next=NULL; //头结点的指针域置空
return OK;
}
Value
Status GetElem(LinkList &L,int i,ElemType &e)
{
LinkList p=L->next; //用p指向首元节点
j=1; //j作为计数器
while(p&&j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
return ERROR;
e=p->data;
return OK;
}
Find
LNode *LocateElem(LinkList &L,ElemType e)
{
p=L->next;
while(p&&p->data!=e)
{
p=p->next;
}
return p;
}
insert
Status LinkInsert(LinkList &L,int i,ElemType e)
{//在带头结点的单链表L中第i个位置插入元素为e的新节点
s=new LNode;
s->data=e;
p=L;
j=0;
while(p&&j<i-1) //查找第i-1个节点,p指向该节点
{
p=p->next;
++j;
}
if(!p||j>i-1) return ERROR;
s->next=p->next;
p->next=s;
return 0K;
}
delete
Status ListDelete(LinkList &L,int i)
{
//在带头结点的单链表中删除第i个节点
p=L;
j=0;
while((p->next)&&(j<i-1))
{
p=p->next;
j++;
}
if(!(p->next)||(j>i-1)) return ERROR;
q=p->next; //保存待删除的节点
p->next=p->next->next;
delete q;
return OK;
}
Create a singly linked list:
Note: What is the difference between the creation of a singly linked list and the initialization of a singly linked list: The initialization of a singly linked list is to create an empty linked list with only the head node, and the creation of a singly linked list is to create a singly linked list with multiple nodes. That is, starting from the state of the empty list, the nodes of each element are established in turn, and inserted into the linked list one by one.
According to the different insertion positions, it is divided into pre-interpolation and post-interpolation.
(1) Pre- interpolation (obviously, the time complexity of the algorithm is O(n))
void CreatList_H(LinkList &L,int n)
{
//逆位序输入n个元素的值,建立带头结点的单链表
L=new LNode;
L->next=NULL;
for(int i=0;i<n;i++)
{
p=new LNode;
cin>>p->data;
p->next=L-next;
L-next=p;
}
}
(2) Post-interpolation method (the time complexity of the algorithm is still O(n))
In order to insert a new node to the end of the linked list, a new pointer r needs to be added to point to the end node of the linked list
void CreatList(LinkList &L,int n)
{
L=new LNode;
L->next=NULL;
r=L;
for(int i=0;i<n;i++)
{
p=new LNode;
cin>>p->data;
p->next=NULL;
r->next=p;
r=p;
}
}
Circular linked list
statement:
typedef struct DuLNode
{
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;
Stack
Stack: first in last out
Sequential stack representation
#define Maxsize 100
typedef struct
{
SElemType *top;
SElemType *base;
int stackSize;
}SqStack;
Knock on blackboard
initialization
Status InitSatck(SqStack &s)
{
s.base=new SElemType[Maxsize];
if(!s.base) return ERROR;
s.top=s.base;
s.stackSize=Maxsize;
return OK;
}
Push
Status Push(SqStack &s,SElemType e)
{
if(s.top-s.base==Maxsize) return ERROR;
*s.top++=e;
return OK;
}
Unstack
Status Pop(SqStack &s,SElemType &e)
{
if(s.top==s.base) return ERROR;
e=*--top;
return OK;
}
Take the top of the stack
SElemType GeTop(SqStack &s)
{
if(s.top!=s.base)
{
p=s.top;
return *--p;
}
}
Chain stack representation
typedef struct StackNode
{
ElemType data;
struct StackNode *next;
}StackNode,*LinkStack;
initialization
Status InitStack(LinkStack &s)
{
s=NULL;
return OK;
}
Push
Status Push(LinkStack &s,SElemType e)
{
p=new StackNode;
p->data=e;
p->next=s;
s=p;
return 0K;
}
Unstack
status Pop(LinkStack &s,SElemType &e)
{
if(s==NULL) return ERROR;
e=s->data;
p=s;
s=s->next;
delete p;
return OK;
}
Take the top of the stack
SElemType GeTop(LinkStack &s)
{
if(s!=NULL)
return s->data;
}
Representation of sequential circular queue
#define Maxsize 100
typedef struct
{
QElemType *base; //存储空间的基地址
int rear;
int front;
}SqQueue;
initialization
Status InitQueue(SqQueue &Q)
{
Q.base=new QElemType[Maxsize];
if(!Q.base) return ERROR;
Q.front=Q.rear=0;
return OK;
}
Find the length
int QueueLength(SqQueue &Q)
{
return (Q.rear-Q.front+Maxsie)%Maxsize;
}
Join the team
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%Maxsize==Q.front) return ERROR; //队满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%Maxsize;
return OK;
}
Leave the team
Status DeQueue(SQueue &Q,QElemType &e)
{
if(Q.rear==Q.front) return ERROR; //队空
e=Q.base[Q.front];
Q.front=(Q.front+1)%Maxsize;
return OK;
}
Fetch element
QElemType GetTop(SQueue &Q)
{
if(Q.rear!=Q.front)
return Q.base[Q.front];
}
Chain team
typedef struct QNode
{
QElemType data;
struct ONode *next;
}QNode,*QueuePtr;
typedef struct
{
QNode *front;
QNode *rear;
}LinkQueue;
initialization
status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=new QNode;
Q.front->next=NULL;
return OK;
}
Join the team
status EnQueue(LinkQueue &Q,QElemType e)
{
p=new QNode;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p
return OK;
}
Leave the team
status DeQueue(LinkQueue &Q,QElemType &e)
{
if(Q.rear==Q.front) return OK;
p=Q.front->next;
e=p->data;
Q.front->next=p->next; //修改头指针
if(Q.rear=p) Q.rear=Q.font; //如果最后一个元素被删
delete p;
return OK;
}
Take the top of the head
QElemType GeTop(LinkQueue &Q)
{
if(Q.rear!=Q.front)
return Q.front->next->data;
}