您的位置:首页 > 理论基础 > 数据结构算法

数据结构----单向链表之 新建-插入-删除-排序(选择法)-合并-删除-销毁

2014-08-29 21:23 501 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐