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;
}
#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;
}
...全文
相关推荐
c++实现链表删除.cpp
c++实现链表删除.cpp
cpp代码-C/C++ 通用链表代码测试
以上就是关于"cpp代码-C/C++ 通用链表代码测试"的主题涵盖的知识点,这个项目不仅提供了链表的实现,也是对C++基础数据结构和算法实践的考察。
class写的C++链表.cpp
在这个“class写的C++链表.cpp”程序中,我们将探讨如何使用C++的类(class)来实现一个链表。
C++中链表的建立、链表的数据查询的cpp源代码
C++中链表的建立、链表的数据查询的cpp源代码
链表 实现 c++ cpp 数据结构 作业
链表 实现 c++ cpp 数据结构 作业 链表 实现 c++ cpp 数据结构 作业


新手乐园
