数据结构(链表/栈/队列)

332 阅读3分钟

链表(链式)

typedef struct LNode{
	ElemType data;        
	struct LNode *next;
}LNode,*LinkList;  
Status List_Init(LinkList *L); 
void List_Create(LinkList L);
int main()
{
	LinkList L ;
	List_Init(&L);
	List_Create(L);
}
/*
	头节点的初始化 
*/
Status List_Init(LinkList *L)
{
	*L = (LinkList)malloc(sizeof(LNode));	//头节点的初始化 
	(*L)->next = NULL;
	return OK;
}
/*
 尾插法创建单链表 
*/
void List_Create(LinkList L)
{
	LinkList p,r;
	int num;
	r = L;		//rear 尾指针 处于最后一个元素的位置 
	do
	{
		scanf("%d",&num);
		p = (LinkList)malloc(sizeof(LNode));
		p->data = num;
		r->next = p;
		r = p;
	}while(getchar()!='\n');
	r->next = NULL;
}

结论:

  1. 初始化链表头部指针需要用二级指针或者一级指针的引用(c++)。

  2. 销毁链表需要用到二级指针或者一级指针的引用。

  3. 插入、删除、遍历、清空结点用一级指针即可。

分析:

对于头节点L,修改L本身需要用二级指针,但是修改L->next的指向用一级指针就可以了

堆栈(数组实现)

#include<bits/stdc++.h>
using namespace std;
typedef int Position;
struct SNode {
    int *Data; /* 存储元素的数组 */
    Position Top;      /* 栈顶指针 */
    int MaxSize;       /* 堆栈最大容量 */
};
typedef struct SNode *Stack;
/*
	堆栈的初始化 
*/
Stack Stack_Init(int Maxsize)
{
	Stack S = (Stack)malloc(sizeof(struct SNode));
	S->Data = (int *) malloc(Maxsize*sizeof(int));
	S->Top = -1;
	S->MaxSize = Maxsize;
	return S;
}
bool IsFull(Stack S)
{
	return (S->Top == S->MaxSize-1);
}
bool IsEmpty(Stack S)
{
	return (S->Top==-1);
}
bool Push(Stack S,int x)
{
	if(IsFull(S))
	{
		cout<<"堆栈满";		return false;
	}
	S->Data[++(S->Top)] = x;
	return true;
}
int Pop(Stack S)
{
	if(S->Top==-1) 
	{
		cout<<"堆栈空";
		return false; 
	}
	return S->Data[(S->Top)--];
}
int main()
{
	Stack S;
	S = Stack_Init(100);
	Push(S,1);
	Push(S,3);
	Push(S,11);
	while(!IsEmpty(S))
	{
		cout<<Pop(S)<<" ";
	}
}

堆栈(链表实现)

#include<bits/stdc++.h>
using namespace std;
struct SNode {
    int Data;
    struct SNode *Next;
};
typedef struct SNode*Stack;
Stack Stack_Init()
{
	Stack S = (Stack)malloc(sizeof(struct SNode));
	S->Next = NULL;
	return S;
}
bool Push(Stack S,int x)
{
	Stack p = (Stack)malloc(sizeof(struct SNode));
	p->Data = x;
	p->Next = S->Next;
	S->Next = p;
	return true;
}
int Pop(Stack S)
{
	Stack q = S->Next;
	if(!q) 
	{
		cout<<"堆栈空";
		return -1;
	}
	int res = q->Data;
	S->Next = q->Next;
	free(q);
	return res;
}
int main()
{
	Stack S;
	S = Stack_Init();
	Push(S,1);
	Push(S,3);
	Push(S,11);
	while(S->Next)
	{
		cout<<Pop(S)<<" ";
	}
}

一个数组中实现两个堆栈

#include <stdio.h>
#include <stdlib.h>
#define ERROR 1e8
typedef int ElementType;
typedef int Position;
struct SNode {
    ElementType *Data;
    Position Top1, Top2;
    int MaxSize;
};
typedef struct SNode *Stack;

Stack CreateStack( int MaxSize )
{
	Stack S = (Stack)malloc(sizeof(struct SNode));
	S->Data = (ElementType*)malloc(MaxSize*sizeof(ElementType));
	S->Top1 = -1;
	S->Top2 = MaxSize;
	S->MaxSize = MaxSize;
	return S;
}
bool Push( Stack S, ElementType X, int Tag )
{
	if(S->Top2 - S->Top1==1)
	{
		printf("Stack Full\n");
		return false;
	}
	if(Tag==1)
	{
		S->Data[++(S->Top1)] = X;
	}
	else
	{
		S->Data[--(S->Top2)] = X;
	}
}
ElementType Pop( Stack S, int Tag )
{
	if(Tag==1)
	{
		if(S->Top1==-1)
		{
			printf("Stack %d Empty\n",Tag);
			return ERROR;
		}
		else
			return S->Data[(S->Top1)--];
	}
	else
	{
		if(S->Top2==S->MaxSize)
		{
			printf("Stack %d Empty\n",Tag);
			return ERROR;
		}
		else
			return S->Data[(S->Top2)++];
	}
}
int main()
{
    Stack S;
    S = CreateStack(5);
    Push(S,1,1);
    Push(S,3,1); 
    Push(S,5,1);
    Push(S,5,2);
    Push(S,6,2);
    Push(S,7,2);
    while(S->Top1!=-1)
    {
    	printf("%d ",Pop(S,1));
	}
	printf("\n------------\n");
	while(S->Top2!=S->MaxSize)
    {
    	printf("%d ",Pop(S,2));
	}
	
}

循环队列(顺序表实现)

  1. Q->front == Q->rear无法区分 队列空/满
    • 使用额外标记,比如用size记录队列大小
    • 仅使用n-1个空间`
  2. 为什么使用指针的原因struct QNode *Queue,因为相当于对链表头节点进行操作,不然调用要用(&Q)
#include<bits/stdc++.h>
using namespace std;
struct QNode {
    int *Data;     /* 存储元素的数组 */
    int Front;		//队列不空,指向队列头元素 
    int Rear;		//队列不空,指向队列尾元素的下一位置 
    int MaxSize;    /* 队列最大容量 */
};
typedef struct QNode *Queue;
void Queue_Init(Queue *Q,int maxsize)
{
	*Q = (Queue)malloc(sizeof(struct QNode));
	(*Q)->Data = (int *)malloc(sizeof(int));
	(*Q)->Front = (*Q)->Rear = 0;
	(*Q)->MaxSize = maxsize;
}
bool PushQ(Queue Q,int x)
{
	if( (Q->Rear+1)%(Q->MaxSize)==(Q->Front) ) 
	{
		printf("队列满\n");
		return false;
	}
	Q->Data[Q->Rear] = x;
	Q->Rear = (Q->Rear+1)%Q->MaxSize;
	return true;
}
int PopQ(Queue Q)
{
	if(Q->Front==Q->Rear) 
	{
		printf("队列空");
		return 1e8; 
	}
	int res = Q->Data[Q->Front];
	Q->Front = (Q->Front+1)%Q->MaxSize;
	return res;
}
int main()
{
	Queue Q;	
	Queue_Init(&Q,5);
	PushQ(Q,11);PushQ(Q,21);PushQ(Q,31);
	PushQ(Q,41);PushQ(Q,41);
	int n;
	while(1)
	{
		n=PopQ(Q);
		if(n==1e8) break;
		printf("%d ",n);
	}
}