链表的基本操作,实现链表的创建、查找、增加、删除、修改

链表的基本操作,实现链表的创建、查找、增加、删除、修改

#include <iostream>
using namespace std;
//链表数据元素的定义 
typedef struct node{
	char data;
	node * next;
}Node,*pNode;
pNode creatList1();//链表建立,头插法
pNode creatList2();//链表建立,尾插法 
pNode researchList1(pNode head,int num);//链表元素的查找,按照链表的序号查找 
pNode researchList2(pNode head,char Data,int& i);//链表元素的查找,按照链表的字符查找
void insertList1(pNode head,int i,char Data);//链表插入,在指定节点后插入 
void insertList2(pNode head,int i,char Data);//链表插入,在指定节点前插入 
void changeList(pNode head,int i,char m);//修改指定链表节点的字符
void deleteList1(pNode head,int i);//链表删除指定序号的节点 
void deleteList2(pNode head,char Data);//链表删除指定字符的节点
void printList(pNode head);//输出链表

int main(){
	cout << "头插法" << endl;
	pNode head1 = creatList1();
	printList(head1);
	cout << "尾插法" << endl;
	pNode head = creatList2();
	printList(head);
	cout << "以下是尾插法建立的链表的操作" << endl;
	int j;
	char n;
	cout << "请输入要查找节点的序号"; 
	cin >> j; 
	pNode a = researchList1(head,j);
	cout << "查找节点的字符为" << a->data << endl;
	cout << "请输入要查找节点的字符";
	cin >> n; 
	pNode b = researchList2(head,n,j);
	cout << b->data << "的序号为" << j << endl;
	insertList1(head,j,n);
	printList(head);
	insertList2(head,j,n);
	printList(head);
	cout << "请输入要修改的节点的位置";
	cin >> j;
	cout << "请输入要修改节点的修改字符";
	cin >> n;
	changeList(head,j,n);
	printList(head);
	cout << "请输入要删除的节点的序号"; 
	cin >> j;
	deleteList1(head,j);
	printList(head);
	cout << "请输入要删除的节点的字符"; 
	cin >> n;
	deleteList2(head,n);
	printList(head);
	return 0;
	
} 
//链表建立,头插法
pNode creatList1(){
    pNode head = new Node;
	if(head == NULL)
	{
		cout << "分配内存失败"; 
	}
	head->next = NULL;
	pNode p = head;
	int n;
	cout << "请输入要输入的字符数";	
	cin >> n;
	cout << "请输入要输入的字符";
	for(int i=0;i<n;i++){
		pNode q = new Node;
	    cin >> q->data;
	    q->next = p->next;
	    p->next = q;
	}
	return head;
} 
//链表建立,尾插法 
pNode creatList2(){
	pNode head = new Node;
	if(head == NULL)
	{
		cout << "分配内存失败"; 
	}
	head->next = NULL;
	pNode p = head;
	int n;
	cout << "请输入要输入的字符数";	
	cin >> n;
	cout << "请输入要输入的字符";
	for(int i=0;i<n;i++){
		pNode q = new Node;
	    cin >> q->data;
	    p->next = q;
	    p = q;
	}
	p->next = NULL;
	cout << "2";
	return head;
}
//链表元素的查找,按照链表的序号查找 
pNode researchList1(pNode head,int num){
	pNode p = head;
	int i = 0; 
	while(p != NULL && i < num){
		p = p->next;
		i++;
	}
	if(p == NULL){
		cout << "查找失败,查找位置不符合要求";
	}else{
		return p;
	}
}  
//链表元素的查找,按照链表的字符查找
pNode researchList2(pNode head,char Data,int& i){
	pNode p = head;
	i = 0;
	while(p != NULL && p->data != Data){
		p = p->next;
		i++;
	}
	if(p == NULL){
		cout << "查找失败,查找位置不符合要求";
	}else{
		return p;
	}
}
//链表插入,在指定节点后插入 
void insertList1(pNode head,int i,char Data){
	cout << "在指定节点后插入,请输入要插入节点的位置";
	cin >> i;
	cout << "请输入要插入节点的字符"; 
	cin >> Data;
	pNode p = researchList1(head,i);
	pNode q;
	q->data = Data;
	q->next = p->next;
	p->next = q;
} 
//链表插入,在指定节点前插入 
void insertList2(pNode head,int i,char Data){
	cout << "在指定节点前插入,请输入要插入节点的位置";
	cin >> i;
	cout << "请输入要插入节点的字符"; 
	cin >> Data;
	pNode p = researchList1(head,i-1);
	pNode q = new Node;
	q->data = Data;
	q->next = p->next;
	p->next = q;
} 
//修改指定链表节点的字符 
void changeList(pNode head,int i,char m){
  	pNode p = researchList1(head,i); 
  	p->data = m;
}
//链表删除指定序号的节点 
void deleteList1(pNode head,int i){
	pNode p = researchList1(head,i-1);
	pNode q = p->next;
	p->next = q->next;
	q = NULL;
	delete q;
} 
//链表删除指定字符的节点 
void deleteList2(pNode head,char Data){
	int i;
	pNode p = head;
	pNode m;
	i = 0;
	while(p != NULL && p->data != Data){
		m = p;
		p = p->next;
		i++;
	}
	if(p == NULL){
		cout << "查找失败,查找位置不符合要求";
	}
	//pNode p = researchList2(head,Data,i);
	m->next = p->next;
	p = NULL;
	delete p;
} 
//输出链表
void printList(pNode head){
	pNode p = head->next;
	cout << "打印的链表为";
	while(p != NULL){
		cout << p->data;
		p = p->next;
	}
	cout << endl;
} 

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值