首页 技术 正文
技术 2022年11月9日
0 收藏 740 点赞 4,020 浏览 1976 个字
/*单链表操作*/
#include <iostream>
using namespace std;class Node{
public:
Node(){
next=0;
}
Node(int el, Node *ptr=0)
{
info=el;
next=ptr;
}
int info;
Node *next;
};class LList{
public:
LList(){head=tail=0;}
~LList();
int isEmpty(){return head==0;}
void addToHead(int);
void addToTail(int);
int deleteHead();
int deleteTail();
void deleteNode(int);
bool isInList(int)const;
void displayall() const;
void inverted();
private:
Node *head, *tail;
};LList::~LList(){
for(Node *p;!isEmpty();){
p=p->next;
delete head;
head=p;
}
}void LList::addToHead(int el){
head=new Node(el,head);
if(tail==0)
tail=head;
}void LList::addToTail(int el){
if(tail!=0){
tail->next=new Node(el);
tail=tail->next;
}
else head=tail=new Node(el);
}int LList::deleteHead(){
int el=head->info;
Node *tmp=head;
if(head==tail)
head=tail=0;
else head=head->next;
delete tmp;
return el;
}int LList::deleteTail(){
int el=tail->info;
if(head==tail) {
delete tail;
head=tail=0;
}
else{
Node *tmp=NULL;
for(tmp=head;tmp->next!=tail;tmp=tmp->next);
delete tail;
tail=tmp;
tail->next=0;
}
return el;
}
void LList::deleteNode(int el){
if(head!=0){
if(head==tail && el == head->info)
{ delete head;
head=tail=0;
}
else if(el ==head->info)
{Node *tmp=head;
head=head->next;
delete tmp;
}
else{
Node *pred,*tmp;
for(pred=head,tmp=head->next;tmp!=0 && (tmp->info==el);pred=pred->next,tmp=tmp->next);
if(tmp!=0){
pred->next=tmp->next;
if(tmp==tail)
tail=pred;
delete tmp;
}
}
    }
}bool LList::isInList(int el) const{
Node *tmp;
for(tmp=head;tmp!=0 && !(tmp->info==el);tmp=tmp->next);
return tmp!=0;
}
void LList::displayall() const{
    Node *tmp;
for(tmp=head;tmp!=0;tmp=tmp->next)
{cout<<tmp->info<<"-";}
cout<<endl;
}void LList::inverted(){
Node *pre=NULL;
Node *next=NULL;
tail=head;while(head!=NULL)
{
next=head->next;
head->next=pre;
pre=head;
head=next;}
head=pre;
}int main()
{
LList *list= new LList();
list->addToTail(1);
list->addToTail(2);
list->addToTail(3);
list->addToTail(4);
list->addToHead(0);
list->addToTail(4);
list->addToTail(5);
list->addToTail(6);
list->addToTail(7);
list->addToTail(8);
list->displayall();list->inverted();
list->displayall();return 0;
}

输出结果:

0-1-2-3-4-4-5-6-7-8-
8-7-6-5-4-4-3-2-1-0-

  

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918