zoukankan      html  css  js  c++  java
  • 利用顺序栈实现单链表的逆序输出

    源代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #define MAXSIZE 100

    //定义单链表
    typedef struct node
    {
      int data;
      struct node *next;
    }linklist;
    linklist *head;

    //定义栈
    typedef struct st
    {
      int d[100];
      int top;
    }seqstack;
    seqstack s1;

    //创建单链表
    linklist *creatlist()
    {
      linklist *p, *q, *head;
      p = q = (linklist *)malloc(sizeof(linklist));
      head = p;
      p->next = NULL;//定义头结点,头结点数据域为空
      p = (linklist *)malloc(sizeof(linklist));
      scanf("%d", &p->data);
      while (p->data != -1)
      {
        q->next = p;
        q = p;
        p = (linklist *)malloc(sizeof(linklist));
        scanf("%d", &p->data);
      }
      q->next = NULL;
      return head;
    }

    //输出单链表
    void print(linklist *head)
    {
      linklist *p;
      p = head->next;
      if (p == NULL)
        printf(" 空链表! ");
      else
      {
        do
        {
          printf("%6d", p->data);
          p = p->next;
        } while (p != NULL);
      printf(" ");
      }
    }

    //初始化栈,构造一个空栈
    int InitStack(seqstack *s)
    {
      s->top = 0;
      return 1;
    }

    //入顺序栈
    seqstack *Push(seqstack *s, int x)
    {
      if (s->top == MAXSIZE - 1)
      {
        printf(" 栈已满,不能进入栈 ");
        return 0;
      }
      else
      {
        s->top++;
        s->d[s->top] = x;
        return s;
      }
    }

    //判断栈是否为空
    int StackEmpty(seqstack *s)
    {
      if (s->top == 0)
        return 1; //栈为空时返回1(真)
      else
        return 0; //栈非空时返回0(假)
    }

    //出栈操作
    int Pop(seqstack *s)
    {
      int y;
      //if (s->top == 0)
      if(StackEmpty(s))
      {
        printf(" 栈为空,无法出栈! ");
        return 0;
      }
      else
      {
        y = s->d[s->top];
        s->top--;
        return y;
      }
    }

    //利用栈s逆置单链表

    linklist *back_linklist(linklist *head)
    {
      linklist *p;
      InitStack(&s1);
      p = head->next;//p指向首结点

      while (p)
      {
        Push(&s1, p->data); //链表结点中的数据入栈
        p = p->next; //p指针后移
      }
      p = head->next;//p再指向首结点
      while (!StackEmpty(&s1)) //当栈s不空时
      {
        p->data = Pop(&s1); //数据出栈,并存入p所指结点的数据域
        p = p->next; //p指针后移
      }
      return head;
    }

    /*
    linklist *reverse_linklist(linklist *head)
    {
    linklist *p;
    InitStack(&s1);
    p = head->next;
    while (p)
    {
    Push(&s1, p->data);
    if (p->next == NULL)
    p = p->next;
    else
    p = p->next->next;
    }
    p = head->next;
    while (!StackEmpty(&s1))
    {
    p->data = Pop(&s1);
    if (p->next == NULL)
    p = p->next;
    else
    p = p->next->next;
    }
    return head;
    }
    */
    int main()
    {
      linklist *head;
      head = creatlist();
      print(head);
      head = back_linklist(head);
      print(head);

      // system("pause");

      return 1;
    }

    运行结果:

  • 相关阅读:
    IE浏览器版本的判断
    Ajax中的同步和异步
    linq之多表连接
    C#中const 和 readonly 修饰符的用法详解
    sql中的分页实现
    JS中的编码,解码类型及说明
    HttpContext概念讲解
    VS语法书写提示
    c#版本23个设计模式
    批处理 使用默认浏览器 打开html文件
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11646522.html
Copyright © 2011-2022 走看看