单向链表 头尾插法

单向链表  头尾插法

#include<iostream.h>
#include<conio.h> 
struct node
{
   int data;
   node *next;       
       
};

node *createlist(int );
node *createlistinvert(int );
void outputlist(node *head); 

int main()
{
  int n;
  node *listhead=NULL;
  
  cout<<"请输入节点数目: ";
  cin>>n;

  cout<<"**************下面请您输入"<<n<<"个整数*****************\n"<<endl; 
  if(n>0)
    {
         cout<<"      ***************尾差法********************\n"<<endl; 
        listhead=createlist(n); 
      
        outputlist(listhead);
        
         
        cout<<" \n\n    *****************头插法*********************\n"<<endl; 
         cout<<"**************下面请您输入"<<n<<"个整数*****************\n"<<endl; 
         
        listhead=createlistinvert(n);
        
        outputlist(listhead); 
     } 
  
  
  getch(); 
  return 0;
 
}
//尾插法 
node *createlist(int n)
{
   node *temp=NULL,*tail=NULL,*head=NULL;
   
   int num;
   
   cout<<"请输入数据: "; 
   cin>>num;
   
   head=new node;
   
   if(head==NULL)
     {
        cout<<"内存分配不成功,重试吧!";            
        
        return NULL;         
                 
     }     
     
   else
    {
       head->data=num;       
       head->next=NULL;
       tail=head;
           
           
    }
    
    for(int i=0;i<n-1;i++)
      {
      
         cin>>num;
         
         temp=new node;
         
         if(temp==NULL)
           {
              cout<<"内存分配不成功";
                          
              return head;         
                       
           }
               
          else
            {
                temp->data=num;
                temp->next=NULL;
                tail->next=temp;
                
                tail=temp;
                       
            }
            
      }
     
     return head;
     
}

//头插法
node *createlistinvert(int n)

{
      node *temp=NULL,*head=NULL;
      
      cout<<"请输入数据:"; 
      int num;
      cin>>num;
       
      head=new node;
      
      if(head==NULL)
        {
           cout<<"内存分配不成功!";   
          return NULL; 
                    
                    
        }  
        else
        {
           head->data=num;
               
           head->next=NULL; 
            
            
        }   
     for(int i=0;i<n-1;i++)
     {
           cin>>num;
           
           temp=new node;
             
         if(temp==NULL)
          {
          cout<<"内存分配不成功!";            
        
          return head;         
                 
          }    
     else 
       {
          temp->data=num;       
          temp->next=head; 
          
          head=temp; 
              
       } 
    
   }
  
  return head; 
 } 
 
 
 void outputlist(node *head)
 {
      cout<<"List:   "; 
      node *curnode=head;
       
     while(curnode)
       {
         cout<<curnode->data;
         if(curnode->next)
              cout<<"  -> ";
              
        curnode=curnode->next; 
       
       }     
      
  cout<<endl;    
      
 } 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值