zoukankan      html  css  js  c++  java
  • 单链表操作(带头节点)

    //带头节点的单链表 
    #include <stdio.h>
    #include <malloc.h>
    
    typedef struct LNode{		//定义单链表 
    	int data;
    	struct LNode *next;
    }Node , *LinkList; 
    
    bool InitList(LinkList &);	//初始化单链表 
    bool ListInsert(LinkList & , int , int);	//在指定位置插入数据 
    bool Empty(LinkList); 	//判断链表是否为空  
    bool HListInsert(LinkList & , int , int);	//在指定节点前面插入数据  
    bool RListInsert(LinkList * , int , int);	//在指定节点后面插入数据  
    void output(LinkList);	//输出链表 
    bool ListDelete(LinkList & , int , int &);	//删除指定位置的数据 
    bool HeadInsert(LinkList , int);	//头插法 
    Node* GetElem(LinkList , int);		//查找指定位置的值 
    int LocateElem(LinkList , int);		//查找指定的值在哪个位置 
    
    int main(void){
    	LinkList L;
    	InitList(L);
    	
    	HeadInsert(L,5);
    	HeadInsert(L,2);
    	TailInsert(L,7);
    	TailInsert(L,4);
    	RListInsert(&L,2,3);
    	
    	printf("当前单链表中的数据是:");
    	output(L);
    	
    	printf("
    ");
    	Node* r = GetElem(L,3);
    	printf("你要查找的值是%d",r->data);
    	
    	printf("
    ");
    	int x = LocateElem(L,7);
    	printf("你要查找的值在第%d位",x);
    	
    	printf("
    ");
    	int e = -1;
    	ListDelete(L,2,e);
    	printf("删除的哪个元素是:%d",e);
    	
    	printf("
    ");
    	printf("当前单链表中的数据是:");
    	output(L);
    	
    	printf("
    ");
    	if(Empty(L)){
    		printf("当前链表为空!");
    	}else{
    		printf("当前链表不为空! ");
    	} 
    	return 0;
    } 
    
    //初始化单链表 
    bool InitList(LinkList &L){
    	L = (Node*)malloc(sizeof(Node));
    	if(L == NULL){
    		return false;
    	}
    	L->next = NULL;
    	return true;
    } 
    
    //遍历单链表
    void output(LinkList L){
    	Node *p = L->next;
    	while(p != NULL){
    		printf("%d ",p->data);
    		p = p->next;
    	}
    } 
    
    //判断单链表是否为空
    bool Empty(LinkList L){
    	if(L->next == NULL){
    		return true;
    	}else{
    		return false;
    	}
    } 
    
    //在指定的位置插入数据 
    bool ListInsert(LinkList &L ,int i , int e){
    	if(i < 1){
    		return false;
    	}
    	Node *p = L;
    	int j = 0;
    	while(p != NULL && j < i-1){
    		p = p->next;
    		j ++;
    	}
    	if(p == NULL){
    		return false;
    	}
    	Node *s = (Node*)malloc(sizeof(Node));
    	s->data = e;
    	s->next = p->next;
    	p->next = s;
    	return true;
    } 
    
    //在指定节点前面插入数据 
    bool HListInsert(LinkList &L , int i , int e){
    	if(i < 1){
    		return false;
    	}
    	Node *p = L;
    	int j = 0;
    	while(p != NULL && j < i){
    		p = p->next;
    		j ++; 
    	}
    	if(p == NULL){
    		return false;
    	}
    	Node *s = (Node *)malloc(sizeof(Node));
    	s->data = p->data;
    	p->data = e;
    	s->next = p->next;
    	p->next = s; 
    	return true;
    } 
    
    //在指定节点后面插入数据 
    bool RListInsert(LinkList *L , int i , int e){
    	if(i < 1){
    		return false;
    	}
    	Node *p = *L;
    	int j = 0;
    	while(p != NULL && j < i){
    		p = p->next;
    		j ++;
    	}
    	if(p == NULL){
    		return false;
    	}
    	Node *s = (Node *)malloc(sizeof(Node));
    	s->next = p->next;
    	p->next = s;
    	s->data = e; 
    }
    
    //删除指定位置的数据 
    bool ListDelete(LinkList &L , int i , int &e){
    	if(i < 1){
    		return false;
    	}
    	Node *p = L;
    	int j = 0;
    	while(p != NULL && j < i-1){
    		p = p->next;
    		j ++;
    	}
    	if(p == NULL || p->next == NULL){
    		return false;
    	}
    	Node *q = p->next;
    	e = q->data;
    	p->next = q->next;
    	free(q);
    	return true;
    } 
    
    //头插法
    bool HeadInsert(LinkList L , int e){
    	Node *s = (Node *)malloc(sizeof(Node));
    	//L->next = NULL;	//使用头插法建立单链表时需要初始化next 
    	s->data = e;
    	s->next = L->next;
    	L->next = s;
    	return true;
    }
    
    //查找指定位置的值 
    Node* GetElem(LinkList L , int i){
    	if(i < 1){
    		return NULL;
    	}
    	Node *p = L;
    	int j = 0;
    	while(p != NULL && j < i){
    		p = p->next;
    		j ++;
    	}
    	return p;
    }
    
    //查找指定的值在哪个位置 
    int LocateElem(LinkList L , int e){
    	Node *p = L->next;
    	int j = 1;
    	while(p != NULL && p->data != e){
    		p = p->next;
    		j ++;
    	}
    	if(p == NULL){
    		return -1;
    	}else{
    		return j; 
    	}
    }
    
  • 相关阅读:
    runas用法
    转:在 DB2 9.7 for Linux, UNIX, and Windows OLTP 环境中使用 EMC Enterprise Flash Drives 的好处
    Dlink路由器的special application 功能
    对称加密和非对称加密
    转:Windows 7加密促使犯罪几率上升
    S60V5版本的手机QQ支持截图、语音和视频功能
    各类存储设备速度比较
    DiffieHellman密钥交换协议工作过程
    转:QQ圈子:社交神器还是隐私魔鬼?
    转:你对存储性能了解多少?
  • 原文地址:https://www.cnblogs.com/Timesi/p/12425972.html
Copyright © 2011-2022 走看看