33,311
社区成员
发帖
与我相关
我的任务
分享
#ifndef SIMPLELIST_H
#define SIMPLELIST_H
//#include <windows.h>
template <class T> class SimpleListNode ...{
public:
T &get() ...{ return object; };
void set(T &object) ...{ this->object = object; };
SimpleListNode<T> *getNext() ...{ return nextNode; };
void setNext(SimpleListNode<T> *nextNode) ...{ this->nextNode = nextNode; };
private:
T object;
SimpleListNode<T> *nextNode;
};
template <class T> class SimpleList ...{
public:
// Constructor
SimpleList() ...{
headNode = new SimpleListNode<T>;
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
// Destructor
~SimpleList() ...{
SimpleListNode<T> *pointerToDelete, *pointer = headNode;
while (pointer != NULL) ...{
pointerToDelete = pointer;
pointer = pointer->getNext();
delete pointerToDelete;
}
};
T &get() ...{
if (currentNode == NULL)
start();
return currentNode->get();
};
void add(T &addObject) ...{
SimpleListNode<T> *newNode = new SimpleListNode<T>;
newNode->set(addObject);
newNode->setNext(headNode->getNext());
headNode->setNext(newNode);
size++;
};
void remove() ...{
lastCurrentNode->setNext(currentNode->getNext());
delete currentNode;
currentNode = lastCurrentNode;
size--;
};
void start() ...{
lastCurrentNode = headNode;
currentNode = headNode;
};
bool next() ...{
// If the currentNode now points at nothing, we've reached the end
if (currentNode == NULL)
return false;
// Update the last node and current node
lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
// If currentNode points at nothing or there is nothing added, we can immediately return false
if (currentNode == NULL || size == 0)
return false;
else
return true;
};
int getSize() ...{ return size; };
private:
int size;
SimpleListNode<T> *headNode;
SimpleListNode<T> *currentNode, *lastCurrentNode;
};
#endif
#include <iostream>
using namespace std;
template <class T>
class LinkList{
private:
struct LNode{
T data;
struct LNode *next;
};
LNode *hlist;
static int len;
//特别注意指针的移动
public:
LinkList();
~LinkList();
bool IsEmpty()const;
void InsFirst(const T & e); //插入的结点作为头结点
void InsLast(const T & e); //在链尾插入
int GetLength(); //返回链表长度
bool DelFirst(); //删除头结点
bool InsAfter(const T & e,int n); //在第n个结点后插入
bool DelIndex(int n); //删除指定的索引结点
void TraverseList(); //遍历整个表
};
template <class T>
int LinkList<T>::len=0;
template <class T>
LinkList<T>::LinkList(){
hlist=NULL;
}
template <class T>
LinkList<T>::~LinkList(){
while(!IsEmpty()){
cout<<"Destroy list,Destroy list length is: "<<len<<endl;
DelFirst();
system("pause");
}
exit(0);
cout<<"Destroy list,Destroy list length is: "<<len<<endl;
system("pause");
}
template <class T>
bool LinkList<T>::IsEmpty()const{
return len==0;
}
template <class T>
void LinkList<T>::InsFirst(const T &e){
LNode *p=new LNode;
p->data=e;
if(len==0){
p->next=NULL;
hlist=p;
}
else{
p->next=hlist;
hlist=p;
}
len++;
}
template <class T>
void LinkList<T>::InsLast(const T &e){
if(IsEmpty())
InsFirst(e);
else{
LNode *p=new LNode;
p->data=e;
LNode *temp=hlist;
while(temp->next!=NULL)
temp=temp->next;
p->next=temp->next;
temp->next=p;
len++;
}
}
template <class T>
bool LinkList<T>::InsAfter(const T & e,int n){
if(n<0||n>len)
return false;
if(IsEmpty()) //如果链表为空 作为头结点插入
InsFirst(e);
else{
LNode *p=new LNode;
p->data=e;
LNode *temp=hlist;
while(n-1>0){
n--;
temp=temp->next;
}
p->next=temp->next;
temp->next=p;
len++;
}
return true;
}
template <class T>
bool LinkList<T>::DelIndex(int n){
if(IsEmpty()||n<0||n>len)
return false;
else{
LNode *temp=hlist;
while(n-2>0){
temp=temp->next;
n--;
}
LNode *p=temp->next;
temp->next=p->next;
delete p;
len--;
}
return true;
}
template <class T>
int LinkList<T>::GetLength(){
return len;
}
template <class T>
bool LinkList<T>::DelFirst(){
LNode *p=hlist;
if(IsEmpty())
return false;
else{
hlist=p->next;
delete p;
len--;
}
}
template <class T>
void LinkList<T>::TraverseList(){
LNode *p=hlist;
while(p!=NULL){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int main(){
LinkList<int> L;
L.InsFirst(3);
L.InsFirst(2);
L.InsFirst(5);
L.InsFirst(1);
L.InsLast(4);
L.InsLast(6);
cout<<"Now , Linklist all mumber is: "<<endl;
L.TraverseList();
cout<<"The Linklist length is: "<<L.GetLength()<<endl;
cout<<"Delete the fifths mumber."<<endl;
L.DelIndex(2);
cout<<"Now , Linklist all mumber is: "<<endl;
L.TraverseList();
cout<<"Insert '5'."<<endl;
L.InsAfter(5,4);
cout<<"The Linklist length is: "<<L.GetLength()<<endl;
L.TraverseList();
return 0;
}
#include<iostream.h>
struct node{
int data;
node *next;
};
node* Listcreat() //´´½¨Á´±í
{
node *head,*n,*tail;
int a;
head=NULL; //¿ÕÁ´
cout<<"ÇëÊäÈëÊý¾Ý£¨0±íʾ½áÊø£©:";
for(cin>>a;a!=0;cin>>a)
{
n=new node;
n->data=a;
n->next = NULL;
if(!head) tail=head=n;
else tail->next=n,tail=n;
}
if(head) tail->next=NULL;
return head;
}
void clearlist(node *list)//Èç¹ûÁ´±í²»ÊÇÓÐÐòÇÒÊÇÉýÐòµÄ£¬Ôò½«Á´±í±»ËùÓнáµãɾ³ý
{
node*head,*p;
head=list;
while(head)
{
p=head;head=head->next;
delete p;
}
cout<<"ÎÞЧÊý¾ÝÇå³ý³É¹¦£¡\n";
}
int panduan(node *List)//ÅжÏÁ´±íÊDz»ÊÇÓÐÐò£¨ÉýÐò£©µÄÁ´±í
{
node *head,*p;
head=List;
if(head)
{
p=head->next;
for(head;head->next!=NULL;head=head->next,p=head->next)
for(p;p!=NULL;p=p->next)
if(p->data<head->data)
return 0;
if(!head->next)
return 1;
}
else
return 0;
return 1;
}
void print(node*List)//Êä³öÁ´±íÉÏËùÓеÄÊý¾ÝÔªËØ
{
node *p;
p=List;
while(p)
{ cout<<p->data<<'\t';p=p->next; }
cout<<endl;
}
void DelList(node *p)
{
node *q = p;
while(p)
{
q = p->next;
delete p;
p = q;
}
}
void combin (node *La,node *Lb) //ʵÏÖÁ´±íLa,LbµÄ¹é²¢,¹é²¢ºóΪLc
{
node *pa,*pb,*pc,*Lc;
pa=La;
pb=Lb;
pc = new node;
pc->next = NULL;
Lc=pc; //¹é²¢ºóµÄÁ´±íµÄÍ·½áµãÖ¸ÏòÁ¬±íLaµÄÍ·½áµã
while(pa&&pb)
{
if(pa->data<=pb->data)
{
pc->next=pa;
pa=pa->next;
}
else
{
pc->next=pb;
pb=pb->next;
}
pc = pc->next;
}
if(pa)
pc->next=pa;//²åÈëÊ£ÓàµÄ½áµã
else
pc->next=pb;//²åÈëÊ£ÓàµÄ½áµã
pc = Lc;
Lc = Lc->next;
delete pc;
print(Lc);//Êä³ö¹é²¢ºóµÄ¼¯ºÏÔªËØ
DelList(Lc);
}
void main()
{
node *List_La = NULL, *List_Lb = NULL;
cout<<"¼¯ºÏA"<<endl;
List_La=Listcreat();//´´½¨Á´±íList_La´æ´¢¼¯ºÏAµÄÔªËØ
while(!panduan(List_La))
{
cout<<"ÊäÈëµÄÊý¾ÝÎÞЧ£¬ÇëÊäÈëÓÐÐòµÄ¼¯ºÏÔªËØ!\n";
clearlist(List_La);
List_La=Listcreat();//ÅжϺóÖØÐÂÊäÈ뼯ºÏÔªËØ
}
cout<<"¼¯ºÏB"<<endl;
List_Lb=Listcreat();//´´½¨Á´±íList_La´æ´¢¼¯ºÏAµÄÔªËØ
while(!panduan(List_Lb))
{
cout<<"ÊäÈëµÄÊý¾ÝÎÞЧ£¬ÇëÊäÈëÓÐÐòµÄ¼¯ºÏÔªËØ!\n";
clearlist(List_Lb);
List_Lb=Listcreat();
}
cout<<"¾¹ý¹é²¢ºó£¡";
combin(List_La,List_Lb);
}
两个链表归并排序,乱码是中文,你可以忽略不计递归创建链表和释放
#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
typedef struct data
{
int Data;
struct data *next;
}str;
void Create(str **p)
{
*p=(str*)malloc(sizeof(str));
printf("input the data:");
scanf("%d",&((*p)->Data));
if((*p)->Data!=0)
{
Create(&((*p)->next));
}
else
{
p=NULL;
return;
}
}
void FreeList(data* pList) //释放链表
{
data* temp = pList;
while(temp->Data != 0)
{
pList=pList->next;
free(temp);
temp = NULL;
temp=pList;
}
}
void main()
{
data *p;
Create(&p);
FreeList(p);
}
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cstdlib>
using namespace std;
template<typename T>
class List {
public:
List();
List(T);
~List();
void addToFront(T data); // 把数据加到链表头
void add(T data); // 把数据加到链表尾
void erase(T); // 删除指定的数据
void reverse(); // 返转链表
void sort();
void sort(bool); // 对链表进行排序,指定排序方式
void insert(T); // 把数据插入链表,按链表
bool contains(T) const; // 链表是否包含指定的数据
void print() const; // 打印出链表的元素
private:
// 链表中的结点的结构
struct Node{
T data;
Node* next;
};
Node* const head; // 头结点,能够简化算法,何乐而不用!
bool isIncrease; // 是按升序排列还是按降序排列链表.
};
template<typename T>
List<T>::List(): head(new Node()) { // 为链表头结点分配空间.
head->next = NULL;
this->isIncrease = true;
}
template<typename T>
List<T>::List(T data): head(new Node()) {
head->next = NULL;
this->add(data);
this->isIncrease = true;
}
template<typename T>
List<T>::~List() {
Node* temp;
Node* pre = head;
int count = 0;
while (pre != NULL) {
temp = pre->next;
delete pre;
pre = temp;
//cout << "Destructor times: " << ++count << endl;
}
}
template<typename T>
void List<T>::addToFront(T data) {
Node* temp = new Node();
temp->data = data;
temp->next = head->next;
head->next = temp;
//cout << temp->data << endl;
}
template<typename T>
void List<T>::add(T data) {
Node* tail = head;
// 找到链表最后一个结点.
while (tail->next != NULL) {
tail = tail->next;
//cout << "tail" << endl;
}
Node* temp = new Node();
temp->data = data;
temp->next = NULL;
tail->next = temp;
}
template<typename T>
void List<T>::erase(T data) {
Node* temp = head->next;
Node* pre = NULL;
// 找到指定数据的结点,如果temp == NULL,如没找到.
while (temp != NULL && temp->data != data) {
pre = temp;
temp = temp->next;
}
// temp != NULL,说明找到.
if (temp != NULL) {
pre->next = temp->next;
delete temp;
temp = NULL;
}
}
template<typename T>
void List<T>::reverse() {
if (NULL == head->next) return;
Node* temp = head->next;
Node* post = temp->next;
head->next = NULL;
while (temp != NULL) {
temp->next = head->next;
head->next = temp;
temp = post;
if (post != NULL) {
post = post->next;
}
}
}
template<typename T>
void List<T>::sort() {
this->sort(true);
}
template<typename T>
void List<T>::sort(bool isIncrease) {
this->isIncrease = isIncrease;
// 如果链表除头结点或者只有一个有交数据结点,则不用排序.
if ((NULL == head->next) || (NULL == head->next->next)) {
return;
}
// 链表有两个或两个以上有效数据结点,则排序.
Node* temp = head->next->next; // temp为第二个结点
head->next->next = NULL;
Node* post = temp->next; // post为temp的下一结点
Node* t = NULL; // 与temp进行比较,在链表中找到合适位置的结点指针.
if (isIncrease) { // 按升序排序, 用插入法排序.
while (temp != NULL) {
t = head;
//**这里的取&&的顺序不能反,否则出错.
// 找到比temp大的结点t,即把temp插入t的前面.
while ((t->next != NULL) && (t->next->data < temp->data)) {
t = t->next;
}
temp->next = t->next;
t->next = temp;
//cout <<"sort" << endl;
temp = post;
if (post != NULL) {
post = post->next;
}
}
} else { // 按降序排序
while (temp != NULL) {
t = head;
while ((t->next != NULL) && (t->next->data > temp->data)) {
t = t->next;
}
temp->next = t->next;
t->next = temp;
//cout <<"sort" << endl;
temp = post;
if (post != NULL) {
post = post->next;
}
}
}
}
template<typename T>
void List<T>::insert(T data) {
Node* node = new Node();
node->data = data;
Node* temp = head;
if (this->isIncrease) { // 按升序排列的时候的顺序插入结点.
// 找到比node大的结点,插入node在其前面.
while (temp->next != NULL && node->data > temp->next->data) {
temp = temp->next;
}
node->next = temp->next;
temp->next = node;
} else {
while (temp->next != NULL && node->data < temp->next->data) {
temp = temp->next;
}
node->next = temp->next;
temp->next = node;
}
}
template<typename T>
bool List<T>::contains(T data) const {
Node* temp = head->next;
while (temp != NULL) {
if (temp->data == data) {
return true;
}
temp = temp->next;
}
return false;
}
template<typename T>
void List<T>::print() const {
Node* temp = head;
cout << "[";
if (temp->next != NULL) {
// 打印结点直到倒数第二个.
while (temp->next->next != NULL) {
cout << temp->next->data << ", ";
temp = temp->next;
}
}
if (temp->next != NULL) {
// 打印倒数第一个结点
cout << temp->next->data;
}
cout << "]" << endl;
}
int main() {
List<int> lst(23);
for (int i = 11; i > 0; i--) {
//lst.add(i);
}
for (int i = 0; i < 21; i++) {
//lst.add(i);
}
srand(time(0));
for (int i = 0; i < 10; i++) {
lst.add(rand() % 150);
}
lst.print();
//lst.erase(15);
//lst.erase(9);
lst.sort(false);
lst.print();
lst.insert(55);
lst.print();
lst.sort(true);
lst.insert(100);
lst.print();
cout << lst.contains(55);
return 0;
}