Estructura de datos: lista enlazada individualmente | lista perfecta enlazada individualmente

Notas de tabla lineales vinculadas individualmente

Lo anterior es el primero escrito al principio.


El siguiente es el segundo artículo con recursividad.

#include<iostream>
using namespace std;
typedef int Status;
typedef char elementype;

typedef struct Link
{
    
    
	elementype data;
	struct Link *next;
}*Node;

//单链表设置一个空的头结点
Status IniLink(Node &link)
{
    
    
	link = new Link;
	link->next = NULL;
	return 0;
}

//不断生成新的结点
Status CreatLink(Node &link)
{
    
    
	link = new Link;

	cout << "please input a element" << endl;
	cin >> link->data;

	if (link->data == '#')
	{
    
    
		link= NULL;//如果该结点元素不符合  使得该结点为空 而不是该节点的元素为空
		return NULL;
	}

	else
	{
    
    
		cout << "Please enter the   " << link->data << "   next node" << endl;
		//link->next = new Link;
		CreatLink(link->next);
	}
	return 0;
}

//打印输出
Status printLink(Node &link)
{
    
    
	if (link==NULL)
	{
    
    
		return 0;
	}
	if (link != NULL)
	{
    
    
		cout << link->data;
		//printf("%c ",link->data);
	}
	if (link->next != NULL)
	{
    
    
		printLink(link->next);
	}
	return 0;
}

//在第i个位置插入结点x
Status InsertLink(Node &link)
{
    
    
	if (link == NULL)
	{
    
    
		return 0;
	}
	else
	{
    
    
		int pos;
		int j = 0;
		elementype x;
		Node p = link;
		cout <<"please input a data" << endl;
		cin >> x;
		cout << "please input a position" << endl;
		cin >> pos;
		//第一个结点为空 后面顺序依次为1 2 3...
		while (p&&j < pos - 1)
		{
    
    
			p=p->next;
			++j;
		}
		if (!p&&j > pos - 1)
		{
    
    
			cout <<"error" << endl;
			return 0;
		}

		Node new_s = new Link;
		new_s->data = x;
		new_s->next = p->next;
		p->next = new_s;
	}
	return 0;
}

//将第i个位置的结点删除
int main()
{
    
    
	Node l;
	IniLink(l);
	CreatLink(l);

	printLink(l);
	InsertLink(l);
	printLink(l);
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_46096297/article/details/112384440
Recomendado
Clasificación