• 单链表的基本操作 增删查以及 头插法尾插法建表(初学)


    需要注意的:1.InitList(List &L)&是标识符的作用 L和实参同一个引用
    2.在每个方法中不能对头结点操作 先把头结点赋值一份 对副本引用操作
    3.操作链表中间需要从头结点开始一个个查找,找到目标再插入 删除 可以先写查找方法 实现代码复用

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct LNode{
    	int data;
    	LNode* next;
    }*LinkList,*List;
    void CreateLinkList_head(LinkList &l,int n){  //n可以设置为插入结点次数 
    	List p;  //l头结点 p为新结点 
    	int i=0,x;
    	printf("请输入第%d个结点的值
    ",++i);
    		scanf("%d",&x);	
    	while(x!=-1){
    		p=(List)malloc(sizeof(LNode));
    	//   头插法	
    		p->next=l->next;  //新结点指向原结点指向的 
    		p->data=x;    //新结点赋值 
    		l->next=p;    //原结点和新结点建立连接 
    		printf("请输入第%d个结点的值
    ",++i);
    		scanf("%d",&x);	
    	}
    }
    void CreateLinkList_end(LinkList &e,int n){
    	List l,newList;
    	int i=1,x;
    	l=e;
    	while(n>0){
    		newList=(List)malloc(sizeof(LNode));
    		l->next=newList;
    		l=newList;
    		printf("请输入第%d个结点的值
    ",i++);
    		scanf("%d",&x);
    		newList->data=x;
    		n--;
    	}
    	l->next=NULL;
    }
    void initLinkList(LinkList &p){
    	p=(List)malloc(sizeof(LNode));
    	p->next=NULL; 
    }
    void DestroyList(LinkList &l){
    	List p=l->next;
    	while(l->next!=NULL){
    		l=l->next;
    		free(p);
    	}
    	free(l);   //释放头结点
    	l=NULL;   //把链表首地址赋空 
    } 
    void ClearList(LinkList &l){
    	List p=l->next;
    	while(l->next!=NULL){
    		l=l->next;
    		free(p);
    	}
    }
    int LenghtList(LinkList &l){
    	int i=0;
    	LinkList h;
    	h=l;
    	while(h->next!=NULL){
    		h=h->next;
    		i++;
    	}
    	return i;
    }
    List LocateList(LinkList &l,int n){  //找到第n个元素 返回他的地址 
    	List h;
    	h=l;
    	while(n){
    		h=h->next;  //在第n次循环完 h指向第n个元素 
    		n--;
    	}
    	return h;
    }
    void InsertList(LinkList &l,int n,int x){   //在第n个位置之后 插入 值x 
    	List h,p;
    	p=(List)malloc(sizeof(LNode));
    	p->data=x; 
    	h=l;
    	if(n>LenghtList(l)){
    		printf("ERROR!");
    		return; 
    	}
    //	while(n){
    //		h=h->next;
    //		n--;
    //	}   //经过n次循环 h是第n个结点的地址
    	h=LocateList(l,n); //找到第n个结点  代码复用 
    	p->next=h->next;
    	h->next=p;
    }
    void DeleteList(LinkList &l,int n){
    	List h,p;
    	h=l;
    	if(n>LenghtList(l)){
    		printf("ERROR!");
    		return ;
    	}
    //	while(n-1){
    //		h=h->next;
    //		n--;   //经过n-1次循环 h是n前一个结点的地址 
    //	}
    	h=LocateList(l,n-1);
    	p=h->next;  //要删除的结点地址  设定p原因:改变指针后用p找到 
    	h->next=p->next;
    	p->next=NULL;
    	free(p);
    }
    int main(){
    	LinkList L,p;
    	initLinkList(L);
    	CreateLinkList_end(L,5);
    	DeleteList(L,4);
    	InsertList(L,3,-1);
    	p=LocateList(L,3);
    	int i=1;
    	while(L->next!=NULL){
    		L=L->next;
    		printf("第%d个结点的值为%d
    ",i++,(L->data));
    	}
    	printf("%d",p->data);
    	return 0;
    	
    } 
    
  • 相关阅读:
    安卓模拟器genimotion安装过程
    python3安装过程
    python基础数据类型数字和字符串
    genimotion模拟器和appium环境配置
    ArcEngine中的ICommand和ITool
    BM.AE介绍
    地图数据和版式数据联动
    BM.AE中的命令工具体系
    2021年1月1日 AutoCAD.Net/C#.Net QQ群:193522571获取任意路径dwg文件内所有块在控件中显示并能选择插入当前模型空间
    2021年1月1日 AutoCAD.Net/C#.Net QQ群:193522571用acedGrRead()函数实现一拖多
  • 原文地址:https://www.cnblogs.com/biturd/p/12623196.html
Copyright © 2020-2023  润新知