c++ 链表
新手一枚,创建一个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;
}