第1个回答
2020.01.17回答#include"stdafx.h"#include<iostream>usingnamespacestd;typedefintElemType;structListNode{intval;ListNode*next;ListNode(intx):val(x),next(NULL){}};classSolution{public://创建无头结点的单链表ListNode*Create(void){chars;ListNode*tail=NULL;ListNode*head=NULL;while(1){ListNode*tmp=newListNode(0);//定义一个s节点用来存放每次要输入的值scanf("%d",&tmp->val);if(head==NULL){head=tmp;}else{tail->next=tmp;}tail=tmp;s=getchar();if(s=='\n'){break;}}if(tail!=NULL)tail->next=NULL;returnhead;}//输出无头结点单链表voidList(ListNode*L){ListNode*p;p=L;//while(p!=NULL){printf("%2d",p->val);p=p->next;}}};intmain(){ListNode*L1;inttemp;printf("请输入链表:\n");L1=Solution().Create();printf("无头结点的链表为:\n");Solution().List(L1);system("pause");return0;}