尾插法建立带头结点单链表
#include <iostream>
using namespace std;
/*
*writer:yaojinhui
*/
#define Eletype char
typedef struct LNode
{
Eletype data;
struct LNode *next;
}LNode,*Linklist;
void createList(Linklist &L)
{
LNode *p,*q;
L = (Linklist)malloc(sizeof(LNode));
L->next=NULL;
p=L;
char ch;
while(1)
{
cin>>ch;
if(ch=='#')
break;
q=new LNode;
q->data=ch;
q->next=NULL;
p->next=q;
p=q;
}
p->next=NULL;
}
void print(Linklist L)
{
LNode *p;
p=L->next;
while(p)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
int main() {
Linklist L;
createList(L);
print(L);
return 0;
}