栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

链表LinkList C++/C

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

链表LinkList C++/C

//C++版本
#include 
using namespace std;
class Node {
public:
	int data;
	Node *next;
};
int main() {
	int A[] = { 3,5,7,10,15 };
	Node *head = new Node;
	Node *temp;
	Node *last;
	head->data = A[0];
	head->next = nullptr;
	last = head;
	for(int i = 1; i < sizeof(A) / sizeof(A[0]); i++){
		temp = new Node;
		temp->data = A[i];
		temp->next = nullptr;

		last->next = temp;
		last = temp;
}
	Node *p = head;
	while (p != nullptr) {
		cout << p->data << "->" << flush;
		p = p->next;
	}
	return 0;

}

C语言版本

# include 
#include 
struct Node {
	int data;
	struct Node *next;

}*first=NULL;

void create(int A[], int n) {
	int i;
	struct Node*t, *last;
	first = (struct Node *) malloc(sizeof(struct Node));
	first->data = A[0];
	first->next = NULL;
	last=first;
	for (i = 0; i < n; i++) {
		t= (struct Node *) malloc(sizeof(struct Node));
		t->data = A[i];
		t->next = NULL;
		last->next = t;
		last = t;
	}
}
void Display(struct Node *p)
{
	while (p != NULL) {
		printf("%d", p->data);
		p = p->next;
	}
}
void RDisplay(struct Node *p)
{
	if (p != NULL)
	{
		RDisplay(p->next);//recursion 递归
		printf("%d", p->data);
	}
}

int main() {
	struct Node *temp;
	int A[] = { 3,5,7,10,25,8,32,2 };
	create(A, 8);
	Display(first);

	return 0;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315894.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号