zoukankan      html  css  js  c++  java
  • C++对双链表的操作

    //C++对双链表的操作
    #include <iostream>
    using namespace std;
    
    typedef struct node
    {
    	int data;
    	struct node *left;
    	struct node *right;
    }node;
    
    typedef struct single
    {
    	int data;
    	struct single *next;
    }single;
    
    /*
    创建一个双链表
    */
    
    node* Create()
    {
    	struct node *head;
    	struct node *p;
    	struct node *s;
    	head = (node*)malloc(sizeof(node));
    	p = head;
    	int cycle = 1;
    	int x;
    	while(cycle)
    	{
    		cout<<"请输入数据"<<endl;
    		cin>>x;
    		cout<<' ';
    		if(x!=0)
    		{
    			s = (node*)malloc(sizeof(node));
    			s->data = x;
    			p->right = s;
    			s->left = p;
    			p= s;
    		}
    		else
    		{
    			cycle = 0;
    		}
    	}
    	head = head->right;
    	head->left = NULL;
    	p->right = NULL;
    	return (head);
    }
    
    /*
    建立一个空的循环单链表
    */
    int InitList(struct single *head,int n)
    {
    	struct single *p;
    	head = (single*)malloc(sizeof(single));
    	p = head;
    	int cycle = 1;
    	
    	for(int i = 0;i<n;i++)
    	{
    		p = (single*)malloc(sizeof(single));
    		p = p->next;
    	}
    	return 1;
    }
    
    
    void main()
    {
    	Create();
    }
    
  • 相关阅读:
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
    POJ 1840 Eqs(乱搞)题解
    UVALive 6955 Finding Lines(随机化优化)题解
    CodeForces 828E DNA Evolution(树状数组)题解
    UVA 11019 Matrix Matcher(二维hash + 尺取)题解
  • 原文地址:https://www.cnblogs.com/CBDoctor/p/2624137.html
Copyright © 2011-2022 走看看