#ifndef CHAPTER2_LINELINK_LLK_H
#define CHAPTER2_LINELINK_LLK_H
#endif
#include <iostream>
using namespace std;
typedef int Elemtype;
typedef struct LNode {
Elemtype data;
struct LNode *next;
}LNode, *LinkList;
LinkList CreatListByArray(LinkList &L,int n,int a[n]){
int i = 0;
L = (LinkList) malloc(sizeof(LNode));
LNode *s;
L->next = NULL;
LNode *r = L;
L->data = a[0];
for (i = 1; i < n; i++) {
s = (LinkList) malloc(sizeof(LNode));
s->data = a[i];
r->next = s;
s->next = NULL;
r = s;
}
}
LinkList CreatLListByArray(LinkList &L,int n,int a[n]){
int i = 0;
L = (LinkList) malloc(sizeof(LNode));
LNode *s;
L->next = NULL;
LNode *r = L;
L->data = 0;
for (i = 0; i < n; i++) {
s = (LinkList) malloc(sizeof(LNode));
s->data = a[i];
r->next = s;
s->next = NULL;
r = s;
}
}
LinkList CreatListByHead(LinkList &L){
LNode *s;
int x;
L = (LinkList)malloc(sizeof (LNode));
L->next = NULL;
cout<<"请输入需要创建的链表,以 9999 为结束:";
cin>>x;
while (x!=9999){
s = (LinkList)malloc(sizeof (LNode));
s->data = x;
s->next = L->next;
L->next = s;
cin>>x;
}
return L;
}
LinkList CreatListByTail(LinkList &L){
int x;
L = (LinkList)malloc(sizeof (LNode));
LNode *s;
LNode *r = L;
cout<<"请输入需要创建的链表,以 9999 为结束:";
cin>>x;
while (x!=9999){
s = (LinkList)malloc(sizeof (LNode));
s->data = x;
r->next = s;
r = s;
cin>>x;
}
r->next = NULL;
return L;
}
void PrintLinkListwithoutL(LinkList &L){
cout<<"单链表为:"<<endl;
for(LinkList I = L;I!=NULL;I=I->next){
cout<<I->data<<"->";
}
cout<<"Null";
}
void PrintLinkListwithL(LinkList &L){
cout<<"单链表为:"<<endl;
for(LinkList I = L->next;I!=NULL;I=I->next){
cout<<I->data<<"->";
}
cout<<"Null";
}
|