2013-11-24 37 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]。 請務必提及您的問題 –