首页 > 试题广场 >

输入一行字符,按输入字符的反序建立一个字符结点的单向链表,并

[问答题]

输入一行字符,按输入字符的反序建立一个字符结点的单向链表,并输出该链表中的字符。

推荐
#include <iostream>
using namespace std;
struct node
{
 char ch;
  node *next;  
};
void show( node *head );
int main()
{
 node *head, *p;
  char c;
  head = NULL;
  while( (c = getchar()) != '\n' )        //输入一行字符
   {
 p = new node;                 //建立新结点
        p->ch = c;
        p->next = head;                  //插入表头
        head=p;  
}
   show(head);
}
void show( node *head )              //输出链表
{
 node *p = head;
  cout << "链表中的字符是: \n";
  while( p )
   { cout << p->ch;
      p = p->next;  
}
  cout << endl;
}

发表于 2018-05-07 11:52:43 回复(0)
#include <iostream>
using namespace std;
struct node
{
 char ch;
  node *next;  
};
void show( node *head );
int main()
{
 node *head, *p;
  char c;
  head = NULL;
  while( (c = getchar()) != '\n' )        //输入一行字符
   {
 p = new node;                 //建立新结点
        p->ch = c;
        p->next = head;                  //插入表头
        head=p;  
}
   show(head);
}
void show( node *head )              //输出链表
{
 node *p = head;
  cout << "链表中的字符是: \n";
  while( p )
   { cout << p->ch;
      p = p->next;  
}
  cout << endl;
}
发表于 2020-08-24 21:10:19 回复(0)
11
发表于 2019-09-10 21:03:47 回复(0)