zoukankan      html  css  js  c++  java
  • 栈和队列

    只是简单的建立和输出,剩下的一些操作参考线性表相关内容 先进后出

    线性栈
    #include<stdio.h> #include<stdlib.h> #define max 10 char data[max]; int top = 0; void show(char data[],int top) { int t; t = top-1; for (; t >= 0; t--) printf("%c", data[t]); } int push(char ch) { if (top > max) { printf("out error"); return 0; } data[top] = ch; top++; return 1; } int main() { int i; char s[10] = "abcdefghef"; for (i = 0; i < 10; i++) push(s[i]); show(data, top); }

    链栈  说真的这些临时变量用的我自己都恶心

    #include<stdio.h>
    #include<malloc.h>
    struct node
    {
        char data;
        struct node *next;
    };
    typedef struct node node;
    node *top=NULL;
    int push(char ch)
    {
        node *p;
        p = (node *)malloc(sizeof(node));
        if (!p)return 0;
        p->data = ch;
        p->next = top;
        top= p;
        return 1;
    }
    int pop(char *ch)
    {
        node *p;
        p = top;
        if (!p)return 0;
        ch = p->data;
        top = top->next;
        free(p);
    }
    show(node *top) {
        node *p = top;
        while (p != NULL)
        {
            printf("%c ", p->data);
            p = p->next;
        }
    }
    void main()
    {
        int i;
        char s[] = "abcdefghea";
        for (i = 0; i < 10; i++)
        {
            push(s[i]);
        }
        node *p = top;
        while (p != NULL)
        {
            printf("%c ", p->data);
            p = p->next;
        }
        printf("
    ");
        char ch='a';
        pop(&ch);
        show(top);
        printf("
    ");
        printf("%c", ch);
        
    }

    队列  这个特点就是先进先出

    有毛病的代码 出队后不能及时删除出队的数据、用的是循环队列,总觉得哪里不对但是又说不上来,好像又没错如果是循环队列的话好像这种现象说明我的循环正确
    #include<stdio.h> #define max 10 char data[max]; int head = -1, tail = -1; int push(char ch) { if (tail == (head + max - 1) % max) { printf("队满 "); return 0; } data[tail] = ch; tail = (max + tail+1) % max; return 1; } show() { int i; i = head; while (data[i] != '') { printf("%c ", data[i]); i++; } } int pop(char *ch) { if (head == tail) { printf("队空 "); return 0; } *ch = data[head]; head = (max + head + 1) % max; return 1; } main() { char s[] = "abcdefghee"; int i; for (i = 0; i < 10; i++) { push(s[i]); } show(); char ch='p'; pop(&ch); printf("出队元素:%c ", ch); printf("队内元素: "); show(); printf("队内元素: "); for (i = 0; i < 10; i++) { push(s[i]); }show(); for (i = 0; i < 10; i++) { pop(&ch); printf("出队元素:%c ", ch); } }

     链表栈

    #include<stdio.h>
    #include<stdlib.h>
    struct node 
    {
    	char data;
    	struct node *next;
    };
    typedef struct node node;
    node *head=NULL, *tail=NULL;
    int push(char ch)
    {
    	node *p;
    	p = (node *)malloc(sizeof(node));
    	if (!p)return 0;
    	p->data = ch;
    	p->next = NULL;
    	if (head == NULL)
    	{
    		head = p; printf("%c",head->data);
    	}
    	else {
    		tail->next = p;
    		printf("%c
    ", tail->data);
    	}
    		tail = p;
    		return 1;
    	
    }
    int pop(char *p_y)
    {
    	node *p;
    	if (head == NULL)
    		return(1);
    	*p_y = head->data;
    	p = head;
    	head = head->next;
    	free(p);
    	return(0);
    }
    main()
    {
    	char s[] = "abcdefghee";
    	int i;
    	for (i = 0; i < 10; i++)
    	{
    		push(s[i]);
    
    	}
    	node *p;
    	p = head;
    	while (p != NULL)
    	{
    		printf("%c ", p->data);
    		p = p->next;
    	}
    	printf("
    ");
    	char ch;
    	pop(&ch);
    }
    
  • 相关阅读:
    Java 介绍比较全面的一遍文章
    JSP页面中path和basepath的含义
    myeclipse2014 破解步骤
    word文档去掉复制过来的背景颜色
    String,创建对象问题
    使用Spring框架的好处(转帖)
    myeclipse中将整块的代码所选中的代码左右移动的快捷键
    点击关闭窗口时,弹出提醒的一个事件
    switch能使用的数据类型有6种
    观察者模式(设计模式_15)
  • 原文地址:https://www.cnblogs.com/qxhn/p/6165533.html
Copyright © 2011-2022 走看看