【数据结构】链表操作示例

#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm> 

using namespace std;

typedef struct{
	char key[10];
	char name[20];
	int age;
}Data;

typedef struct Node{
	Data nodeData;
	struct Node * nextNode;
}CLType;

CLType * CLAddEnd(CLType * head, Data nodeData){  	//追加结点 
	CLType * node, * htemp;
	if(!(node=(CLType*)malloc(sizeof(CLType)))){
		cout<<"申请内存失败!\n";
		return NULL;
	}
	else{
		node->nodeData=nodeData;
		node->nextNode=NULL;
		if(head==NULL){
			head=node;
			return node;
		}
		htemp=head;
		while(htemp->nextNode!=NULL){
			htemp=htemp->nextNode;
		}
		htemp->nextNode=node;
		return head;
	}
}

CLType * CLAddFirst(CLType * head, Data nodeData){
	CLType *node;
	if(!(node=(CLType*)malloc(sizeof(CLType)))){
		cout<<"申请内存失败!\n";
		return NULL;
	}
	else{
		node->nodeData=nodeData;
		node->nextNode=head;
		head=node;
	}
} 

CLType * CLFindNode(CLType * head, char *key){
	CLType *htemp;
	htemp=head;
	while(htemp){
		if(strcmp(htemp->nodeData.key, key)==0){
			return htemp;
		}
		htemp=htemp->nextNode;
	}
	return NULL;
}

CLType * CLInsertNode(CLType * head, char * findkey, Data nodeData){
	CLType * node, * nodetemp;
	if(!(node=(CLType *)malloc(sizeof(CLType)))){
		cout<<"内存申请失败!\n";
		return 0;
	}
	node->nodeData=nodeData;
	nodetemp=CLFindNode(head, findkey);
	if(nodetemp){
		node->nextNode=nodetemp->nextNode;
		nodetemp->nextNode=node;
	}
	else{
		cout<<"未找到正确的插入位置!\n";
		free(node);
	}
	return head;
}

int CLDeleteNode(CLType * head, char * key){
	CLType * node, * htemp;
	htemp=head;
	node=head;
	while(htemp){
		if(strcmp(htemp->nodeData.key, key )==0){
			node->nextNode=htemp->nextNode;
			free(htemp);
			return 1;
		}
		else{
			node= htemp;
			htemp = htemp -> nextNode;
		}
		return 0;
	}
}

int CLLength(CLType * head){
	CLType * htemp;
	int Len=0;
	htemp=head;
	while(htemp){
		Len++;
		htemp=htemp->nextNode;
	}
	return Len;
}

void CLAllNode(CLType *  head){
	CLType * htemp;
	Data nodeData;
	htemp = head;
	cout<<"当前链表共有"<<CLLength(head)<<"个结点。链表所以数据如下:\n";
	while(htemp){
		nodeData=htemp->nodeData;
		cout<<"结点"<<nodeData.key<<nodeData.name<<nodeData.age<<endl;
		htemp=htemp->nextNode; 
	} 
}

int main(){
	CLType *node, *head=NULL;
	Data nodeData;
	char key[10], findkey[10];
	cout<<" 链表测试。请输入链表中的数据,格式为:关键字 姓名 年龄\n";
	do{
		fflush(stdin);
		cin>>nodeData.key;
		if(strcmp(nodeData.key, "0")==0){
			break;
		}
		else{
			cin>>nodeData.name>>nodeData.age;
			head=CLAddEnd(head, nodeData);
		}
	}while(1);
	
	CLAllNode(head);
	
	cout<<"\n 演示插入结点,输入插入结点的关键字: ";
	cin>>findkey;
	cout<<"输入插入结点的数据(关键字 姓名 年龄):";
	cin>>nodeData.key>>nodeData.name>>nodeData.age;
	head=CLInsertNode(head, findkey, nodeData);
	CLAllNode(head);
	
	cout<<"\n演示删除结点,输入要删除的关键字:";
	fflush(stdin);
	cin>>key;
	CLDeleteNode(head, key);
	CLAllNode(head);
	
	cout<<"\n 演示在链表中查找,输入查找关键字:";
	fflush(stdin);
	cin>>key;
	node=(CLFindNode(head, key));
	if(node){
		nodeData=node->nodeData;
		cout<<"关键字"<<key<<"对应的结点为:"<<nodeData.key<<nodeData.name<<nodeData.age; 
	} 
	else{
		cout<<"在链表中未找到关键字为"<<key<<"的结点!"<<endl; 
	}
} 

  

posted @ 2015-11-30 19:51  Dragonir  阅读(390)  评论(0编辑  收藏  举报