c++单链表实现大数的求和运算

首先list.h

#ifndef LIST_H
#define LIST_H

#include <string>
#include <iostream>
#include <cassert>

using namespace std;
typedef 	struct NODE		node;
typedef 	struct NODE*	nodeptr;
typedef 	string::size_type	size_type;

struct NODE
{
	nodeptr link;
	int value;
};

class List
{
private :
	nodeptr head;
	nodeptr tail;
	int charToInt(char ch)
	{
		return ((int)ch - 48);
	}

public :
	List();
	void create(string& str); 		
	void insertBack(int value);
	void reverse();
	List& operator+(List& list);
	void show();
};
#endif

//构造函数,默认构造一个只有头结点的链表
List :: List()
{
	head = (nodeptr)operator new(sizeof(node));
	assert(head != NULL);
	head -> link = NULL;
	head -> value = 0;
	tail = NULL;
}

//根据传入的大数字符串创建一个相对应的链表
void List :: create(string& str)
{
	for(size_type i = 0; i < str.size(); ++ i)	
	{
		insertBack(charToInt(str[i]));
	}
}

//每次都在链表的后面插入
void List :: insertB
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个基本的单链表通讯录的实现。首先,我们需要定义一个通讯录的节点结构体,包含姓名、电话号码、邮箱等信息: ```c++ struct Contact { std::string name; std::string phone; std::string email; Contact* next; }; ``` 其中,`next`指向下一个节点。接下来,我们需要定义一个用于管理通讯录的类 `ContactList`,其中包括添加、删除、查找联系人等方法: ```c++ class ContactList { public: ContactList() { head = nullptr; } ~ContactList() { Contact* current = head; while (current != nullptr) { Contact* next = current->next; delete current; current = next; } } void addContact(const std::string& name, const std::string& phone, const std::string& email) { Contact* newContact = new Contact(); newContact->name = name; newContact->phone = phone; newContact->email = email; newContact->next = head; head = newContact; } void removeContact(const std::string& name) { Contact* current = head; Contact* previous = nullptr; while (current != nullptr) { if (current->name == name) { if (previous == nullptr) { head = current->next; } else { previous->next = current->next; } delete current; return; } previous = current; current = current->next; } } Contact* findContact(const std::string& name) { Contact* current = head; while (current != nullptr) { if (current->name == name) { return current; } current = current->next; } return nullptr; } private: Contact* head; }; ``` 上述代码中,`addContact`方法在链表头部插入一个新的节点,`removeContact`方法删除指定姓名的节点,`findContact`方法查找指定姓名的节点。 使用示例: ```c++ ContactList myContacts; myContacts.addContact("Alice", "123456", "[email protected]"); myContacts.addContact("Bob", "789012", "[email protected]"); myContacts.addContact("Charlie", "345678", "[email protected]"); Contact* contact = myContacts.findContact("Bob"); if (contact != nullptr) { std::cout << "Phone: " << contact->phone << std::endl; } myContacts.removeContact("Charlie"); ``` 以上是一个基本的单链表通讯录实现,您可以根据需要进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值