链表

1.创建一个带有头结点的单链表

	head=new Node();
	head->next =NULL;

尾插法:

	end=head;
	int n;cin>>n;
	while(n--){
		p=new Node();
		cin>>p->data;
		end->next=p;
		end=p;
	}
	end->next =NULL;//必须要有

头插法:

int n;cin>>n;
	while(n--){
		p=new Node();
		cin>>p->data;
		p->next=head->next;
		head->next =p;
	}

2.遍历

	void TraverseList(pNode head){
	pNode p;
	p=head->next ;
	while(p!=NULL){
		cout<<p->data ;
		p=p->next ; 
	}
	cout<<endl;
	return ;
}

3。指定结点前面插入结点

	bool Insert_Node(pNode head,int num,int data){
	pNode p,q;
	q=new Node();
	q->data =data;
	p=head->next;
	if(num<1||p==NULL){
		return false;
	}
	int i=1;//链表的结点是从第一个开始的 
	while(i<num-1){
		p=p->next ;
		i++;
	}
	q->next =p->next ;
	p->next =q;
	return true;
}

4.删除指定结点

	bool Del_Node(pNode head,int num)
	{
	pNode p,q;
	p=head->next ;
	q=head;
	int i=1;
	while(i<num){
		p=p->next ;
		q=q->next ;
		i++;
	}
	q->next =p->next ;
	free(p);
	return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值