2013-11-24 28 views
0

任何人都可以帮助我的代码? 即时通讯不能插入带空格的字符串文本。 除此之外,删除最后一个节点的删除功能并不能正常工作。 基本上这是一个双向链表,它在一个节点中存储了3个元素,它是2个字符串和1个整数,它要求用户输入每个元素并且把它放入一个节点。 *如果我需要在struct node中声明字符串Cusname?C++ Struct节点,如何插入字符串并删除特定节点?

#include <stdio.h> 
#include <stdlib.h> 
#include <iostream> 
#include <string> 
using std::string; 


void AddToStart(); 
void RemoveNodeAt(); 
void createlist(); 
void PrintList(); 
void AddToEnd(); 
void menu(); 
int option,num; 
char name[50], tran[200], delname[50]; 

struct node 
{ 
    struct node *previous; 
    char CusName[50]; 
    int Customer_Number; 
    char Trans[200]; 
    struct node *next; 
}*insertnode,*list,*next,*prev,*temp,*tmpdisplay,*del,*Lnode; 


void main() 
    { 
createlist(); 

do 
{ 
    menu(); 
    switch (option) 
    { 
    case 1: AddToStart();break; 
    case 2: AddToEnd();break; 
    case 3: PrintList();break; 
    case 4: RemoveNodeAt();break; 
    case 5: exit(1);break; 
    } 
}while (option !=5); 

} 

void createlist() 
{ 
    list=NULL; 

} 

void menu() 
{ 
printf("\n=====================================================\nCustomers' Transactions\n"); 
printf("1-- Insert at Begining\n"); 
printf("2-- Insert at End\n"); 
printf("3-- Print List\n"); 
printf("4-- Remove a Customer\n"); 
printf("5-- Quit Programe\n"); 
printf("Select your option : "); 
scanf("%d",&option); 
} 
void AddToStart() 
{ 
insertnode=(struct node*) malloc (sizeof(struct node)); 


printf("Insert Customer Name : "); 
scanf("%s",&name); 
strcpy(insertnode->CusName,name); 
printf("Insert Customer Number : "); 
scanf("%d",&num); 
insertnode->Customer_Number=num;  
printf("Enter Customer Transaction Description : \n"); 
scanf("%s",&tran); 
strcpy(insertnode->Trans,tran); 

insertnode->next=NULL; 
insertnode->previous=NULL; 
if (list==NULL) 

    list=insertnode; 

else 
    { 
     insertnode->next=list; 
     list=insertnode; 

} 
} 


void RemoveNodeAt() 
{ 
    printf("Customer to delete : "); 
    scanf("%s",delname); 

    if (list==NULL) 
    printf("\nList is empty \n\n"); 

    else 
    { 

     if (strcmp(delname,list->CusName)==0) //only first node 
      //list=NULL; 
      printf("DONE"); 

     else if (strcmp(delname,Lnode->CusName)==0)//last node 
      Lnode->previous->next =NULL; 

     else 

      del=list; 
      while (strcmp(del->CusName,delname)!=0) 
      { 
       prev=del; 
       del=del->next; 
      } 
      { 
      prev->next=prev->next->next; 
      del->next=del->previous; 
      } 

    } 
} 
+0

什么是'RemoveNodeAt()'的问题? – theharshest

回答

0

采取字符串与空间作为输入,你可以使用getline

http://www.cplusplus.com/reference/string/string/getline/

+0

printf(“插入客户姓名:”); std :: string name; std :: getline(std :: cin,name); (insertnode-> CusName,name); 我试过这个..但最终输出它显示一些奇怪的符号。 或mayb我写错了方式?或者当我错误时插入到节点? – user3026515

+0

你在做什么是正确的,问题现在在别的地方。 – theharshest

+0

谢谢.. 嗯..任何想法?我正确定位节点? 我的意思是这个(insertnode-> CusName,name); – user3026515

0

您可以使用gets();函数以及获取带空格的字符串。 您能否详细说明删除节点的问题。

也可以访问http://basiccodings.blogspot.in/

+0

我的代码现在功能已经..现在只留下一个错误..这是当删除第一个节点..它有错误...我可以发送你我的代码,你帮我检查? – user3026515

+0

当然。请发送至[email protected]。 请务必提及您的问题 –