输入一行字符,按输入字符的反序建立一个字符结点的单向链表,并输出该链表中的字符。
#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; }
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题