任何人都可以帮助我的代码? 我无法插入带空格的字符串文本。 除此之外..删除最后一个节点的删除功能不能正常工作.. 基本上这是一个双向链表,在一个节点中存储3个元素,即2个字符串和1个整数。它要求用户输入每个元素并将其放入节点。 *如果我需要在struct node中声明字符串Cusname?
#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 :(得分:0)
要将带空格的字符串作为输入,您可以使用getline
答案 1 :(得分:0)
你可以使用gets();函数以获取带空格的字符串。 你能否详细说明删除节点的问题。