string链表,直接上代码,少废话

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

转载于:https://my.oschina.net/u/230229/blog/110001

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值