码迷,mamicode.com
首页 > 其他好文 > 详细

九度1511

时间:2015-03-11 19:33:07      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <malloc.h>
#include <stack>
using namespace std;

typedef struct LNode
{
 int value;
 LNode *next;
}*LinkedList;

 

LinkedList createList() //前插入法创建链表
{
 LinkedList head = NULL;
 LNode *p;
 int val;
 scanf("%d",&val);
 while(val != -1)
 {
  p =(LinkedList)malloc(sizeof(LinkedList));
  p->value = val;
  p->next = head;
  head = p;
  scanf("%d",&val);
 }
 return head;
}

LinkedList createList1() //后插入法建立链表
{
 LinkedList head = NULL;
 LNode *tail,*p;
 int val;
 scanf("%d",&val);
 while(val != -1)
 {
   p=(LinkedList)malloc(sizeof(LinkedList));
   p->value = val;
   p->next = NULL;
  if(head == NULL)
  {
   head = p;
   tail = head;
  }
  else
  {
   
   tail->next = p;
   tail = p;
   
  }
  scanf("%d",&val);
 }
 return head;
}

void printLinkedList(LinkedList head) //从头到尾输出链表
{
 LNode *p = head;
 while(p != NULL)
 {
  cout << p->value << endl;
  p = p->next;
 }
}

void printreverseList(LinkedList head) //从尾到头输出链表
{
 stack<LinkedList> list; //建立栈,通过标准库中的栈建立
 LinkedList tail = head;
 while(tail != NULL)
 {
  list.push(tail);
  tail = tail->next;
 }
  while(!list.empty())
  {
   LinkedList n = list.top();
   printf("%d\n",n->value);
   //printf("\n");
   list.pop();
   
  }
}
void printreverseList1 (LinkedList head) //递归法从尾到头输出链表
{
 LinkedList tail = head;
 if(tail != NULL)
 {
  printreverseList1(tail->next);
  printf("%d\n",tail->value);
  
 }
}


int main(int argc, char *argv[])
{
 LinkedList head = createList1();
 printreverseList1(head);
 return 0;
}

九度1511

标签:

原文地址:http://www.cnblogs.com/feichang4and1/p/4330612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!