C++ 我一直在主函数的左括号中发现一个错误,函数'zn1linked_listC1Ev';'中的消息是obj\Debug\main.o |:|
我曾尝试在VisualStudio和CodeBlocks中编译,但收到了来自这两个版本的不同错误消息。我已经努力解决这个问题好几个小时了,非常感谢您的帮助。我只是想写一个简单的链表程序 这是我的头文件:C++ 我一直在主函数的左括号中发现一个错误,函数'zn1linked_listC1Ev';'中的消息是obj\Debug\main.o |:|,c++,C++,我曾尝试在VisualStudio和CodeBlocks中编译,但收到了来自这两个版本的不同错误消息。我已经努力解决这个问题好几个小时了,非常感谢您的帮助。我只是想写一个简单的链表程序 这是我的头文件: #ifndef LINKED_LIST_H_INCLUDED #define LINKED_LIST_H_INCLUDED #include<iostream> #include<cstdio> #include<cstdlib
#ifndef LINKED_LIST_H_INCLUDED
#define LINKED_LIST_H_INCLUDED
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;
/*
* Class Declaration
*/
class linked_list
{
public:
node* create_node(int);
void insert_begin();
void insert_last();
void insert_pos();
void delete_pos();
void delete_begin();
void delete_last();
void display();
linked_list()
{
start = NULL;
}
};
#endif // LINKED_LIST_H_INCLUDED
#如果包含链接列表#
#定义包含的链接列表
#包括
#包括
#包括
使用名称空间std;
/*
*节点声明
*/
结构节点
{
国际信息;
结构节点*下一步;
}*开始;
/*
*类声明
*/
类链表
{
公众:
节点*创建_节点(int);
void insert_begin();
void insert_last();
无效插入位置();
void delete_pos();
void delete_begin();
void delete_last();
void display();
链表()
{
start=NULL;
}
};
#endif//包含链接列表
这是我的实现文件:
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include "linked_list.h"
using namespace std;
/*
* Create Node
*/
node *linked_list::create_node(int 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;
}
}
/*
* Display all the elements of the linked list
*/
void linked_list::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
/*
* Inserting at the beginning of the list
*/
void linked_list::insert_begin()
{
int 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;
}
/*
* Inserting Node at the end of the list
*/
void linked_list::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last position"<<endl;
}
/*
* Insertion of node at the specified position
*/
void linked_list::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the position at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Position out of range"<<endl;
}
}
/*
* Deletion element at a given position
*/
void linked_list::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
/*
* Deletion of element at the beginning of the list
*/
void linked_list::delete_begin()
{
struct node *temp, *p;
temp = start;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
else
{
p = start;
start = temp;
p=start->next;
delete temp;
}
cout<<"Element deleted at beginning"<<endl;
}
/*
* Deletion of element at the end of the list
*/
void linked_list::delete_last()
{
struct node *p, *s;
s = start;
while (s->next != NULL)
{
p = s;
s = s->next;
}
p->next = NULL;
delete s;
cout<<"Element deleted at last position"<<endl;
}
#包括
#包括
#包括
#包括“链接列表.h”
使用名称空间std;
/*
*创建节点
*/
节点*链接的_列表::创建_节点(int值)
{
结构节点*temp,*s;
temp=新建(结构节点);
if(temp==NULL)
{
第一个错误是:
struct node
{
int info;
struct node *next;
}*start;
main()
您在header文件中创建了非常量变量。然后,此头包含在两个源文件中。
最后,在两个对象文件中有两个名为“start”的变量。这就是链接器抛出错误“start的多个定义”的原因
正如注释中提到的,“start”变量必须移动到linked_list类
第二个错误:
struct node
{
int info;
struct node *next;
}*start;
main()
应该是
int main()
为什么要包括
和
?不要在头文件中使用名称空间!它是int main()
链接列表(){start=NULL;}<代码> >不要使用全局变量。如果<代码> Link KEdList< /Cord>有一个<代码>开始/代码>,然后声明它为成员,并使用构造函数的初始化列表来设置其值。不要使用<代码> Ext()/<代码>结束C++程序。如果您这样做,则不会出现堆栈解卷。要退出<代码>主()
指示错误返回退出失败;
插入开始()
通常被称为推前()
,插入最后一个()
-->推后()
,插入位置()
插入位置()
(为什么插入位置()
不使用参数指定插入值的位置?),delete_pos()
-->remove()
(再次说明:为什么没有参数?),delete_begin()
-->pop_front()
,delete_last()
-->pop_back()
。