您的位置:首页 > 其它

单向链表简单实现

2017-10-16 14:14 274 查看
// MyList.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

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

using namespace std;

// data

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

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

// AddTail
CLType *CLAddEnd(CLType *head, Data nodeData)
{
CLType *node,*htemp;

if(!(node = (CLType*)malloc(sizeof(CLType))))
{
printf("malloc false!\n");
return NULL;
}
else
{
node->nodeData = nodeData;
node->nextNode = NULL;

if(head == NULL)
{
head = node;
return head;
}
htemp = head;
while(htemp->nextNode != NULL)
{
htemp=htemp->nextNode;
}
htemp->nextNode = node;
return head;
}
}

// InsertFirst
CLType *CLAddFirst(CLType *head, Data nodeData)
{
CLType *node = NULL;   // init

if(!(node == (CLType *)malloc(sizeof(CLType))))
{
printf("AddFirst malloc false\n");
return NULL;
}
else
{
node->nodeData = nodeData;
node->nextNode = head;
head = node;
return head;
}
}

// Find Key
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;
}

// Insert Node
CLType *CLInsertNode(CLType *head, char *findkey, Data nodeData)
{
CLType *node = NULL, *nodetemp = NULL;

if(!(node = (CLType*)malloc(sizeof(CLType))))
{
printf("malloc false!\n");
return NULL;
}
node->nodeData = nodeData;
nodetemp = CLFindNode(head, findkey);

if(nodetemp)
{
node->nextNode = nodetemp->nextNode;
nodetemp->nextNode = node;
}
else
{
printf("%s Not Found\n", findkey);
free(node);
}
return head;
}

// DelNode
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;
}

// CL length
int CLLength(CLType *head)
{
CLType *htemp = NULL;
int Len = 0;
htemp = head;

while(htemp)
{
Len++;
htemp = htemp->nextNode;
}
return Len;
}

// Show ALL
void CLAllNode(CLType *head)
{
CLType *htemp;
Data nodeData;
htemp = head;
printf("having %d node show All \n", CLLength(head));

while(htemp)
{
nodeData = htemp->nodeData;
printf("Node(%s,%s,%d)\n", nodeData.key, nodeData.name, nodeData.age);
htemp = htemp->nextNode;
}
}

int main()
{
CLType *node, *head = NULL;
Data nodeData;
char key[10], findkey[10];

cout<<"test CList enter list init data format like: key name age"<<endl;

do
{
fflush(stdin);
scanf("%s", nodeData.key);

if(strcmp(nodeData.key, "0") == 0)
{
break;
}
else
{
scanf("%s%d", nodeData.name, &nodeData.age);
head = CLAddEnd(head, nodeData);
}
}while(1);

CLAllNode(head);

printf("inset Data, enter insert find key\n");
fflush(stdin);
//scanf("%s", findkey);
cin>>findkey;
cout<<"findkey"<<findkey<<endl;
cout<<"enter insert data format like: key name age"<<endl;
scanf("%s%s%d", nodeData.key, nodeData.name, &nodeData.age);
//cin>>nodeData.key>>nodeData.name>>nodeData.age;
cout<<nodeData.key<<nodeData.name<<nodeData.age<<endl;
head = CLInsertNode(head, findkey, nodeData);
CLAllNode(head);

cout<<"delete enter you want delete key: "<<endl;
scanf("%s", key);
//cin>>key;
CLDeleteNode(head, key);
CLAllNode(head);

cout<<"find enter you want find key:"<<endl;
scanf("%s", key);
//cin>>key;
node = CLFindNode(head, key);
if(node)
{
nodeData = node->nodeData;
cout<<"key"<<nodeData.key<<"name"<<nodeData.name<<"age"<<nodeData.age<<endl;
}
else
{
cout<<"CList not found"<<endl;
}

return 0;
}

test:

test CList enter list init data format like: key name age
1 test 1
2 test 2
3 test 3
4 tom 4
0
having 4 node show All
Node(1,test,1)
Node(2,test,2)
Node(3,test,3)
Node(4,tom,4)
inset Data, enter insert find key
3
findkey3
enter insert data format like: key name age
6 test 6
6test6
having 5 node show All
Node(1,test,1)
Node(2,test,2)
Node(3,test,3)
Node(6,test,6)
Node(4,tom,4)
delete enter you want delete key:
3
having 4 node show All
Node(1,test,1)
Node(2,test,2)
Node(6,test,6)
Node(4,tom,4)
find enter you want find key:
4
key4nametomage4
Press any key to continue
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: