zoukankan      html  css  js  c++  java
  • 实战数据结构(5)_双向循环链表的基本操作

    /************************************************************************/
    /* @author lynnbest
    双向循环列表的使用:
    1.创建
    2.插入
    3.删除
    4.打印
    5.按位置查找
    6.按内容查找
    7.退出                                                                */
    /************************************************************************/
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct node
    {
    	int data;
    	struct node *prior;
    	struct node *next;
    }Dlistnode;
    Dlistnode *CreateDClist(int n);
    void printflist(Dlistnode *head);
    int lengthlist(Dlistnode *head);
    void InsertDClist(Dlistnode *head,int pos);
    void DeleteDClist(Dlistnode *head,int pos);
    void main()
    {
    	printf("   双向循环链表基本操作	
    ");
    	printf("----by lynnbest ----
    
    ");
    	int  choice,num;
    	Dlistnode *head;
    	while(1)
    	{
    		printf("1---创建一个双向循环链表
    ");
    		printf("2---插入节点操作
    ");
    		printf("3---删除节点操作
    ");
    		printf("4---打印链表元素
    ");
    		printf("5---查找操作(按位置)
    ");
    		printf("6---查找操作(按内容)
    ");
    		printf("7---退出
    请输入操作:
    ");
    		scanf("%d",&choice);
    		switch(choice)
    		{
    			case 1:
    					printf("请输入创建元素的个数:
    ");
    					scanf("%d",&num);
    					head=CreateDClist(num);
    					break;
    			case 2:	
    					printf("输入插入的位置:
    ");
    					scanf("%d",&num);
    					InsertDClist(head,num);
    					break;
    			case 3:
    					printf("输入删除的位置:
    ");
    					scanf("%d",&num);
    					DeleteDClist(head,num);
    					break;
    			case 4:
    				printflist(head);
    			//	printf("共有%d个元素
    ",lengthlist(head));
    				break;
    			case 5:
    				break;
    			case 6:
    				break;
    			case 7:
    				return ;
    				break;
    			default :
    				break;
    		}
    		
    	}
    
    
    }
    Dlistnode *CreateDClist(int n) //创建一个带头节点的双向循环链表
    {
    	Dlistnode *head,*newnode,*pre;
    	int data;
    	if(NULL==(head=(Dlistnode *)malloc(sizeof(Dlistnode))))
    	{
    		printf("头结点创建失败
    ");
    		exit(-1);
    	}
    	pre=head;
    	for(int i=1;i<=n;i++)
    	{
    		if(NULL==(newnode=(Dlistnode *)malloc(sizeof(Dlistnode))))
    		{
    			printf("创建失败
    ");
    			exit(-1);
    		}
    		printf("请输入第%d个元素
    ",i);
    		scanf("%d",&data);
    		newnode->data=data;
    		//开始插入
    		pre->next=newnode;	
    		newnode->prior=pre;
    		pre=newnode;
    		newnode->next=NULL;
    	}
    	newnode->next=head;
    	head->prior=newnode;	//做首位循环
    	return head;
    }
    void printflist(Dlistnode *head)
    {
    	Dlistnode *cur=head->next;
    	while(cur->next!=head)
    	{
    		printf("%3d",cur->data);
    		cur=cur->next;
    	}
    	printf("%3d
    ",cur->data);
    		return ;
    }
    int lengthlist(Dlistnode *head)
    {
    	Dlistnode *cur=head;
    	int count=0;
    	while(cur->next!=head)
    	{	
    		count++;
    		cur=cur->next;
    	}
    	return count;
    }
    void InsertDClist(Dlistnode *head,int pos)
    {	
    	if(pos<1||pos>lengthlist(head)+1){
    		printf("插入位置非法
    ");
    		return ;
    	}
    	Dlistnode *cur=head,*newnode;
    	int data;
    	//完成插入点数据建立
    	if(NULL==(newnode=(Dlistnode *)malloc(sizeof(Dlistnode))))
    	{
    		printf("创建失败
    ");
    		exit(-1);
    	}
    	printf("请输入要插入的节点数据:
    ");
    	scanf("%d",&data);
    	newnode->data=data;
    	for(int i=0;i<pos;i++)	//查找要插入的位置
    		cur=cur->next;
    	//开始插入
    	cur->prior->next=newnode;	//插入需要4步
    	newnode->prior=cur->prior;
    	newnode->next=cur;
    	cur->prior=newnode;
    }
    void DeleteDClist(Dlistnode *head,int pos)
    {
    	if(pos<1||pos>lengthlist(head)){
    		printf("删除位置非法
    ");
    		return ;
    	}
    	Dlistnode *cur=head;
    	for(int i=0;i<pos;i++)	//查找要删除节点的位置
    		cur=cur->next;
    	//删除 需要3步
    	cur->prior->next=cur->next;	//前一个指针指向删除节点的下一个
    	cur->next->prior=cur->prior;//删除节点的前驱指针 指向删除节点前一个节点
    	free(cur);//释放堆
    }


  • 相关阅读:
    vue loading 插件编写与实战
    vue tab切换的几种方式
    IOS安卓常见问题
    vue.js 跨域的解决方法(转载)
    vue 根据设计稿rem适配
    vuex 文件夹目录整理
    vue项目优化之路由懒加载
    flutter 环境配置-01
    锚点 定位 导航 滑动
    居中 子元素无固定 宽高
  • 原文地址:https://www.cnblogs.com/james1207/p/3281551.html
Copyright © 2011-2022 走看看