Java和c++类实现单向链表

Java和c++类实现单向链表

要毕业了最近一直在笔试或者面试,经常考到数据结构的一些问题,下面简单介绍下链表的实现:
Java:
先定义一个节点类

public class Node {
  int node;
  Node next;
  public Node(int node,Node next)//构造函数
  {
      this.node=node;
      this.next=next;
  }
 }

链表实现:

public class Mylist {
   private Node head;//定义表头节点
   private int size;
   public Mylist()//初始化
   {
       head=null;
       size=0;
   }
   public void add(int i)//添加
   {
       if(head==null)
       {
           head=new Node(i, null);
       }
       else
       {
           head=new Node(i,head);
       }
       size++;
   }
   public int size()//链表的大小
   {
       return size;

   }
   public void reverse()//链表翻转
   {
       if(head==null)
       {
           return;
       }
       else
       {
           Node end,p,q;
           p=head;
           end=null;
           while(p!=null)
           {
               q=p.next;
               p.next=end;
               end=p;
               p=q;  
           }
           head=end;
       }

   }
   public void  sort()//链表排序
   {    int temp;
       for(int i=0;i<size;i++)
       {
           for(int j=0;j<size-1-i;j++)
           {
               if(find(head,j).node>find(head,j+1).node)
               {
                   temp=find(head,j).node;
                   find(head,j).node=find(head,j+1).node;
                   find(head,j+1).node=temp;
               }
           }
       }

   }
   private Node find(Node N,int i)//查找
   {
       while(0!=i--)
           N=N.next;
       return N;
   }
   public void print()//打印链表
   {
       Node temp=head;
       while(temp!=null)
       {
           System.out.print(temp.node+" ");
           temp=temp.next;
       }
       System.out.print('\n');
   }

}

C++:

struct Node{   //类实现链表
    int node;
    Node *next;
    Node(int no,Node *ne)
        {
           node=no;
           next=ne;
        }
    };
class list{
private:
    Node *head;
    int size;
public:
    void add(int i)
        {
            if(head==NULL)
                {
                    head=new Node(i,NULL);
                }
            else
                {
                   head=new Node(i,head);
                }

        }
    void print()
        {
              Node *temp;
              temp=head;
              while(temp!=NULL)
                  {
                  cout<<temp->node<<" ";
                  temp=temp->next;
                  }
        }
    void reverse()
        {
              Node *end,*p,*q;
              end=NULL;
              p=head;
              while(p!=NULL)
                  {
                  q=p->next;
                  p->next=end;
                  end=p;
                  p=q;
                  }
           head=end;
        }
   list()
       {
            head=NULL;
            size=0;
       }
    };
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值