双向链表实现

#include<stdio.h>
#include<stdlib.h>
struct Node
{
	int data;
	struct Node* pre;
	struct Node* next;
};
struct Node* head;
Node* GetnewNode()
{
	Node* temp = (Node*)malloc(sizeof(Node));
	return temp;
}
void InsertheadNode(int x)
{
	Node* temp = GetnewNode();
	temp->data = x;
	temp->next = NULL;
	temp->pre = NULL;
	if (head == NULL)
	{
		head = temp;
		return;
	}
	head->pre = temp;
	temp->next = head;
	head = temp;
}
void Insertattail(int x)
{
	Node* temp = GetnewNode();
	temp->data = x;
	temp->next = NULL;
	temp->pre = NULL;
	Node* temp1 = head;
	while (temp1->next != NULL)
	{
		temp1 = temp1->next;
	}
	temp1->next = temp;
	temp->pre = temp1;
}
void Print()
{
	Node* temp = head;
	while (temp!=NULL)
	{
		printf("%d ", temp->data);
		temp = temp->next;
	}
	printf("\n");
}
void ReversePrintf()
{
	Node* temp = head;
	if (temp == NULL)
	{
		return;
	}
	while (temp->next!=NULL)
	{
		temp = temp->next;
	}
	while (temp!=NULL)
	{
		printf("%d ", temp->data);
		temp = temp->pre;
	}
}
int main()
{
	InsertheadNode(2);
	Print();
	InsertheadNode(4);
	Print();
	InsertheadNode(6);
	Print();
	Insertattail(9);
	Insertattail(25);
	Print();
	ReversePrintf();
}
C++

和单向链表差不多,只是多了一个指针域指向前面的节点

C语言

递归反转链表

2024-11-14 17:00:54

C语言

双向链表

2024-11-14 17:07:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索

Powered by atecplugins.com