提高篇——数据结构专题(1)

1. stack的实现(后进先出)

c++中可以用STL实现的栈,但是STL里的stack没有实现stack的清空

while(!st_array.empty())
{
       st.pop();
}
    const int maxs = 100010;
    int st_array[maxs] ;
    int TOP = -1;
    /**
     * 在使用pop和top之前应该判断是否为空
     */
    void clear()
    {
     TOP = -1;
    }
    int size()
    {
        return TOP + 1;
    }
    bool empty()
    {
        return TOP == -1;
    }
    void push(int x)
    {
        st_array[++TOP] = x;
    }
    int pop()
    {
        return st_array[TOP--];
    }
    int top()
    {
        return st_array[TOP];
    }

2. queue实现(先进先出)

出队 取队首 取队尾的时候必须判空

STL的queu也没有实现queue的清空 

while(!q.empty()) qu.pop();
/**
 * rear 加元素动,ront出元素动
 */
    int qu[100010] ;
    int front;
    int rear;
    void initialize()
    {
        front = rear = -1;
    }
    void clear()
    {
        front = rear = -1;
    }
    int size()
    {
        return rear - front;
    }
    bool empty()
    {
        return rear == front;
    }
    void push(int x)
    {
        qu[++rear] = x;
    }
    int pop()
    {
        return qu[front++];
    }
    int get_front()
    {
        return qu[front + 1];
    }
    int get_rear()
    {
        return qu[rear];
    }

3. 链表

链表由数据域和指针域组成

#include <cstdio>
#include <algorithm>
#include <cstring>
/**
 * 带头结点的链表头节点记为head
 * head数据域不存放数据,指针与指向第一个数据域有内容的结点
 */
typedef int tname;
struct node{
    tname data;
    node* next;
};
//new所创建的对象对象指向所创建内容的地址
tname* p = new tname ;
int* t = new int ;
//创建链表
node* creat(int array[]) {
    node *p, *pre, *head; //p为当前结点,pre为前驱结点
    head = new node;
    head->next = NULL;//头结点不需要数据域,指针域初始为NULL;
    //pre和head里保存的是结点的地址信息,所以可以用pre来代替head
    pre = head;
    for (int i = 0; i < 5; i++) {
        node *p = new node;
        p->data = array[i];
        p->next = NULL;
        pre->next = p;
        pre = p;
    }
    return head;
}
//查找元素x count
int search(node* head,int x)
{
    int count = 0;
    node *p = new node;
    p = head->next;
    while (p->next != NULL) {
        if(p->data==x) count++;
        p = p->next;
    }
    return count;
}
//将x插入以head为头结点的第pos个位置上
//传参应该传地址,否则只会传形参,不做用在原变量上
void insert(node* head,int pos,int x)
{
    //生成一个node p这个p的指针指向head
    //node *p中*是一个node类型的指针指针指向p,node* p 为固定写法,表示创建一个p
//    node *p = head->next; 写法不对
    node *p = new node;
    p = head;
    for (int i = 0; i < pos -1; ++i) {
        p = p->next;
    }
    node *temp = new node;
    temp->data = x;
    temp->next = p->next;
    p->next = temp;
}
//删除结点
void del(node *head,int x)
{
    node *p = head->next;
    node *pre = head;
    while (p!=NULL)
    {
        if(p->data==x)
        {
            pre->next = p->next;
            delete p;
            p=pre->next;
        }else{
            pre = p;
            p=p->next;
        }
    }
}

int main()
{
    int array[5] = {1,2,3,4,5};
    node* L = creat(array);
    del(L,3);
    L = L->next;//从第
    // 一个结点开始有数据域
    while(L!=NULL)
    {
        printf("%d",L->data);
        L = L->next;
    }
    return 0;
}

静态链表用数组下标直接表示结点

 

 //静态链表
 struct Node{
 	typename data;
 	int next;
 }node[size]; 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值