常用数据结构————链表

链表应该是面试时被提及最频繁的数据结构!!!


下面是一个往链表末尾插入元素的操作:

typedef struct node{
	int value;
	node *next;
} node;

//往列表的末尾添加一个节点
node* insert_tail(node *head,int key){
	node *new_node;
	node *temp ;
	temp = head;
	new_node =(node *)malloc(sizeof(node));
	new_node->value = key;
	if(head == NULL){
		head = new_node;
		new_node ->next = NULL;		
	}else{
		while(temp->next!=NULL){
			temp = temp->next;
		}
		temp->next = new_node;
		new_node->next = NULL;
	}

	return head;
}


题目:从尾到头打印链表


反向打印链表:

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
	int value;
	node *next;
} node;
typedef struct stack{
	int key;
	stack *pre;
	stack *next;
} stack;

stack *head;
stack *bottom;

stack* push(int key){
	if(head == NULL && head==bottom){
		head = (stack*) malloc(sizeof(stack));
		bottom = head;
		bottom->key = key;
		bottom->pre = bottom;
		bottom->next= NULL;
	}else{
		//分配新的节点
		stack *new_node;
		new_node =(stack*) malloc(sizeof(stack));
		new_node->key = key;
		//连接到下一个节点
		head->next = new_node;
		new_node->pre = head;
		//移动头指针到新的位置上
		head = new_node;
		head->next=NULL;
	}
	return head;
}
//反向打印链表的出栈操作
stack* pop_all_print(){
	stack *temp;
	//最后一个节点没有输出出来
	if(head!=NULL && bottom!=NULL){
		while(head!= bottom){
			printf("node key: %d\n",head->key);
			temp = head;
			head = head->pre;
			head->next = NULL;
			free(temp);
		}
		//通过该方式将最后一个节点输出出来,并且释放内存
		printf("node key: %d\n",head->key);
		free(head);
		head = bottom;
	}
	return NULL;
}
//反向打印链表
void reverse_print(node *link_head){
	node *search;
	search = link_head;
	while(search !=NULL){
		head = push(search->value);
		search = search->next;
	}
	pop_all_print();
}
int main(){
	node *link_head;
	link_head = NULL;
	for(int i=0;i<15;i++){
		link_head = insert_tail(link_head,i);
	}
	reverse_print(link_head);
	system("pause");
	return 0;
}



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值