链表

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<data ;
		p=p->next ; 
	}
	cout<

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(inext ;
		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(inext ;
		q=q->next ;
		i++;
	}
	q->next =p->next ;
	free(p);
	return true;
}

你可能感兴趣的:(数据结构笔记)