【问题标题】:C++ Struct node, how insert string and removing specific node?C ++ Struct节点,如何插入字符串和删除特定节点?
【发布时间】:2013-11-24 04:52:39
【问题描述】:

任何人都可以帮助我的代码? 我无法插入带空格的字符串文本。 除此之外..删除最后一个节点的删除功能效果不佳.. 基本上这是一个双向链表,在一个节点中存储 3 个元素,即 2 个字符串和 1 个整数。它需要用户输入每个元素并将其放入一个节点中。 *如果我需要在结构节点中声明字符串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;
            }

    }
} 

【问题讨论】:

  • RemoveNodeAt() 有什么问题?

标签: c++


【解决方案1】:

要将带空格的字符串作为输入,可以使用getline

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

【讨论】:

  • printf("插入客户姓名:");标准::字符串名称; std::getline(std::cin, 名称); (插入节点->CusName,name);我试过这个..但最终输出它显示一些奇怪的符号。还是我写错了?或者当我把它插入节点时出错了?
  • 谢谢..嗯..有什么想法吗?我是否正确定位了节点?我的意思是这个 (insertnode->CusName,name);
【解决方案2】:

你可以使用gets();还可以获取带空格的字符串。 你能详细说明删除节点的问题吗?

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

【讨论】:

  • 我的代码现在已经运行了..现在只留下一个错误..删除第一个节点时..它有错误...我可以把我的代码发给你,你帮我检查一下吗?
  • 当然。发送至 iqbal.muddassir@gmail.com。请务必提及您的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多