c++单链表的基本操作
来源:互联网 发布:ubuntu源文件在哪 编辑:程序博客网 时间:2024/06/07 06:17
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef struct node{
int value;
struct node* next;
}Node;
typedef struct{
Node* head;
Node* tail;
int size;
}LinkedList;
LinkedList* NewLinkedLIst(){
LinkedList* tList=new LinkedLIst;//!!!LinkedList* tList=0;记住无论什么类型空指针都好,都不配拥有任何属性
/*tList->head=0;
tList->tail=0;如果是这样的话不好插入*/
tList->head=new Node;//!!!!0->next=0是大错特错的
tList->head->next=0;
tList->tail=tList->head;//方便尾插法
tList->size=0;
return tList;
}
bool BubbleSortList(LinkedList*& list){
Node* p=list->head->next;
if(0==p)return false;
if(0==p->next)return true;
Node* tag=0;
for(;p->next;p=p->next){//n-1次冒泡
for(Node* q=p->next;q;q=q->next){
if(tag->value>q->value)tag=q;
}
if(tag==p)continue;
int tmp=p->value;
p->value=tag->value;
tag->value=tmp;
}
return false;
}
void Combine(LinkedList list1,LinkedList list2){
Node* p=list1->tail;
Node* q=list2->head->next;
p->next=q;
}
bool Reverse(LinkedList*& list){//传递指针的引用过来是为了控制这个指针
if(0==list->head->next)return false;
if(0==list->head->next->next)return true;
Node* pre=list->head->next;
Node* mid=list->head-next->next;
Node* pro=mid->next;
while(mid){
mid->next=pre;
pre=mid;
mid=pro;
if(pro)
pro=pro->next;
}
list->head->next->next=0;
list->tail=list->head->next;
list->head->next=pre;
return true;
}
bool Remove(LinkedList*& list,int value){
if(0==list->size)goto END;//转向语句不允许跳同层含初始化区域
{//加了一层无意义的花括号以便使goto能编译过去
Node*p=list->head;
Node*q=list->head->next;
while(q){
if(value==q->value){
p->next=q->next;
if(0==q->next)
list->tail=p;
delete q;
q=0;//请百度野指针
list->size--;
return true;
}
q=q->next;
}
}
END:printf("Did't find this value in the list!\n");
return false;
}
LinkedList* insert(LinkedList* list,int value){
Node* tNode=new Node;//new 的三部曲:申请内存,调用构造函数,返回指针//Node* tNode=new Node();都行
tNode->value=value;
tNode->next=0;
list->tail->next=tNode;
list->size++;
return list;
}
void Print(LinkedList* list){
Node* p=list->head->next;
if(0==p)return;
while(p){
printf("%d ",p->value);
p=p->next;
}
printf("\n");
}
int main(){
LinkedList* lList=0;
lList=NewLinkedList();
lList=Insert(lList,100);
lList=Insert(lList,1);
lList=Insert(lList,10);
lList=Insert(lList,80);
Print(lList);
Remove(lList,99);
Reverse(lList);
Print(lList);
BubbleSortList(lList);
Print(lList);
return 0;
}
- 【C++】单链表的基本操作
- C++-----单链表基本操作
- c语言单链表的基本操作(代码)
- C实现单链表的基本操作笔记
- C语言 单链表的基本操作
- c语言 之 单链表 的 基本操作
- C语言实现单链表的基本操作
- 【C++/数据结构】单链表的基本操作
- C语言单链表的基本操作
- 单链表的基本操作c语言实现
- C语言实现单链表的基本操作
- 【c语言】单链表的基本操作
- 单链表的基本操作C语言实现
- c语言单链表的基本操作
- C的大数基本操作
- C基本的读写操作
- C++---文件的基本操作
- 【C#】特性的基本操作
- 操作系统面试大纲
- Hibernate
- Qt Quick 之 PathView 详解
- Selenium2的入门学习
- HDU 1158 Employment Planning
- c++单链表的基本操作
- 正弦曲线投影及ENVI下Modis数据坐标投影定义
- CRegKey类中的函数open()失败原因
- Eclipse远程调试Tomcat .
- setLayoutParams
- 用于并行计算的多线程数据结构,第 2 部分: 设计不使用互斥锁的并发数据结构
- web项目部署
- "XX cannot be resolved to a type "eclipse报错及解决说明
- 规则dll