一个能用的链表

功能:插入(Insert) ,删除(delete) ,列出(list) ,查找(search)。
p.s.若在链表仅有1个节点时进行删除操作,会爆炸,

#include "cstdio"
#include "cstdlib"
#include "cstring"
#include "cmath"
#include "sstream"
#include "iostream"
#include "algorithm"
using namespace std;
struct database
{
	string data;
	database *link, *delink;
} *root, *tail;
int main()
{

	/*
		Initialize begin
	*/
	int n;
	scanf("%d", &n);
	root = (database*)malloc(sizeof(database));
	database *Link = root;
	/*
		Initialize end
	*/

	/*
		Input begin
	*/
	for (int i = 0; i < n; i++)
	{
		string temp;
		cin>>temp;
		Link->data = temp;
		Link->link = (database*)malloc(sizeof(database));
		Link->link->link = NULL;
		Link->link->delink = Link;
		Link = Link->link;
	}
	tail = Link;
	/*
		Input end
	*/

	string command;
	while(cin>>command)
	{
		if (command == "search")
		{
			string keyword;
			cin>>keyword;
			for (Link = root; Link != tail; Link = Link->link)
			{
				if (Link->data == keyword)
				{
					cout<<"Source found ! Address = "<>keyword;
			Link = tail;
			Link->link = (database*)malloc(sizeof(database));
			Link->link->delink = Link;
			Link->link->link = NULL;
			Link->data = keyword;
			Link = Link->link;
			tail = Link;
			cout<<"insertion finished ! Address = "<delink<link)
			{
				if (Link->data == keyword)
				{
					database *temp = Link, *left, *right;
					left = temp->delink;
					right = temp->link;
					left->link = right;
					right->delink = right;
					free(temp);
					check = false;
					printf("Source deleted !\n");
				}
			}
			if (check)
				printf("Source not found !\n");
			goto esc;
		}
		if (command == "list")
		{
			printf("======Source List======\n");
			for (Link = root; Link != tail; Link = Link->link)
				cout<data<



CC BY-SA 4.0 一个能用的链表 by 小小泥娃 is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

发表评论

This site uses Akismet to reduce spam. Learn how your comment data is processed.