在链接列表中定义字符串节点

时间:2016-04-19 20:12:04

标签: c++ linked-list singly-linked-list

#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include <ctime>
#include <fstream>
#include <windows.h>
#include <string>
using namespace std;


/* data variable is used to store data as name 
suggests,the "next" is a pointer of the type node
that is used to point to the next node of the 
Linked List*/
/*
* Node Declaration
*/
struct node
{
  string info;
  struct node *next;
}*start;

/*
 * Class Declaration
*/
class single_llist
{
  public:
    node* create_node(string);
    void insert_begin();
    void insert_pos();
    void insert_last(); 
    void delete_pos();
    void sort();
    void search();
    void update();
    void reverse();
    void display();
    single_llist() 
    {
        start = NULL;
    }
};

/*
* Inserting element in beginning
*/
void single_llist::insert_begin()
{
  string value;
  cout<<"Enter the value to be inserted: ";
  cin>>value;
  struct node *temp, *p;
  temp = create_node(value);
  if (start == NULL)
  {
    start = temp;
    start->next = NULL;          
  } 
  else
  {
    p = start;
    start = temp;
    start->next = p;
  }
  cout<<"Element Inserted at beginning"<<endl;
}

我正在用Dev C ++程序开发我的程序。我试图输入特定的单词到txt文件并保存它们。因此我正在处理字符串。程序给出了这个错误:undefined reference to single_llist::create_node(std::string)并显示我这里有错误,temp = create_node(value);我还在研究解决这个问题需要做些什么?

1 个答案:

答案 0 :(得分:0)

感谢@NathanOliver我认为我在没有先创建节点的情况下尝试了错误的方法。在代码片段之后创建节点检查。

/*
* Creating Node
*/
node *single_llist::create_node(string value)
{
struct node *temp, *s;
temp = new(struct node); 
if (temp == NULL)
{
    cout<<"Memory not allocated "<<endl;
    return 0;
}
else
{
    temp->info = value;
    temp->next = NULL;     
    return temp;
}
}