刚接触面相对象,自己猜着写的,请多指教
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class List
{
private :
typedef int ElementType;
typedef struct Lnode
{
ElementType x;
struct Lnode *next;
} *Ltnode;
Ltnode head = NULL;
int length;
public :
int n;
void BeforeRead()
{
length = 0;
}
bool Read(ElementType &t)
{
if(length++ < n)
{
scanf("%d", &t);
//if(t == -1) return false;
return true;
}
return false;
}
Ltnode Gethead()
{
return head;
}
void ProperList();
void InvertedList();
void oddMake(Ltnode head2);
void evenMake(Ltnode head2);
void ReverseList();
void Newnode(Ltnode &p)
{
p = (Ltnode) malloc( sizeof(struct Lnode) );
p->next = NULL;
p->x = 0;
}
void ListMerge(Ltnode head2);
void PrintL();
int size()
{
return length;
}
};
void List::ProperList()
{
Newnode(head);
Ltnode tail = head;
ElementType t;
BeforeRead();
while(Read(t))
{
Ltnode p;
Newnode(p);
p->x = t;
tail->next = p;
tail = tail->next;
}
}
void List::InvertedList()
{
Newnode(head);
ElementType t;
while(Read(t))
{
Ltnode p;
Newnode(p);
p->x = t;
p->next = head->next;
head->next = p;
}
}
void List::oddMake(Ltnode head2)
{
Newnode(head);
BeforeRead();
Ltnode q = head2->next, tail = head, f = head2;
head->next = NULL;
while(q)
{
if(q->x & 1)
{
length++;
f->next = q->next;
tail = tail->next = q;
q = q->next;
continue;
}
q = q->next;
f = f->next;
}
tail->next = NULL;
}
void List::evenMake(Ltnode head2)
{
Newnode(head);
BeforeRead();
Ltnode q = head2->next, tail = head, f = head2;
head->next = NULL;
while(q)
{
if((q->x+1) & 1)
{
length++;
f->next = q->next;
tail = tail->next = q;
q = q->next;
continue;
}
q = q->next;
f = f->next;
}
tail->next = NULL;
}
void List::ReverseList()
{
Ltnode p = head->next;
Ltnode q;
head->next = NULL;
while(p)
{
q = p->next;
p->next = head->next;
head->next = p;
p = q;
}
}
void List::ListMerge(Ltnode head2)
{
Ltnode p = head->next, q = head2->next, tail = head;
head->next = NULL;
while(p&&q)
{
if(p->x < q->x)
{
tail = tail->next = p;
p = p->next;
}
else
{
tail = tail->next = q;
q = q->next;
}
}
if(p)
{
tail->next = p;
}
if(q)
{
tail->next = q;
}
}
void List::PrintL()
{
if(head && head->next)
{
Ltnode t = head->next;
while(t->next)
{
printf("%d ", t->x);
t = t->next;
}
printf("%d\n", t->x);
}
else
printf("The list is empty!\n");
}
void Listmerge2(List &L1)
{
List L2;
scanf("%d %d", &L1.n, &L2.n);
L1.ProperList();
L2.ProperList();
L1.ListMerge(L2.Gethead());
}
int main()
{
List L;
scanf("%d", &L.n);
L.ProperList();
List L1, L2;
L1.evenMake(L.Gethead());
L2.oddMake(L.Gethead());
printf("%d %d\n", L1.size(), L2.size());
L1.PrintL();
L2.PrintL();
//Listmerge2(L);
return 0;
}