数据结构模板(大学)
1.线性表


#include<iostream>
#define for1(i,a,b) for(int i = a;i <=b;i ++)
using namespace std;
const int maxn = 1e4 + 7;
struct List {
int data[maxn];
int length;
}L;
int n;
void InitList(List& L)
{
L.length = 0;
}
int ListLength(List L)
{
return L.length;
}
int GetNode(List L,int i)
{
if (L.length < i) return -1;
return L.data[i];
}
int LocateNode(List L, int x)
{
for1(i, 1, L.length)
if (L.data[i] == x)
return i;
return -1;
}
void InsertList(List& L,int x, int i)
{
for (int j = L.length;j >= i;j--)
L.data[j + 1] = L.data[j];
L.data[i] = x;
L.length++;
return ;
}
void DeleteList(List& L, int i)
{
for (int j = i;j < L.length;j++)
L.data[j] = L.data[j + 1];
L.length--;
return;
}
void OutList(List L)
{
for1(i, 1, L.length) printf("%d ", L.data[i]);
cout << endl;
}
int main()
{
InitList(L);
cin >> n;
L.length=n;
for1(i, 1, n) cin >> L.data[i];
InsertList(L, 10, 4);
OutList(L);
DeleteList(L,4);
OutList(L);
return 0;
}
2.链表
typedef struct Lnode
{
ElemType data; //数据域
struct LNode *next; //指针域
}LNode,*LinkList;
// *LinkList为Lnode类型的指针
尾插法建立单链表
//后插操作;在p结点之后插入元素e(已知)
bool InsertNextNode (LNode *p,ElemType e){
if (p==NULL)
return false;
LNode *s =(LNode *)malloc(sizeof(LNode))
if (s==NULL)
//内存分配失败
return false;
s->data = e
//用结点5保存数据元素6
s->next=p->next
p->next=s:
//将结点s连到p之后
return true
头插法建立单链表
//后插操作;在p结点之后插入元素e(已知)
bool InsertNextNode (LNode *p,ElemType e){
if (p==NULL)
return false;
LNode *s =(LNode *)malloc(sizeof(LNode))
if (s==NULL)
//内存分配失败
return false;
s->data = e
//用结点5保存数据元素6
s->next=p->next
p->next=s:
//将结点s连到p之后
return true

补一个链表的模板
3.栈
储存结构
#define MaxSize 10 //定义栈中元素的最大个数
typedef struct{
ElemType data[MaxSize]; //静态数组存放栈中元素
int top; //栈顶指针,定义,指向栈顶元素的位置
}SqStack;

#include<iostream>
#define for1(i,a,b) for(int i = a;i <=b;i ++)
using namespace std;
const int maxn = 1e5 + 7;
typedef struct {
int data[maxn];
int top;
} SStack;
SStack S;
void InitStack(SStack& S)
{
S.top = -1;
}
void DestroyStack(SStack& S)
{
SStack* t = &S;
free(t);
return;
}
void Push(SStack& S, int x)
{
S.data[ ++S.top] = x;
}
void Pop(SStack& S, int &x)
{
x =S.data[S.top--];
return;
}
void GetTop(SStack S, int &x)
{
x = S.data[S.top];
return;
}
bool StackEmpty(SStack S)
{
return S.top == -1 ? true : false;
}
int main()
{
InitStack(S);
int n, x;
cin >> n;
for1(i, 1, n)
cin >> x, Push(S, x);
for1(i, 1, n)
{
Pop(S, x);
cout << x << ' ';
}
cout << endl;
return 0;
}
//5
//3 5 1 2 4
4.队列

#include<iostream>
#define for1(i,a,b) for(int i = a;i <=b;i ++)
using namespace std;
const int maxn = 1e5 + 7;
typedef struct {
int data[maxn];
int head,tail;
} Queue;
Queue Q;
void InitQueue(Queue& Q)
{
Q.tail = -1;
Q.head = 0;
}
void DestroyQueue(Queue& Q)
{
Queue* t = &Q;
free(t);
return;
}
void EnQueue(Queue& Q, int x)
{
Q.data[++Q.tail] = x;
return;
}
bool DeQueue(Queue& Q, int &x)
{
if (Q.tail < Q.head) return false;
x=Q.data[Q.head++];
return true;
}
bool GetHead(Queue Q, int &x)
{
if (Q.tail < Q.head) return false;
x = Q.data[Q.head];
return true;
}
bool QueueEmpty(Queue Q)
{
return Q.tail < Q.head ? true : false;
}
int main()
{
InitQueue(Q);
int n, x;
cin >> n;
for1(i, 1, n)
cin >> x, EnQueue(Q, x);
for1(i, 1, n)
{
DeQueue(Q, x);
cout << x << ' ';
}
cout << endl;
return 0;
}
//5
//3 5 1 2 4
5.串

补一个模板

浙公网安备 33010602011771号