zoukankan      html  css  js  c++  java
  • 单链表复习

    单链表复习


    作者:vpoet

    mails:18200268879@163.com

    注:转载请注明出处,谢谢合作


    #include <iostream>
    #include <stack>
    using namespace std;
    
    typedef struct ListNode
    {
    	int data;
    	struct ListNode* next;
    }NODE;
    
    NODE *CreateList()
    {
    	NODE * head,*p,*s;
    
    	head=(NODE*)malloc(sizeof(NODE));
    	p=head;
    
    	int LinkData;
    	int InputIndex=1;
    	while(InputIndex)
    	{
    		cout<<"Please input the Node data.(if 0 is inputed,CreateLink Over!): ";
    		cin>>LinkData;
    		if(0!=LinkData)
    		{
    			s=(NODE*)malloc(sizeof(NODE));
    			s->data=LinkData;
    			p->next=s;
    			p=s;
    		}
    		else
    		{
    			InputIndex=0;
    		}
    	}
    
    	head=head->next;
    	p->next=NULL;
    
    	return head;
    }
    
    void Print(NODE *head)
    {
    	NODE *p=head;
    	cout<<"The LinkList is: ";
    	while(p!=NULL)
    	{
    		cout<<p->data<<"  ";
    		p=p->next;
    	}
    }
    
    void ListLength(NODE*head)
    {
    	
    	NODE* p=head;
    	int count =0;
    	while(p!=NULL)
    	{
    		count++;
    		p=p->next;
    	}
    	cout<<"The length is: "<<count<<endl;
    }
    
    void InsertNode(NODE* head)
    {
    	NODE *p=head;
    	
    	while(p->next!=NULL)
    	{
    		p=p->next;
    	}
    
    	NODE *New;
    	New=(NODE*)malloc(sizeof(NODE));
    	cout<<"Please input the new data: ";
    	cin>>New->data;
    	p->next=New;
    	New->next=NULL;
    	
    	head=p;
    }
    
    void ReversePrint(NODE* head)
    {
    	stack <NODE*> StackNode;
    	NODE *p=head;
    	while(p!=NULL)
    	{
    		StackNode.push(p);
    		p=p->next;
    	}
    	cout<<"Reverse to Print: ";
    	while(!StackNode.empty())
    	{
    		NODE* temp;
    		temp=StackNode.top();
    		StackNode.pop();
    		cout<<temp->data<<" ";
    	}
    }
    
    void DeleteNode(NODE *head,int DelValue)
    {
    	NODE *p=head;
    	
    	while(p!=NULL)
    	{
    		if(p->next->data==DelValue)
    		{
    			p->next=p->next->next;
    			break;
    		}
    		else
    		{
    			p=p->next;
    		}
    	}
    	head=p;
    }
    
    
    int main()
    {
    	cout<<"Create New LinkList....
    "<<endl;
    	NODE *p=CreateList();
    	cout<<"Print the LinkList.....
    "<<endl;
    	Print(p);
    	cout<<"Print the length of LinkList...
    "<<endl;
    	ListLength(p);
    
    	cout<<"Insert a New LinkNode....
    "<<endl;
    	InsertNode(p);
    	cout<<"Cout The New LinkNode....
    "<<endl;
    	Print(p);
    
    	cout<<"Reverse to input the LinkList...
    "<<endl;
    	ReversePrint(p);
    
    	cout<<"Delete a Node in LinkList....
    "<<endl;
    	int DelValue;
    	cout<<"Please input the Delete Node Value"<<endl;
    	cin>>DelValue;
    	DeleteNode(p,DelValue);
    	cout<<"Print the del Node LinkList....
    "<<endl;
    	Print(p);
    	cout<<endl;
    	return 0;
    }
    


  • 相关阅读:
    Mimblewimble:新型的隐私保护协议
    权益证明生态系统
    理解去中心化身份
    TPS 是一种糟糕的评价标准
    以太坊 2.0 :双生以太奇谭
    以太坊 2.0:信标链
    以太坊 2.0:验证者详解
    论共识机制
    以太坊钱包开发系列
    将不确定变成确定~LINQ DBML模型可以对应多个数据库吗
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6918812.html
Copyright © 2011-2022 走看看