数据结构 双链表的增删改查

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct Node2{
	int num;
	Node2 *next;
	Node2 *pre;
}Node2;//定义节点
/*
	输入:头节点,初始化长度

	返回:空
*/
void init(Node2 *head,int len){//初始化链表长度
	Node2 *head2=head;
	for(int i=0;i<len;i++){//尾插法
		Node2 *node=(Node2 *)malloc(sizeof(Node2));//创建一个节点,因为是malloc函数分配,分配的空间并不会因为函数运行结束而结束
		node->num=i;//给节点赋值
		head2->next=node;//将新节点的地址会给上一个节点的next,形成链表
		node->pre=head2;//指向前一个节点
		head2=node;//移动到新节点,也就是最后一个节点,然后继续循环
	}
	head2->next=NULL;//最后一个节点,next的值要为NULL

}
/*
	输入:头节点

	返回:空
*/
void print(Node2 *head){
	//从前面开始打印
	Node2 *head1=head->next,*back;//头结点没有数值,所以先移动到下一个节点
	while(head1!=NULL){
		cout<<head1->num<<" ";
		back=head1;
		head1=head1->next;//移动到下一个节点
	}
	cout<<endl;
	// 从后面开始打印
	Node2 *head2=back;
	while(head2!=NULL){
		if(head2->pre!=NULL)cout<<head2->num<<" ";//不要把头结点的值读出来
		head2=head2->pre;//移动到前一个节点
	}
	cout<<endl;
}
/*
	输入:头节点,要插入的位置(0开始数),插入的数
	
	返回:1成功,其他失败
*/
int add(Node2 *head,int pos,int number){
	//number,要追加的数字
	int i=0;
	Node2 *head1=head,*head2=NULL;
	if(head->next!=NULL)head2=head->next;
	do{
		if(i==pos){
			// cout<<"12132"<<endl;
			Node2 *node=(Node2 *)malloc(sizeof(Node2));
			node->num=number;

			head1->next=node;
			head2->pre=node;

			node->pre=head1;
			node->next=head2;
			
			return 1;//ok
		}
		// cout<<"78798"<<endl;
		if(head2==NULL)return -2;
		head1=head2;
		head2=head2->next;

	}while(i++<pos&&head1!=NULL&&head2!=NULL);//从0开始
	return -1;
}
/*
	输入:头节点,要删除的位置(0开始数)
	
	返回:1成功,其他失败
*/
int deletes(Node2 *head,int pos){
	int i=0;
	Node2 *head1=head,*head2=NULL;
	if(head->next!=NULL)head2=head->next;
	else return -3;//只剩头结点
	do{
		if(i==pos){
			Node2 *deletes=head1->next;
			head1->next=head2->next;
			head2->next->pre=head1;
			free(deletes);
			return 1;//ok
		}
		// cout<<"78798"<<endl;
		if(head2==NULL)return -2;
		head1=head2;
		head2=head2->next;

	}while(i++<pos&&head1!=NULL&&head2!=NULL);//从0开始
	return -1;
}
/*
	输入:头节点,要修改的位置(0开始数),修改后的值
	
	返回:1成功,其他失败
*/
int change(Node2 *head,int pos,int number){
	int i=0;
	if(head->next!=NULL)head=head->next;
	do{
		if(i==pos){
			head->num=number;
			return 1;
		}
		head=head->next;
	}while(i++<pos&&head!=NULL);//从0开始
	return -1;//查询失败:pos错误
}
/*
	输入:头节点,要修改的位置(0开始数)
	
	返回:成功则返回查询的值,否则返回-99999
*/
int check(Node2 *head,int pos){
	int i=0;
	if(head->next!=NULL)head=head->next;
	do{
		if(i==pos){
			return head->num;
		}
		head=head->next;
	}while(i++<pos&&head!=NULL);//从0开始
	return -99999;//查询失败:pos错误
}

int main(int argc, char const *argv[]){
	Node2 *head=(Node2 *)malloc(sizeof(Node2));
	head->next=NULL;
	head->pre=NULL;

	//双向链表初始化
	init(head,5);
	print(head);

	// cout<<check(head,1)<<endl;//0开始数

	// cout<<add(head,1,10)<<endl;//0开始数
	// print(head);

	// cout<<deletes(head,2)<<endl;//0开始数
	// print(head);

	// cout<<check(head,1)<<endl;//0开始数
	// print(head);

	// cout<<change(head,2,20)<<endl;//0开始数
	// print(head);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值