今天写数据结构题,对于链表的新写法,和对于指针的认识都有新的理解,指针真是一个好东西啊!!!
#include <stdio.h>
#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode List;
List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表;空链表将输出NULL */
List Merge( List L1, List L2 );
int main()
{
List L1, L2, L;
L1 = Read();
L2 = Read();
L = Merge(L1, L2);
Print(L);
//Print(L1);
//Print(L2);
return 0;
}
List Read()
{
int n;
cin>>n;
int x;
cin>>x;
List p=(List)malloc(sizeof(Node));
p->Data=x;
List now;
now=p;
for(int i=0;i<n-1;i++)
{
cin>>x;
List s=(List)malloc(sizeof(Node));
s->Data=x;
s->Next=NULL;
now->Next=s;
now=s;
}
return p;
}
List Merge( List L1, List L2 )
{
List t;
t=L1;
while(t->Next!=NULL)
{
t=t->Next;
}
t->Next=L2;
return L1;
}
void Print( List L )
{
while(L)
{
cout<<L->Data<<" ";
L=L->Next;
}
}
可以好好理解一下这个代码还不错,对于链表的读入有很多种方法,每个人有不同的读入