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);//释放堆
    }


  • 相关阅读:
    css常用字体
    多行文本显示省略号,点击展开隐藏
    某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的, 加密规则如下:每位数字都加上5,然后用除以10的余数代替该数字,再将第一位和第四位交换, 第二位和第三位交换,请编写一个函数,传入原文,输出密文
    编写一个函数,计算任意两个数字之间所能组成的奇数个数,数字必须是个位数。 比如:计算0~3之间能组成的奇数是: 01、03、13、21、23、31
    Redis(一) 数据结构与底层存储 & 事务 & 持久化 & lua
    IO多路复用之Reactor
    IO多路复用之select poll epoll
    五种IO模型
    kafka(五) 流式处理 kafka stream
    kafka(二) 高性能技术分析
  • 原文地址:https://www.cnblogs.com/james1207/p/3281551.html
Copyright © 2011-2022 走看看