单链表--笔记(自己看)

#include <iostream> //c99标准 iostream 流 
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>		
#include <vector>		
#include <queue>		
#include <string>
#include <cstring>      
#include <map>			
#include <stack>		
#include <set>
#define Status int
#define ElemType int
 
 
using namespace std;
 
//-----单链表的存储结构-----
typedef struct LNode{
	ElemType data;//结点的数据域  
	struct LNode *next;//结点指针域 
}LNode,*LinkList;//Linklist未指向结构体LNode的指针类型 
 
//-----初始化-----
Status InitList(LinkList &L){
	//构造一个空的链表L
	L=new LNode;//生成新结点,用头指针L指向头结点
	L->next = NULL;//头结点的指针域置空 
	return 1; 
} 
//-----单链表的取值-----
//查找第i个元素 
Status GetElem(LinkList L,int i){
	
	LinkList p = L->next; int flag=1;
	//这里是因为单链表要获取下一个元素的地址,才能比较元素 
	while(p&&flag<i){ 
	p = p->next;
		flag++;
	}
	if(!p || flag>i) return 0;
	return p->data; 
	
} 
//-----查找-----
LNode *LocateElem(LinkList L, ElemType e)
{
	LinkList p = L->next;
	//查找到对应元素后,跳出循环 
	while(p && p->data != e)
		p=p->next;
	return p;//返回p的地址	 
 } 
//-----插入-----
Status ListInsert(LinkList &L,int i,ElemType e)
{
	LinkList p = L; 
	int flag = 0;//注意从0开始,因为可以插如在第一个元素的前面
	//i-1插入位置的前zh一个元素,找到他后便跳出循环  
 	//查找第i-1个结点,p指向该结点 
	 while(p && (flag < i-1))
		{p = p->next; flag++;}
	if(!p || flag>i-1) return 0;
	LinkList s = new LNode;//生成新的结点
	s->data = e;  
	s->next = p->next;//将结点*s的指针域指向ai	
	p->next = s;//将结点*p的指针域指向结点*s
	return 1;  
 } 
//-----删除-----
Status ListDelete(LinkList &L,int i)
{
	LinkList p,q;
	p = L;
	int flag = 0;
	//查找第i-1个结点,p指向该结点 
	while ((p->next) && (flag<i-1)) 
		{p=p->next; flag++;} 
	if (! (p->next) || (flag>i-1)) return 0; //当心n或区1时,删除位置不合理
 	q = p->next;//临时保存被删结点的地址以备释放
 	p->next=q->next; //改变删除结点前驱结点的指针域
 	delete q; //释放删除结点的空间
 	return 1;
 } 
//-----前插-----
//因为是从前面插入,所以输入的数组排列,要反序输入 
 void CreateList_H(LinkList &L,int n)
 {
 	L = new LNode;
 	L->next = NULL;//先建立一个带头结点的空链表
 	for(int i = 0; i <= n; i++)
 	{
 		LinkList p = new LNode; 
 		cin >> p->data;//输入元素值赋给新结点*p的数据域
 		p->next = L->next;
 		L->next = p;
	 }
 }
//-----后插-----
void CreateList_R(LinkList &L,int n)
{
	LinkList r,p;
	L=new LNode; 
	L->next=NULL;//先建立一个带头结点的空链表
	r=L;//尾指针r指向头结点
	for(int i=0;i<n;i++){
		p=new LNode;//生成新结点
		cin>>p->data;//输人元素值赋给新结点*p的数据域
		p->next=NULL; r->next=p; //将新结点*p插人尾结点*r之后
		r=p;//r指向新的尾结点*p
	}  	
}
//-----输出-----
void PrintList(LinkList L){
	cout << "当前链表元素: ";
	LNode *p;
	p = L->next;
	if(p==NULL){
		cout << "没有创建链表" << endl; 
	}
	while (p != NULL){
		cout << p->data << ",";
		p = p->next;
	}
	cout << endl; 
} 
int kong(LinkList L){
	LNode *p;
	p = L->next;
	p == NULL;
	return 1;
}
//-----菜单-----
void menu(){
	cout << "--------------" << endl;
	cout << "|---1.创建---|" << endl; 
	cout << "|---2.取值---|" << endl;
	cout << "|---3.查找---|" << endl;
	cout << "|---4.删除---|" << endl;
	cout << "|---5.插入---|" << endl;
	cout << "|---6.输出---|" << endl;
	cout << "|---7.退出---|" << endl;
	cout << "--------------" << endl;
} 
 
int main(){
	LNode *L;
	InitList(L); //初始化链表
	int choice;
	while(1){
		menu();
		cout << "请输入你的选择" << endl;
		cin >> choice;
		if (choice == 7) return 0;
		switch (choice){
			case 1:{
				int n = 0;
				cout << "请输入元素个数: ";
				cin >> n;  
				CreateList_R(L,n);
				cout << "创建完成" << endl;
				system("pause");
				break;
			}
			case 2:{
				LinkList p; 
				int i = 0;
				cout << "请输入查找的位置: ";
				cin >> i;
				p->data = GetElem(L,i);
				if (p->data == 0){
					cout << "没有添加该元素" << endl; 
				}  
				else 
			 	cout << "该位置的元素是:" << p << endl;
				break;
			}
			case 3:{
				int e = 0;
				cout << "输入查找的元素:";
				cin >> e;  
				if(LocateElem(L,e) == 0)
				cout << "没有查找到该元素" << endl;
				else 
				cout << "该元素的地址是: " << LocateElem(L,e) << endl;
				system("pause");
				break;
			}
			case 4:{
				int i = 0;
				cout << "输入删除结点的位置: ";  
				cin >> i;
				int a;
				cout << "确认删除该结点" << endl;
				cout << "确认输入1  取消输入0" << endl; 
				cin >> a;
				if(a == 1){
					ListDelete(L,i);
					break;
				}
				break;
			}
			case 5:{
				int i=0,e=0;
				if(kong(L)){
					cout << "因为没有创建链表,位置只能为1" << endl;
					i = 1; 
					cout << "元素: " << endl;
					cin >> e; 
					ListInsert(L,i,e);
					break;
				}
				else {
					cout << "分别输入插入的位置和元素: " << endl;
					cout << "位置: " << endl; 
					cin >> i;
					cout << "元素: " << endl;
					cin >> e; 
					ListInsert(L,i,e);
					break;
				}
			} 
			case 6:{
				PrintList(L);
				system("pause");
				break;
			}
		}
        system("cls");
	} 
    return 0;
}

自己写的,可能不太全

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值