c++ 链表

Mr.chan ?? ?? Mr.chan ?? ?? 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
新手一枚,创建一个c++ 链表代码。Input:happy output:hhaappyy。以后是我的代码。请大神帮助哪里出错了。
#include<iostream>

using namespace std;

typedef struct node{
char arr[5];
struct node *next;
}List;

List *CreateList(char line)
{


char arr[5];
List *head = new List;
List *pre = head;
for(int i = 0;i<5;i++)
{
List *p = new List;
arr[i] = line;
pre->next = p;
pre = p;
pre->next=NULL;
}
return head;
}

void display(List *head)
{
List *p = head->next;
int i =0;
while( p!=NULL)
{
cout<<p->arr[i];
cout<<p->arr[i];
p = p->next;
i++;
}
}

int main(){
string line;
cout<<"Enter a string: "<<endl;
cin>>line;
for(int i = 0;i<line.length();i++)
{
List *head = CreateList(line[i]);
display(head);
}
return 0;
}
...全文
评论 3
收藏 已收藏
分享
返回顶部
Mr.chan ?? ??
新手乐园 33,315 成员 41,769 内容