任何人都可以幫助我的代碼? 即時通訊不能插入帶空格的字符串文本。 除此之外,刪除最後一個節點的刪除功能並不能正常工作。 基本上這是一個雙向鏈表,它在一個節點中存儲了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;
}
}
}
什麼是'RemoveNodeAt()'的問題? – theharshest