链栈 链队列 share

来源:互联网 发布:杭州两年java工资 编辑:程序博客网 时间:2024/06/06 01:50

发现自己好像越来越low了 没四儿 开心就好

WIN + D 桌面    WIN + L 锁屏

///链栈#include <iostream>using namespace std;typedef struct node///几种不同的打包方式 自己总是糊里糊涂的 多看看吧{    int data;    node *next;    node *top;}LinkStack;//typedef struct{    node *top;}LinkStack;//void stack_push(LinkStack *s, int a){    node *p;    p = new node;    p->data = a;    p->next = s->top;    s->top = p;}int stack_pop(LinkStack *s){    int a;    a = s->top->data;    node *p;    p = s->top;    s->top = p->next;    delete p;    return a;}bool is_Empty(LinkStack *s){    if(s->top)        return false;    return true;}int main(){    int n;    cin >> n;    int m;    LinkStack *s;    s = new LinkStack;///感觉这样写区分度挺高的    s->top = NULL;    for(int i=0; i<n; i++)    {        cin >> m;        stack_push(s, m);    }    //while(!is_Empty(s))    {        int a = stack_pop(s);        cout << a << ' ';    }//    ///这可是一个链栈 不必像上面那么麻烦的遍历啊 不就是个链表吗    node *p;    p = s->top;    while(p)    {        cout << p->data << ' ';        p = p->next;    }    return 0;}///1 2 3 4 5///链队列#include<iostream>using namespace std;typedef struct node{    int data;    node* next;    node *head;    node *tail;}Queue;void queue_push(Queue *q, int m){    node *s;    s = new node;    s->data = m;    s->next = NULL;///插尾巴上    q->tail->next = s;    q->tail = s;}bool is_Empty(Queue *q){    if(q->tail == q->head)        return true;    return false;}int queue_pop(Queue *q){    node *p;    if(q->head == q->tail)        return -1;    int a;    /*p = q->head;    a = p->data;    q->head = p->next;*/    p = q->head->next;///p->head 是h啊 所以不能像之前那样写    q->head->next = p->next;    if(p->next == NULL)        q->tail = q->head;    a = p->data;    delete p;    return a;}int main(){    int n;    int m;    cin >> n;    Queue *q;///    q = new node;    q->next = NULL;///在我看来 这必须要再建一个节点 不然的话 总是崩 如果有大神能指导一下 那就太好啦    node *h;    h = new node;    h->next = NULL;    ///cin >> m;    ///h->data = m;    q->head = h;    q->tail = h;    for(int i=0; i<n; i++)    {        cin >> m;        queue_push(q, m);    }    while(!is_Empty(q))    {        int a = queue_pop(q);        cout << a << ' ';    }    ///写的不好~    /*node *p = q->head->next;    while(p)    {        cout << p->data << ' ';        p = p->next;    }*/    return 0;}

///哈哈哈哈 我终于开始不用数组了   还有个问题 或许 现在应该开始多用函数?#include<iostream>using namespace std;const int maxsize = 100;typedef struct{    int data[maxsize];    int top1;    int top2;}Stack;int stack_length(Stack s){    return (s.top1) + (maxsize-s.top2-1);}void stack_push(Stack *s, int m, int n)   /// *s     ->{    if(s->top1+1 == s->top2)               /// s     .    {        cout << "栈满" << endl;        return;    }    if(n == 1)///进1    {        s->data[s->top1++] = m;    }    else if(n == 2)    {        s->data[s->top2--] = m;    }}void stack_pop(Stack *s, int &e, int n){   /* if(Is_empty(s) == NULL)    {        cout << "栈空" << endl;        return;    }*/    if(n == 1)    {        e = s->data[--s->top1];    }    else if(n == 2)    {        e = s->data[++s->top2];    }}bool Is_empty(Stack s){    if(s.top1 == 0 && s.top2 == maxsize-1)        return true;    return false;}int main(){    Stack s;    s.top1 = 0;    s.top2 = maxsize-1;    int stack_number;    int n;    while(cin >> n && n)    {        cin >> stack_number;        stack_push(&s, n, stack_number);    }    cout << stack_length(s) << endl;    ///while(!Is_empty(s)) 没必要    int e;    while(s.top1 != 0)    {        stack_pop(&s, e, 1);        cout << e << ' ';    }    cout << endl;    while(s.top2 != maxsize-1)    {        stack_pop(&s, e, 2);        cout << e << ' ';    }    return 0;}/*3 15 18 12 24 21 18 2091 8 5 38 4 2Process returned 0 (0x0)   execution time : 19.239 sPress any key to continue.*/


///循环队列///办法一是设一个flag, flag=0,front = rear队空///flag = 1, front = rear 队满///办法二是队列为空时, front = rear, 队列满时,保留一个元素的空///间,队满条件是 (rear+1)%maxsize == front///队列长度公式为(rear-front+maxsize)%maxsize#include <iostream>using namespace std;const int maxsize = 5;typedef struct{    int data[maxsize];    int front_;    int rear;}que;int a[100];void Enqueue(que *Q,int i){    if((Q->rear-Q->front_+1)%maxsize != Q->front_)    {        Q->data[Q->rear] = i;        Q->rear = (Q->rear+1)%maxsize;    }        ///Q->data[Q->rear++] = i;若到最后,应该转向数组头部}void Dequeue(que *Q, int &i){    if(Q->front_ != Q->rear)    {        i = Q->data[Q->front_];        Q->front_ = (Q->front_+1)%maxsize;        ///Q->front_++;    }}bool IS_EMPTY(que Q){    if(Q.rear == Q.front_)        return true;    return false;}int main(){    while(1)    {        que Q;        Q.front_ = 0;        Q.rear = 0;        int n;        int m;        cin >> m;        for(int i=0; i<m; i++)        {            Enqueue(&Q, i);        }        cout << (Q.rear-Q.front_)%maxsize << endl;        int j;        while(!IS_EMPTY(Q))        {            Dequeue(&Q, j);            cout << j << ' ';        }        cout << endl;        cout << (Q.rear-Q.front_)%maxsize << endl;    }    return 0;}


0 0
原创粉丝点击