/****** dlist.h *****/
using namespace std;
#include <string>
struct _node{
_node* prev;
_node* next;
string* data;
int len;
};
typedef _node Node;
int NodeFind(Node* node,string* data,int len);
int NodeInsert(Node* node,string* data,int len);
int NodeDelete(Node* node,string* data,int len);
void NodePrint(Node* node);
/*** dlist.cpp****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include "dlist.h"
using namespace std;
int NodeFind(Node* node,string* data,int len)
{
int iret = 0; int node_find_num = 0;
Node* tmpnode = node;
while(tmpnode->next)
{
tmpnode = tmpnode->next;
if (tmpnode->data->compare(*data)==0)
{
node_find_num++;
}
}
if(node_find_num>0)
return 0;
else
return -1;
}
int NodeInsert(Node* node,string* data,int len)
{
Node* tmpnode = node;
while(tmpnode->next)
{
tmpnode = tmpnode->next;
}
tmpnode->next = new Node;
tmpnode->next->data = new string;
if(tmpnode->next->data)
{
tmpnode->next->data->assign(*data);
tmpnode->next->prev = tmpnode;
tmpnode->next->next = 0;
tmpnode->next->len = len;
return 0;
}
return -1;
}
int NodeDelete(Node* node,string* data,int len)
{
int iret = 0; int node_find_num = 0;
Node* tmpnode = node;
while(tmpnode->next)
{
tmpnode = tmpnode->next;
if(tmpnode->data->compare(*data)==0)
{
node_find_num++;
tmpnode->prev->next = tmpnode->next;
delete tmpnode->data;
delete tmpnode;
iret++; // Find the item and delete succeed;
}
}
if(iret==node_find_num)
return 0;
else
return -1; // dont find the item;
}
void NodePrint(Node* node)
{
Node* tmpnode = node;
int i=1;
while(tmpnode->next)
{
tmpnode = tmpnode->next;
std::cout<<i<<". "<<*tmpnode->data<<std::endl ;
i++;
}
}
/**** test.cpp *****/
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
#include "dlist.h"
int main(int argc,char* argv[])
{
Node* dlist = new Node;
dlist->next = 0;
dlist->prev = 0;
/*
for(int i=0;i<100;i++)
{
char in[4]={0};
itoa(i,in,10);
NodeInsert(dlist,in,4);
}
*/
std::cout<< "Please pick a choice\n";
std::cout<< "1.Insert A Item\n2.Delete A Item\n3.Find A Item\n4.Print the List\nEnter '0' for Quit \n";
int choice;
while(std::cin>>choice)
{
if(choice==1)
{
std::cout<<"Please input the item you want to insert\n";
string in;
std::cin>>in;
if(!NodeInsert(dlist,&in,in.length()))
std::cout<<"Insert Item OK \n";
else
std::cout<<"Something happened,Insert Item Failed \n";
}
else if(choice==2)
{
if(dlist->next==0)
std::cout<<"You Dont have any item yet,Please Insert Item before you want to delete\n";
else
{
std::cout<<"Please input the item you want to delete\n";
string in;
std::cin>>in;
if(!NodeDelete(dlist,&in,in.length()))
std::cout<<"Delete Item OK\n";
else
std::cout<<"Something happened,Delete Item Failed \n";
}
}
else if(choice==3)
{
if(dlist->next==0)
std::cout<<"You Dont have any item yet,Please Insert Item before you want to delete\n";
else
{
std::cout<<"Please input the item you want to Find\n";
string in;
std::cin>>in;
if(!NodeFind(dlist,&in,in.length()))
std::cout<<"Find Item OK\n";
else
std::cout<<"Didnt Find the Item\n";
}
}
else if(choice==4)
{
if(dlist->next==0)
std::cout<<"You Dont have any item yet,Please Insert Item before print\n";
else
NodePrint(dlist);
}
else
std::cout<<"The Option you Input dont exist\n";
}
return 1;}