h:
#pragma once
#include "pch.h"
namespace list {
class linklist {
public:
linklist init(linklist *);//创建
void head(linklist *);//头插法倒叙
void tail(linklist *);//尾插法顺序
linklist watch(linklist *);//查看
linklist cirwatch(linklist *l);//输出循环链表
int increase(linklist *, int ,char );//增加
int removelocate(linklist *, int);//移除相应坐标值
int removekey(linklist *, char);//移除相应键值
void sort(linklist *);//冒泡排序
linklist combine(linklist* , linklist*);//合并
linklist cirinit(linklist *);//循环列表建立
void cirtail(linklist *);//循环链表尾插
linklist circombine(linklist *, linklist *);//循环链表合并
void doutail(linklist *);//双向链表尾插创建
linklist douwatch(linklist *);//双向链表查看
int douincrease(linklist *, int, char);//双向链表增加
int douiremove(linklist *,int);//双向链表删除
private:
char data;
class linklist *next;
class linklist *prior;
};
}
cpp:
#include"pch.h"
#include "链表.h"
using namespace list;
linklist linklist::init(linklist *h)//*h是头指针
{
h->next = NULL;
cout << "链表初始化成功 " << endl;
return *h;
};
void linklist::head(linklist *h)
{
cout << " 头插法开始" << endl;
linklist *s;
cout << "请输入链表的值 " << endl;
char c;
while (1)
{
c = getchar();
if (c != '\n')
{
s = new linklist;
s->data = c;
s->next = h->next;
h->next = s;
}
else
break;
}
watch(h);
};
void linklist::tail(linklist *h)
{
cout << "尾插法开始" << endl;
linklist *s, *r;//r是尾指针
r = h;
char c;
cout << "请输入链表的值 " << endl;
while (1)
{
c = getchar();
if (c != '\n')
{
s = new linklist;
s->data = c;
r->next = s;
r = s;
}
else
{
r->next = NULL;//最后一个节点置空
break;
}
}
watch(h);
};
int linklist::increase(linklist *l,int x,char y)
{
cout << "增加链表" << endl;
linklist *s; linklist *p;//p用来指向当前节1点
int count = 0;
p = l;
if (l->next == NULL)
{
cout << " 错误,是空链表" << endl;
return 0;
}
loop: cout << " 请输入你要插入的位置" << endl;
cin >> x;
if (x <= 0)
{
cout << " 插入位置不对,请从新输入" << endl;
goto loop;
}
if(x>0)
{
while (p != NULL &&count< x)//找到插入的位置
{
p = p->next;
count++;
}
if (p==NULL)//如果找到了=空
{
cout << "插入位置不对" << endl;
return 0;
}
cout << " 请输入你要插入的值" << endl;
cin >> y;
s = new linklist;
s->data = y;
s->next = p->next;
p->next = s;
cout << "插入成功" << endl;
watch(l);
return 1;
}
};
int linklist::removelocate(linklist *l, int x)
{
cout << "你要删除的位置是:" << endl;
cin >> x;
int count = 0; linklist *p, *r; p = l; r = p->next;
while (p->next != NULL&&count<x)
{
p = p->next;
count++;
}
if (p ->next== NULL||x<1)//如果找到了=空
{
cout << "删除位置不对" << endl;
return 0;
}
r = p->next;
p->next = r->next;
delete(r);
watch(l);
};
int linklist::removekey(linklist *l, char x)
{
cout << "你要删除的值是:" << endl;
cin >> x;
linklist *p, *r; p = l;
while (p->next != NULL&& p->data != x)
{
p = p->next;
}
if (p->next == NULL || x < 1)//如果找到了=空
{
cout << "该值不存在" << endl;
return 0;
}
r = p->next;
p->next = r->next;
delete(r);
watch(l);
return 1;
};
void linklist::sort(linklist *l)
{
cout << "链表开始升序" << endl;
linklist *k = l, *t = l;
while (t->next != NULL)
{
k = l;
while (k->next != NULL)
{
if (k->data > k->next->data)
{
char p;
p = k->data;
k->data = k->next->data;
k->next->data = p;
}
k = k->next;
}
t = t->next;
}
watch(l);
};
linklist linklist::combine(linklist* l0, linklist *l1)
{
cout << "合并链表" << endl;
linklist *l=l0;//先将一个链表赋值进去,防止r=空
linklist *s = l1->next;//不用开辟新的
linklist *q = l0->next;//用来赋值第二个链表
linklist *r; //使用尾插法
r = l;
while(s!=NULL)
{
r->next = s;
r = s;
s = s->next;
}
r->next = NULL;
if (q != NULL)
r->next = q;
sort(l);
return *l;
};
linklist linklist::cirinit(linklist*l)
{
cout << "创建循环链表" << endl;
l->data = '\0';//初始化头节点的值
l->next = l;
return *l;
};
void linklist::cirtail(linklist*l)
{
cout << "尾插法添加循环链表" << endl;
linklist *s, *r;
r = l;
cout << "请输入链表的值 " << endl;
char c;
c=getchar();
while (c != '\n')
{
s = new linklist;
s->data = c;
r->next = s;
r = s;
c = getchar();
}
r->next =l;
};
linklist linklist::circombine(linklist *a,linklist *b)
{
cout << "合并循环链表" << endl;
linklist *l;
l = a->next;
a->next = b->next->next;
delete b->next;
b->next = l;
cirwatch(l);
return *l;
}
linklist linklist::watch(linklist *l)
{
cout << "开始查看链表" << endl;
while (l->next!=NULL)
{
l = l->next;
cout << l->data;
}
return *l;
}
linklist linklist::cirwatch(linklist *l)
{
linklist *p = l;
cout << "开始查看循环链表" << endl;
do {
cout << p->data;
p = p->next;
} while (p != l);
return *p;
};
void linklist::doutail(linklist *l)
{
cout << "双向链表尾插法开始请输入:" << endl;
linklist *r, *s;
l->prior = l->next = NULL;
r = l;
char c;
c=getchar();
while (c != '\n')
{
s = new linklist;
s->data = c;
r->next = s;
s->prior = r;
r = r->next;
c=getchar();
}
r->next = NULL;
watch(l);
};
int linklist::douincrease(linklist *l, int x, char y)
{
cout << "增加链表" << endl;
linklist *s; linklist *p;//p用来指向当前节1点
int count = 0;
p = l;
if (l->next == NULL)
{
cout << " 错误,是空链表" << endl;
return 0;
}
loop: cout << " 请输入你要插入的位置" << endl;
cin >> x;
if (x <= 0)
{
cout << " 插入位置不对,请从新输入" << endl;
goto loop;
}
if (x > 0)
{
while (p != NULL && count < x)//找到插入的位置
{
p = p->next;
count++;
}
if (p == NULL)//如果找到了=空
{
cout << "插入位置不对" << endl;
return 0;
}
cout << " 请输入你要插入的值" << endl;
cin >> y;
s = new linklist;
s->data = y;
s->prior = p->prior;
p->prior->next = s;
s->next = p;
p->prior = s;
cout << "插入成功" << endl;
watch(l);
return 1;
}
}
int linklist::douiremove(linklist *l, int x)
{
cout << "你要删除的位置是:" << endl;
cin >> x;
int count = 0; linklist *p, *r; p = l; r = p->next;
while (p->next != NULL && count < x )
{
p = p->next;
count++;
}
if (p->next == NULL || x < 1)//如果找到了=空
{
cout << "删除位置不对" << endl;
return 0;
}cout << count << endl;
p->prior->next = p->next;
p->next->prior = p->prior;
delete(p);
watch(l);
}