一、顺序表
seqlist.h
#pragma once
#define max 10
class Node
{
private:
int data[max];
int length;
public:
Node();
void Insert(int pos,int e);
void Delete(int pos);
void Print();
void Find(int e);
void Clear();
void Empty();
};
#define max 10
class Node
{
private:
int data[max];
int length;
public:
Node();
void Insert(int pos,int e);
void Delete(int pos);
void Print();
void Find(int e);
void Clear();
void Empty();
};
seqlist.cc
#include<iostream>
#include"puc.h"
using namespace std;
Node::Node()
{
length = 0;
}
void Node::Insert(int pos,int e)
{
if (pos < 0 || pos > length)
{
cout<<"error";
return;
}
if (pos < length)
{
for (int i = length;i>=pos;i--)
data[i+1] = data[i];
}
data[pos] = e;
length++;
}
void Node::Delete(int pos)
{
if (length == 0)
return;
if (pos<0 || pos>length-1)
{
cout<<"error";
return;
}
if (pos <= length-1)
{
for (int i=pos;i<length-1;i++)
data[i] = data[i+1];
}
length--;
}
void Node::Print()
{
int n = length;
for (int i = 0;i<n;i++)
cout<<data[i];
}
void Node::Find(int e)
{
for (int i=0;i<length;i++)
{
if (data[i] == e)
cout<<i;
}
}
void Node::Clear()
{
if (length == 0)
return;
else
length = 0;
}
void Node::Empty()
{
if (length == 0)
cout<<"ture";
else
cout<<"no true";
}
#include"puc.h"
using namespace std;
Node::Node()
{
length = 0;
}
void Node::Insert(int pos,int e)
{
if (pos < 0 || pos > length)
{
cout<<"error";
return;
}
if (pos < length)
{
for (int i = length;i>=pos;i--)
data[i+1] = data[i];
}
data[pos] = e;
length++;
}
void Node::Delete(int pos)
{
if (length == 0)
return;
if (pos<0 || pos>length-1)
{
cout<<"error";
return;
}
if (pos <= length-1)
{
for (int i=pos;i<length-1;i++)
data[i] = data[i+1];
}
length--;
}
void Node::Print()
{
int n = length;
for (int i = 0;i<n;i++)
cout<<data[i];
}
void Node::Find(int e)
{
for (int i=0;i<length;i++)
{
if (data[i] == e)
cout<<i;
}
}
void Node::Clear()
{
if (length == 0)
return;
else
length = 0;
}
void Node::Empty()
{
if (length == 0)
cout<<"ture";
else
cout<<"no true";
}
test.cpp
#include<iostream>
#include"puc.h"
using namespace std;
int main()
{
Node l;
l.Insert(0,1);
l.Insert(1,2);
l.Insert(1,3);
l.Delete(3);
l.Clear();
l.Empty();
l.Print();
cout<<endl;
l.Find(1);
return 0;
}
#include"puc.h"
using namespace std;
int main()
{
Node l;
l.Insert(0,1);
l.Insert(1,2);
l.Insert(1,3);
l.Delete(3);
l.Clear();
l.Empty();
l.Print();
cout<<endl;
l.Find(1);
return 0;
}
二、单链表(无头)
List.h
#pragma once
typedef struct Node
{
int data;
struct Node *next;
}*node;
class List
{
private:
node head;
public:
List();
~List();
void Insert(int pos,int e);
void Delete(int pos);
void Print();
void Clear();
void Empty();
void Head(int *arr,int len);
void Tail(int *arr,int len);
};
typedef struct Node
{
int data;
struct Node *next;
}*node;
class List
{
private:
node head;
public:
List();
~List();
void Insert(int pos,int e);
void Delete(int pos);
void Print();
void Clear();
void Empty();
void Head(int *arr,int len);
void Tail(int *arr,int len);
};
List.cpp
#include<iostream>
#include"puc.h"
using namespace std;
List::List()
{
head = NULL;
}
void List::Insert(int pos,int e)
{
node p = head,q;
q = new Node;
q->data = e;
if (pos == 0)
{
q->next = p;
head = q;
return;
}
int i = 1;
while (p && i<pos)
{
p = p->next;
i++;
}
if (!p || i>pos)
{
cout<<"error";
return;
}
q->next = p->next;
p->next = q;
}
void List::Print()
{
node p = head;
while(p)
{
cout<<p->data;
p = p->next;
}
}
void List::Delete(int pos)
{
node p = head,q;
if (pos <0 || p==NULL)
{
cout<<"error";
return;
}
if (pos == 0)
{
head = p->next;
delete p;
return;
}
int i = 1;
while (p && i<pos)
{
p = p->next;
i++;
}
if (!(p->next) || i>pos)
{
cout<<"error";
return;
}
q = p->next;
p->next = q->next;
delete q;
}
void List::Clear()
{
node p = head,q;
while (p)
{
q = p;
p = p->next;
delete q;
}
head = NULL;
}
void List::Empty()
{
if (head)
cout<<"no ture";
else
cout<<"ture";
}
List::~List()
{
Clear();
}
void List::Head(int *arr,int len)
{
node p = head,q;
for (int i = 0;i < len;i++)
{
q = new Node;
q->data = arr[i];
q->next = head;
head = q;
}
}
void List::Tail(int *arr,int len)
{
node p = head,q;
if (p == NULL)
{
for (int i=0;i<len;i++)
{
q = new Node;
q->data = arr[i];
q->next = head;
head = q;
}
return;
}
while (p->next)
{
p = p->next;
}
for (int i=0;i<len;i++)
{
q = new Node;
q->data = arr[i];
q->next = p->next;
p->next = q;
p = q;
}
}
#include"puc.h"
using namespace std;
List::List()
{
head = NULL;
}
void List::Insert(int pos,int e)
{
node p = head,q;
q = new Node;
q->data = e;
if (pos == 0)
{
q->next = p;
head = q;
return;
}
int i = 1;
while (p && i<pos)
{
p = p->next;
i++;
}
if (!p || i>pos)
{
cout<<"error";
return;
}
q->next = p->next;
p->next = q;
}
void List::Print()
{
node p = head;
while(p)
{
cout<<p->data;
p = p->next;
}
}
void List::Delete(int pos)
{
node p = head,q;
if (pos <0 || p==NULL)
{
cout<<"error";
return;
}
if (pos == 0)
{
head = p->next;
delete p;
return;
}
int i = 1;
while (p && i<pos)
{
p = p->next;
i++;
}
if (!(p->next) || i>pos)
{
cout<<"error";
return;
}
q = p->next;
p->next = q->next;
delete q;
}
void List::Clear()
{
node p = head,q;
while (p)
{
q = p;
p = p->next;
delete q;
}
head = NULL;
}
void List::Empty()
{
if (head)
cout<<"no ture";
else
cout<<"ture";
}
List::~List()
{
Clear();
}
void List::Head(int *arr,int len)
{
node p = head,q;
for (int i = 0;i < len;i++)
{
q = new Node;
q->data = arr[i];
q->next = head;
head = q;
}
}
void List::Tail(int *arr,int len)
{
node p = head,q;
if (p == NULL)
{
for (int i=0;i<len;i++)
{
q = new Node;
q->data = arr[i];
q->next = head;
head = q;
}
return;
}
while (p->next)
{
p = p->next;
}
for (int i=0;i<len;i++)
{
q = new Node;
q->data = arr[i];
q->next = p->next;
p->next = q;
p = q;
}
}
test.cpp
#include<iostream>
#include"puc.h"
using namespace std;
int main()
{
int arr[5] = {1,2,3,4,5};
List h;
h.Insert(0,1);
h.Insert(1,2);
h.Insert(2,3);
h.Delete(3);
/*h.Clear();*/
h.Empty();
h.Head(arr,5);
h.Tail(arr,5);
h.Print();
return 0;
}
#include"puc.h"
using namespace std;
int main()
{
int arr[5] = {1,2,3,4,5};
List h;
h.Insert(0,1);
h.Insert(1,2);
h.Insert(2,3);
h.Delete(3);
/*h.Clear();*/
h.Empty();
h.Head(arr,5);
h.Tail(arr,5);
h.Print();
return 0;
}