m0_51859854
m0_51859854
采纳率0%
2021-04-06 13:33

C++单链表实现通讯录遇到的问题:无法解析的外部命令、无法解析的外部符号

VS2019

求大佬解答用单链表实现通讯录遇到的这些问题,急。

若我没将问题没描述清楚,请联系我

头文件:
#pragma once
#include<iostream>
using namespace std;
#include<string>
template<class T>
struct Node {
	T data; struct Node<T>* next;
};
template<class T>
class LinkList
{
public:
	LinkList(T a[], int n, double h)//头插法构造
	{
		front = new Node<T>;
		front->next = NULL;
		for (int i = n - 1; i >= 0; i--)
		{
			Node<T>* s = new Node<T>;
			s->data = a[i];
			s->next = front->next;
			front->next = s;
		}
	};
	LinkList(T a[], int n, int e)//尾插法构造
	{
		front = new Node<T>;
		Node<T>* r = front;
		for (int i = 0; i < n; i++)
		{
			Node<T>* s = new Node<T>;
			s->data = a[i];
			r->next = s;
			r = s;
		}
	};
	//获取第i个位置上的元素结点的地址yc
	Node<T>* Get(int i)
	{
		Node<T>* p = front->next;
		int j = 1;
		while (p != NULL && j != i)
		{
			p = p->next;
			j++;
		}
		return p;

	};
	int GetLenth()//获取链表的长度yc
	{
		Node<T>* p = front->next;
		int length = 0;
		while (p != NULL) {
			p = p->next; length++;
		}
	};
	void Insert(int i, T x)//插入
	{
		Node<T>* p = front;
		if (i != 1) p = Get(i - 1);
		if (p) {
			Node<T>* s = new Node<T>;
			s->data = x;
			s->next = p->next;
			p->next = s;
		}
		else throw"插入位置错误";
	};

	T Delete(int i)//删除
	{
		Node<T>* p = front;
		if (i != 1)p = Get(i - 1);
		Node<T>* q = p->next;
		p->next = q->next;
		T x = q->data;
		delete q;
		return x;
	};
	/*
	Node<T>* Get(int i)//按位置查找
	{
		Node<T>* p = front->next; int j = 1;
		while (p && j != i) {
			p = p->next; j++;
		}
		return p;
	};
	*/
	int Locate(T x)//按值查找
	{
		Node<T>* p = front->next;
		int j = 1;
		while (p) {
			if (p->data == x)return j;
			p = p->next; j++;
		}
		return -1;
	};
	~LinkList()//销毁
	{
		Node<T>* p = front;
		while (p) {
			front = p; p = p->next; delete front;
		}
	};
	void PrintList()//显示
	{
		Node<T>* p = front->next;
		while (p != NULL)
		{
			p->data.Print();
			p = p->next;
		}
		cout << endl;
	};
private:
	Node<T>* front;
};
class PHONE
{
public:
	PHONE();
	PHONE(int id, string name, string phone, string group) {
		x_id = id; x_name = name; x_phone = phone; x_group = group;
	};
	void Print() {
		cout << x_name << " " << x_phone << " " << x_group << endl;
	};
	bool operator==(PHONE& p)
	{
		if (p.x_id == x_id)return true; return false;
	}
private:
	int x_id; string x_name; string x_phone; string x_group;
};


源文件:
#include"tem.h"
int main()
{
	PHONE pbook[4] = { {1,"liming","111111","classmates"},{2,"zhangtao","222222","teachers"},{3,"zhoulian","333333","family"},{4,"songxu","444444","classmates"} };
	PHONE record{ 5,"wangwu","000000","classmates" };
	LinkList<PHONE>list(pbook, 4, 1.111);
	cout << "通讯录列表为:" << endl;
	list.PrintList();
	list.Insert(1, record);
	cout << "通讯录列表:" << endl;
	list.PrintList();
	PHONE x = list.Delete(3);
	cout << "删除元素:" << endl; x.Print();
	cout << "通讯录列表:" << endl;
	list.PrintList();
	int w = list.Locate(record);
	cout << "插入的元素的位置是:" << w << endl;
}

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • m0_51859854 m0_51859854 1月前

    那要怎么解决呢

    点赞 评论 复制链接分享
  • qq_34124780 qq_34124780 1月前

    初始化失败:

     

    点赞 评论 复制链接分享
  • QA_Assistant 有问必答小助手 1月前

    您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论 复制链接分享
  • SoftwareTeacher SoftwareTeacher 1月前

    PHONE();  要写一个实现的实体,即使是一个 {} ?

    点赞 评论 复制链接分享