zoukankan      html  css  js  c++  java
  • 单链表的基本操作 增删查以及 头插法尾插法建表(初学)

    需要注意的: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;
    	
    } 
    
  • 相关阅读:
    Educational Codeforces Round 89 (Rated for Div. 2) B. Shuffle(数学/双指针)
    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords(贪心/数学)
    Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value(鸽巢原理)
    Codeforces Round #648 (Div. 2) D. Solve The Maze
    Codeforces Round #648 (Div. 2) C. Rotation Matching
    Codeforces Round #648 (Div. 2) B. Trouble Sort
    Codeforces Round #648 (Div. 2) A. Matrix Game
    一些常见的重置密码漏洞分析整理
    有无轻量级的可以显示md大纲的编辑器?
    Python3+ssl实现加密通信
  • 原文地址:https://www.cnblogs.com/biturd/p/12623196.html
Copyright © 2011-2022 走看看