数据结构----单向链表之 新建-插入-删除-排序(选择法)-合并-删除-销毁
来源:互联网 发布:vp矢量化软件 编辑:程序博客网 时间:2024/05/17 20:28
#include<iostream>
#include<string>
#include<time.h>
#include<malloc.h>
#define N 255
#define PHONE_NUM_LIMIT 11
#define NAME_LIMIT 4
#define MALE 1
using namespace std;
typedef struct data{
int index;
string name;
char sex;
string phone;
string locatiaon;
}UserAcount;
#ifndef LIST_INFO
typedef struct list_info{
int info_type;
UserAcount userinfo;
struct list_info *next;
}LIST_INFO;
#else
struct list_info{
int info_type;
UserAcount userinfo;
struct list_info *next;
}LIST_INFO;
#endif
LIST_INFO * create_empty_list(){
LIST_INFO *lis;
lis=new LIST_INFO;
if( NULL == lis )
cout<<"内存申请失败!"<<std::endl;
else{
lis->next = NULL;
}
return lis;
}
void print_list(LIST_INFO *list){
LIST_INFO *p;
cout<<"info_type\t"<<"userinfo.index\t"<<"userinfo.name\t"<<
"userinfo.sex\t"<<"userinfo.phone\t"<<"userinfo.locatiaon\t\n";
for(p = list->next;p != NULL;p = p->next){
cout<<p->info_type <<"\t"<< p->userinfo.index <<"\t"<< p->userinfo.name<<"\t" <<
p->userinfo.sex<<"\t"<<p->userinfo.phone<<"\t"<<p->userinfo.locatiaon<<"\t"<<endl;
}
delete p;
}
/*************************************************************************
* 创建链表:准备、指向、移动
************************************************************************/
LIST_INFO *create_list(LIST_INFO *list,int count){
LIST_INFO *pnext,*pcurrent=list;
string location,name;
string phone;
for( int i =0; i < count; i++){
pnext = new LIST_INFO;
if( NULL == pnext){
cout<<"内存申请失败"<<endl;
}else{
pnext->userinfo.locatiaon = pnext->userinfo.name = pnext->userinfo.phone ;
cout<<"请输入信息类型、信息实体"<<endl;
cin>>pnext->info_type >> pnext->userinfo.index >> pnext->userinfo.name
>>pnext->userinfo.sex >> pnext->userinfo.phone >> pnext->userinfo.locatiaon;
pnext->next = NULL;
pcurrent->next = pnext;
pcurrent = pnext;
}
}
return list;
}
/**********************************************************************************
* 插入链表:准备、指向、移动
*********************************************************************************/
LIST_INFO *insert_list(LIST_INFO *list,LIST_INFO data,int location){
LIST_INFO *p = list,*temp;
int j = 0;
unsigned int lislen = 0;
temp = new LIST_INFO;
if(temp == NULL)
cout<<"分配内存失败";
else{
temp->info_type = data.info_type;
temp->userinfo.index = data.userinfo.index;
temp->userinfo.name = data.userinfo.name;
temp->userinfo.sex = data.userinfo.sex;
temp->userinfo.phone = data.userinfo.phone;
temp->userinfo.locatiaon = data.userinfo.locatiaon;
temp->next = NULL;
}
if(location == 1 || location == 0){//插入在表头
temp->next = list->next;
p->next = temp;
}else{
while(j < location-1 && p->next != NULL ){
p = p->next;
j++;
}if(j >= location-1){//插在中间
temp->next = p->next;
p->next = temp;
}else{//插在尾部
p->next = temp;
}
}
return list;
}
/*********************************************************************************
* 删除元素:准备、指向、移动
********************************************************************************/
bool delete_element(LIST_INFO *header,int index){
LIST_INFO *pcurrent,*ptemp;
int j = 0 ;
if(NULL == header){
cout << "空链表,不能删除任何数据,立即返回"<<endl;
return false;
}else{
pcurrent=header;
while(pcurrent->userinfo.index != index && pcurrent->next != NULL ){
ptemp = pcurrent;
pcurrent = pcurrent->next;
}
if(pcurrent->userinfo.index == index){
if(pcurrent == header)
header = pcurrent->next;
else
ptemp->next = pcurrent->next;
return true;
}else{
cout << "该数据不存在"<<endl;
return false;
}
}
}
/************************************************************************
* 链表合并:将升序链表La,Lb,合并,并输出到升序Loutput
************************************************************************/
LIST_INFO *Merge_list(LIST_INFO *La,LIST_INFO *Lb,LIST_INFO *Loutput){
LIST_INFO *pa = La->next;
LIST_INFO *pb = Lb->next;
LIST_INFO *pc;
Loutput = pc = La;
while(pa != NULL&& pb != NULL ){
if(pa->userinfo.index < pb->userinfo.index){
pc->next = pa;pc = pc->next; pa = pa->next;
}else{
pc->next = pb;pc = pc->next; pb = pb->next;
}
}
pc->next = pa?pa:pb;
return Loutput;
}
/*****************************************************************
* 链表内选择排序:按升序排序
*****************************************************************/
void SelectSort_list(LIST_INFO * L){
LIST_INFO *p,*q,*small;
LIST_INFO temp ;
for(p = L->next; p->next != NULL; p = p->next) {
small = p;
for(q = p->next; q ; q = q->next) {
if(q->userinfo.index < small->userinfo.index ){
small = q;
}
}
if(small != p) {
temp.info_type = p->info_type;
temp.userinfo = p->userinfo;
p->info_type = small->info_type;
p->userinfo = small->userinfo;
small->info_type = temp.info_type;
small->userinfo = temp.userinfo;
}
}
return ;
}
/************************************************************
* 删除链表:链表结构还存在
**********************************************************/
bool delete_list(LIST_INFO *list){
LIST_INFO *pcurrent,*p = list;
while(p != NULL){
pcurrent = p->next;
delete p;
p = pcurrent;
}
list->next = NULL;
return true;
}
/**********************************************************************************
* 销毁链表,链表结构不存在(ps:Debug时可以看到指针情形)
*********************************************************************************/
bool destory_list(LIST_INFO *list){
LIST_INFO *p;
if(NULL == list)
return true;
else{
while(list != NULL )
p = list->next;
delete list;
list = p;
}
return true;
}
/*****************************************************************************
* 反转链表
*****************************************************************************/
LIST_INFO *reverse_list(LIST_INFO *list){
LIST_INFO *pcurrent,*p,*ptmp;
p = list;
pcurrent = p->next;
while(pcurrent->next != NULL){
ptmp = pcurrent->next;
pcurrent->next = p;
p = pcurrent;
pcurrent = ptmp;
}
pcurrent->next = p;
list->next->next = NULL;
list->next = pcurrent;
return list;
}
int main(int argc,char *argv[]){
LIST_INFO *head,*head2,*Loutput,node,node2;
int n,x,c;
node.info_type = 1;
node.userinfo.index = 06;
node.userinfo.name ="王三";
node.userinfo.sex = 'M';
node.userinfo.phone = "15*********";
node.userinfo.locatiaon = "Tokyo hot";
head=create_empty_list();
cout<<"请输入链表的节点个数n=";
cin>>n;
head = create_list(head,n);
cout<<"链表信息:\n";
print_list(head);
cout<<"请输入要插入的位置:\n";
cin >> x;
head = insert_list(head,node,x);
print_list(head);
cout << "待删除的数据号是:"<< endl;
cin >> c;
bool status = delete_element(head,c);
if(status == true)
cout << "delete data success" << endl;
else
cout << "delte data error" <<endl;
cout << "after delete data" <<endl;
print_list(head);
head = reverse_list(head);
cout << "after reverse" <<endl;
print_list(head);
SelectSort_list(head);
cout << "after sort" <<endl;
print_list(head);
/*********************************/
node2.info_type = 1;
node2.userinfo.index = 06;
node2.userinfo.name ="王四";
node2.userinfo.sex = "F";
node2.userinfo.phone = "13*********";
node2.userinfo.locatiaon = "Yale";
head2=create_empty_list();
cout<<"请输入链表的节点个数n=";
head2 = create_list(head2,n);
cout<<"链表信息:\n";
print_list(head2);
cout<<"请输入要插入的位置:\n";
head2 = insert_list(head2,node2,x);
print_list(head2);
cout << "待删除的数据号是:"<< endl;
status = delete_element(head2,c);
if(status == true)
cout << "delete data success" << endl;
else
cout << "delte data error" <<endl;
cout << "after delete data" <<endl;
print_list(head2);
head2 = reverse_list(head2);
cout << "after reverse" <<endl;
print_list(head2);
SelectSort_list(head2);
cout << "after sort" <<endl;
print_list(head2);
Loutput = new LIST_INFO;
Loutput = Merge_list(head,head2,Loutput);
cout << "after merge" <<endl;
print_list(Loutput);
delete_list(Loutput);
cout << "after delete" <<endl;
destory_list(Loutput);
cout << "*********************" <<endl;
return 0;
}
#include<string>
#include<time.h>
#include<malloc.h>
#define N 255
#define PHONE_NUM_LIMIT 11
#define NAME_LIMIT 4
#define MALE 1
using namespace std;
typedef struct data{
int index;
string name;
char sex;
string phone;
string locatiaon;
}UserAcount;
#ifndef LIST_INFO
typedef struct list_info{
int info_type;
UserAcount userinfo;
struct list_info *next;
}LIST_INFO;
#else
struct list_info{
int info_type;
UserAcount userinfo;
struct list_info *next;
}LIST_INFO;
#endif
LIST_INFO * create_empty_list(){
LIST_INFO *lis;
lis=new LIST_INFO;
if( NULL == lis )
cout<<"内存申请失败!"<<std::endl;
else{
lis->next = NULL;
}
return lis;
}
/**************************************************************************
* 遍历输出
*************************************************************************/void print_list(LIST_INFO *list){
LIST_INFO *p;
cout<<"info_type\t"<<"userinfo.index\t"<<"userinfo.name\t"<<
"userinfo.sex\t"<<"userinfo.phone\t"<<"userinfo.locatiaon\t\n";
for(p = list->next;p != NULL;p = p->next){
cout<<p->info_type <<"\t"<< p->userinfo.index <<"\t"<< p->userinfo.name<<"\t" <<
p->userinfo.sex<<"\t"<<p->userinfo.phone<<"\t"<<p->userinfo.locatiaon<<"\t"<<endl;
}
delete p;
}
/*************************************************************************
* 创建链表:准备、指向、移动
************************************************************************/
LIST_INFO *create_list(LIST_INFO *list,int count){
LIST_INFO *pnext,*pcurrent=list;
string location,name;
string phone;
for( int i =0; i < count; i++){
pnext = new LIST_INFO;
if( NULL == pnext){
cout<<"内存申请失败"<<endl;
}else{
pnext->userinfo.locatiaon = pnext->userinfo.name = pnext->userinfo.phone ;
cout<<"请输入信息类型、信息实体"<<endl;
cin>>pnext->info_type >> pnext->userinfo.index >> pnext->userinfo.name
>>pnext->userinfo.sex >> pnext->userinfo.phone >> pnext->userinfo.locatiaon;
pnext->next = NULL;
pcurrent->next = pnext;
pcurrent = pnext;
}
}
return list;
}
/**********************************************************************************
* 插入链表:准备、指向、移动
*********************************************************************************/
LIST_INFO *insert_list(LIST_INFO *list,LIST_INFO data,int location){
LIST_INFO *p = list,*temp;
int j = 0;
unsigned int lislen = 0;
temp = new LIST_INFO;
if(temp == NULL)
cout<<"分配内存失败";
else{
temp->info_type = data.info_type;
temp->userinfo.index = data.userinfo.index;
temp->userinfo.name = data.userinfo.name;
temp->userinfo.sex = data.userinfo.sex;
temp->userinfo.phone = data.userinfo.phone;
temp->userinfo.locatiaon = data.userinfo.locatiaon;
temp->next = NULL;
}
if(location == 1 || location == 0){//插入在表头
temp->next = list->next;
p->next = temp;
}else{
while(j < location-1 && p->next != NULL ){
p = p->next;
j++;
}if(j >= location-1){//插在中间
temp->next = p->next;
p->next = temp;
}else{//插在尾部
p->next = temp;
}
}
return list;
}
/*********************************************************************************
* 删除元素:准备、指向、移动
********************************************************************************/
bool delete_element(LIST_INFO *header,int index){
LIST_INFO *pcurrent,*ptemp;
int j = 0 ;
if(NULL == header){
cout << "空链表,不能删除任何数据,立即返回"<<endl;
return false;
}else{
pcurrent=header;
while(pcurrent->userinfo.index != index && pcurrent->next != NULL ){
ptemp = pcurrent;
pcurrent = pcurrent->next;
}
if(pcurrent->userinfo.index == index){
if(pcurrent == header)
header = pcurrent->next;
else
ptemp->next = pcurrent->next;
return true;
}else{
cout << "该数据不存在"<<endl;
return false;
}
}
}
/************************************************************************
* 链表合并:将升序链表La,Lb,合并,并输出到升序Loutput
************************************************************************/
LIST_INFO *Merge_list(LIST_INFO *La,LIST_INFO *Lb,LIST_INFO *Loutput){
LIST_INFO *pa = La->next;
LIST_INFO *pb = Lb->next;
LIST_INFO *pc;
Loutput = pc = La;
while(pa != NULL&& pb != NULL ){
if(pa->userinfo.index < pb->userinfo.index){
pc->next = pa;pc = pc->next; pa = pa->next;
}else{
pc->next = pb;pc = pc->next; pb = pb->next;
}
}
pc->next = pa?pa:pb;
return Loutput;
}
/*****************************************************************
* 链表内选择排序:按升序排序
*****************************************************************/
void SelectSort_list(LIST_INFO * L){
LIST_INFO *p,*q,*small;
LIST_INFO temp ;
for(p = L->next; p->next != NULL; p = p->next) {
small = p;
for(q = p->next; q ; q = q->next) {
if(q->userinfo.index < small->userinfo.index ){
small = q;
}
}
if(small != p) {
temp.info_type = p->info_type;
temp.userinfo = p->userinfo;
p->info_type = small->info_type;
p->userinfo = small->userinfo;
small->info_type = temp.info_type;
small->userinfo = temp.userinfo;
}
}
return ;
}
/************************************************************
* 删除链表:链表结构还存在
**********************************************************/
bool delete_list(LIST_INFO *list){
LIST_INFO *pcurrent,*p = list;
while(p != NULL){
pcurrent = p->next;
delete p;
p = pcurrent;
}
list->next = NULL;
return true;
}
/**********************************************************************************
* 销毁链表,链表结构不存在(ps:Debug时可以看到指针情形)
*********************************************************************************/
bool destory_list(LIST_INFO *list){
LIST_INFO *p;
if(NULL == list)
return true;
else{
while(list != NULL )
p = list->next;
delete list;
list = p;
}
return true;
}
/*****************************************************************************
* 反转链表
*****************************************************************************/
LIST_INFO *reverse_list(LIST_INFO *list){
LIST_INFO *pcurrent,*p,*ptmp;
p = list;
pcurrent = p->next;
while(pcurrent->next != NULL){
ptmp = pcurrent->next;
pcurrent->next = p;
p = pcurrent;
pcurrent = ptmp;
}
pcurrent->next = p;
list->next->next = NULL;
list->next = pcurrent;
return list;
}
int main(int argc,char *argv[]){
LIST_INFO *head,*head2,*Loutput,node,node2;
int n,x,c;
node.info_type = 1;
node.userinfo.index = 06;
node.userinfo.name ="王三";
node.userinfo.sex = 'M';
node.userinfo.phone = "15*********";
node.userinfo.locatiaon = "Tokyo hot";
head=create_empty_list();
cout<<"请输入链表的节点个数n=";
cin>>n;
head = create_list(head,n);
cout<<"链表信息:\n";
print_list(head);
cout<<"请输入要插入的位置:\n";
cin >> x;
head = insert_list(head,node,x);
print_list(head);
cout << "待删除的数据号是:"<< endl;
cin >> c;
bool status = delete_element(head,c);
if(status == true)
cout << "delete data success" << endl;
else
cout << "delte data error" <<endl;
cout << "after delete data" <<endl;
print_list(head);
head = reverse_list(head);
cout << "after reverse" <<endl;
print_list(head);
SelectSort_list(head);
cout << "after sort" <<endl;
print_list(head);
/*********************************/
node2.info_type = 1;
node2.userinfo.index = 06;
node2.userinfo.name ="王四";
node2.userinfo.sex = "F";
node2.userinfo.phone = "13*********";
node2.userinfo.locatiaon = "Yale";
head2=create_empty_list();
cout<<"请输入链表的节点个数n=";
head2 = create_list(head2,n);
cout<<"链表信息:\n";
print_list(head2);
cout<<"请输入要插入的位置:\n";
head2 = insert_list(head2,node2,x);
print_list(head2);
cout << "待删除的数据号是:"<< endl;
status = delete_element(head2,c);
if(status == true)
cout << "delete data success" << endl;
else
cout << "delte data error" <<endl;
cout << "after delete data" <<endl;
print_list(head2);
head2 = reverse_list(head2);
cout << "after reverse" <<endl;
print_list(head2);
SelectSort_list(head2);
cout << "after sort" <<endl;
print_list(head2);
Loutput = new LIST_INFO;
Loutput = Merge_list(head,head2,Loutput);
cout << "after merge" <<endl;
print_list(Loutput);
delete_list(Loutput);
cout << "after delete" <<endl;
destory_list(Loutput);
cout << "*********************" <<endl;
return 0;
}
0 0
- 数据结构----单向链表之 新建-插入-删除-排序(选择法)-合并-删除-销毁
- 数据结构之单向链表操作1-(插入,删除,交换,反转,排序等操作)
- 单向链表的操作:创建,删除,插入,销毁,查找
- c++之链表篇1:单向链表的创建,打印,删除,插入,销毁等基本操作
- 链表操作 (C语言)插入,合并,销毁,删除,冒泡排序
- 数据结构Java实现 --单向链表的插入和删除
- Java数据结构03----单向链表的插入和删除
- 数据结构之线性表(建表-插入-删除-合并)
- 数据结构 图 创建 插入 遍历 删除 销毁
- 数据结构之循环链表操作3-(合并,拆分,插入,删除,建立等)
- 链表的逆置、合并、排序以及插入删除
- 单向链表的插入删除代码
- 单向链表的创建 删除 插入
- 单向链表插入与删除
- 单向循环链表创建、遍历、插入、删除、查找(按位置,按元素值)、清空、销毁
- 大二 第一次数据结构作业 (数组的排序,删除,查找,插入,合并)(线性表)
- 数据结构Java实现【3】----单向链表的插入和删除
- 数据结构Java实现03----单向链表的插入和删除
- javascript--瀑布流
- 2015阿里巴巴秋招在线笔试题
- sdutoj1020递推——神、上帝以及老天爷
- Langton's Ant
- ACdream1187(简单找规律)
- 数据结构----单向链表之 新建-插入-删除-排序(选择法)-合并-删除-销毁
- Codeforces Round #249 (Div. 2) D. Special Grid
- hdu 3886 Final Kichiku “Lanlanshu” (数位dp)
- ubuntu部署android自动编译打包
- 用U盘重装Win7系统方法
- 1070. Mooncake (25)
- 合作开发-实现后台管理框架
- 字节对齐详解
- linux /etc/security/limits.conf的相关说明