数据结构——链表、栈和队列

来源:互联网 发布:bbs网络论坛系统uml 编辑:程序博客网 时间:2024/06/04 18:42

链表、栈和队列是基本的三种数据结构,下面是这三种数据结构的简单的代码实现:

链表

链表是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到
下一个节点的指针。
链表中最简单的一种是单向链表,它包含两个域,一个信息域和一个指针域。这个指针指向链表中的下一个节点,
而最后一个节点的指针则指向一个空值。

链表的操作

1、创建链表

2、判断链表是否为空

3、查找某个元素的位置

4、删除某个元素

5、插入元素

6、输出链表的元素

C++实现

#pragma once#ifndef LIST_H#define LIST_H#include<iostream>using namespace std;struct Node{    int data;    Node *next;};Node* Create(const int n)   //创建链表{    Node *head, *p, *r;    head = (Node*)malloc(sizeof(Node));    r = head;    for (int i = 0; i < n; i++)    {        p = (Node*)malloc(sizeof(Node));        cin >> p->data;        r->next = p;        r = p;    }    r->next = NULL;    return head;}bool Isempty(Node* list)    //判断链表是否为空{    return list->next == NULL;}Node* Find(int x, Node* list)   //在链表中查找x的位置{    Node* p;    p = list->next;    while (p != NULL && p->data != x)        p = p->next;    return p;}Node* FindPrevious(int x, Node* list)   //查找x的前驱位置{    Node* p;    p = list;    while (p->next != NULL && p->next->data != x)        p = p->next;    return p;}void Delete(int x, Node* list)  //删除链表中的元素x{    Node* p, *temp;    p = FindPrevious(x, list);    if (p->next!=NULL)    {        temp = p->next;        p->next = temp->next;        free(temp);    }}void Insert(int x, Node* list, Node* p) //在链表中位置p后面插入元素x{    Node* temp;    temp = (Node*)malloc(sizeof(struct Node));    temp->data = x;    temp->next = p->next;    p->next = temp;}void Print(Node* list)  //输出链表{    Node* p;    p = list->next;    while (p!=NULL)    {        cout << p->data << " ";        p = p->next;    }    cout << endl;}#endif

栈是限制插入和删除只能在一个位置上进行的表,该位置是表的末端,叫做栈的顶,栈的特点是先进后出。

栈的操作

1、初始化栈

2、判断栈是否为空

3、判断栈是否为满

4、入栈

5、出栈

6、创建栈

7、输出栈

C++实现

#pragma once#ifndef STACK_H#define STACK_H#include <iostream>#define Maxsize 20using namespace std;struct Stack{    int val[Maxsize];    int top;};Stack* InitStack()  //初始化栈{    Stack* s;    s= (Stack*)malloc(sizeof(Stack));    s->top = 0;    return s;}bool IsEmpty(Stack* s)  //判断栈是否为空{    return s->top == 0;}bool IsFull(Stack* s)   //判断栈是否为满{    return s->top == Maxsize - 1;}void PushStack(const int& x, Stack* s)  //入栈{    if (IsFull(s))        cerr << "Stack is full!";    ++s->top;    s->val[s->top] = x;}int PopStack(Stack* s)  //出栈{    if (IsEmpty(s))        cerr << "Stack is empty!";    --s->top;    return s->val[s->top + 1];}Stack* CreatStack(const int n)  //创建栈{    Stack* s = InitStack();    if (n >= Maxsize)        cerr << "Out of space!";    int temp;    for (int i = 1; i <= n; i++)    {        cin >> temp;        PushStack(temp, s);    }    return s;}void PrintStack(Stack* s)   //从栈底到栈顶输出栈{    for (int i = 1; i <= s->top; i++)        cout << s->val[i] << " ";    cout << endl;}#endif

队列

像栈一样,队列也是表,然而使用队列时插入在一端进行而删除则在另一端进行,队列的特点是先进先出。

栈的操作

1、初始化队列

2、判断队列是否为空

3、判断队列是否为满

4、入队

5、出队

6、创建队列

7、输出队列

C++实现

#pragma once#ifndef QUEUE_H#define QUEUE_H#define MaxSize 20#include<iostream>using namespace std;struct Queue{    int front;  //队首位置    int rear;   //队尾位置    int size = rear - front;    int val[MaxSize];};Queue* InitQueue()  //初始化队列{    Queue* q = (Queue*)malloc(sizeof(Queue));    q->front = q->rear = 0;    return q;}int IsEmpty(Queue* q)   //判断队列是否为空{    return q->size == 0;}int IsFull(Queue* q)    //判断队列是否为满{    return q->size == MaxSize;}void EnQueue(int x, Queue* q)   //入队{    if (IsFull(q))        cerr << "Full queue";    ++q->rear;    q->val[q->rear] = x;}int DeQueue(Queue* q)   //出队{    if (IsEmpty(q))        cerr << "Empty queue";    ++q->front;    return q->val[q->front];}Queue* CreateQueue(const int& n)    //创建队列{    Queue* q = InitQueue();    int temp;    for (int i = 1; i <= n; i++)    {        cin >> temp;        EnQueue(temp, q);    }    return q;}void PrintQueue(Queue* q)   //从队首到队尾输出队列{    for (int i = q->front + 1; i <= q->rear; i++)        cout << q->val[i] << " ";    cout << endl;}#endif
原创粉丝点击