Using nodes to implement linked list LinkedList, using arrays and nodes to implement stack stack, using arrays and nodes linked lists to implement queue queues

Source: Internet
Author: User

One, using nodes to implement the linked list LinkedList, without changing the JAVAAPI set frame

Import Java.util.scanner;public class Main {public static class Node {int data;    Node Next=null;    public Node (int data) {this.data=data;};        } public static class Mylinkedlist {Node head=null;    Public mylinkedlist () {}//Add node public void AddNode (int d) {node newnode=new node (d);    if (head==null) {head=newnode;    return;     } Node Tmp=head;     while (tmp.next!=null) {//Find end Tmp=tmp.next;       } Tmp.next=newnode; }//list length public int size (Node head) {int count=0; Node Tmp=head;while (tmp.next!=null) {count++;tmp=tmp.next;} return count+1;} Delete Node public boolean deletenode (int d) {if (Head.data==d) {Head=head.next;return true;} Node Prenode=head; Node Curnode=head.next;while (curnode!=null) {if (Curnode.data==d) {Prenode.next=curnode.next;return true;} Prenode=curnode;curnode=curnode.next;} return false;} Sorts the linked list, returning the sorted head node public node orderlist () {node nextnode=null;int temp=0; Node Curnode=head;while (curnode.next!=null) {nextnode=curnode.next;while (nextnode!=null) {if (curnode.data>nextnode.data) {//exchange data only temp = Curnode.data;curnode.data=nextnode.data;nextnode.data= temp;} Nextnode=nextnode.next;} Curnode=curnode.next;} Select the sorting algorithm/*while (curnode.next!=null) {nextnode=curnode.next; Node k=curnode;int Tmp=curnode.data;while (nextnode!=null) {if (nextnode.data<tmp) {tmp=nextnode.data; k=nextNode;} Nextnode=nextnode.next;} if (k!=curnode) {int t=k.data;k.data=curnode.data;curnode.data=t;} Curnode=curnode.next;} */return Head;} Print List public void Printlist () {Node tmp=head;{    while (Tmp!=null) {System.out.print (tmp.data+ ""); tmp=tmp.next;}}} public static void Main (string[] args) {Scanner in=new Scanner (system.in); int n=in.nextint (); int[] Nodes=new int[n]; fo         R (int i=0;i<n;i++) {nodes[i]=in.nextint ();}         int K=in.nextint ();         Mylinkedlist list=new mylinkedlist ();         for (int i=0;i<n;i++) {list.addnode (nodes[i]);}                  LinkedList list2=new LinkedList ();         Node P=findkthtotail (list.head,k); System. OUT.PRINTLN (P.data);} Single-linked list finds the number of the penultimate K public static node Findkthtotail (node Head,int k) {if (k<=0| | Head==null) {return null;} Node P1=head; Node p2=head;for (int i=0;i<k+1;i++) {//GO first to K step P1=p1.next;} while (p1!=null) {p1=p1.next;p2=p2.next;} return p2;}}
two, implementing stack stack

1, using the general array object[] implementation stack

Import Java.util.arrays;public class Main {public static class mystack<e>{private object[] stack;//using an array to implement the stack Priva  Te int size;//The size of the array public mystack () {stack=new object[10];  } public boolean IsEmpty () {return size==0;  Public E Peek () {if (IsEmpty ()) {return null;    } return (E) stack[size-1];   } public E Pop () {e e=peek ();   Stack[size-1]=null;   size--;    return e;  Public boolean push (E item) {ensurecapacity (size+1);  Stack[size++]=item;  return true;  } private void ensurecapacity (int size) {int len=stack.length;  if (len<size) {int newlen=10;    stack=arrays.copyof (Stack, newlen);  }}}public static void Main (string[] args) {mystack<integer> s=new mystack<integer> ();  S.push (52);  S.push (20);  S.push (96);  System.out.println ("Number of elements in stack:" +s.size);  System.out.println ("Stack top element:" +s.pop ());  System.out.println ("Stack top element:" +s.pop ()); }}
2, using the node linked list to implement the stack, which is always inserted in the list header, the left extension . Let us first ignore the JAVAAPI provided by the set framework, I mainly discuss the idea

public class Main {//Node class public static class node<e>{E data;            Node<e> next=null;public Node (E data) {this.data = data;} } public static class mystack<e>{node<e> top=null;//equivalent to the link header public boolean push (E d) {No    De<e> newnode=new node<e> (d);    if (top!=null) {newnode.next=top;//has been inserted in the chain header, want to expand to the left} Top=newnode;    return true;    Public E Peek () {if (top==null) return null;    return top.data;    } public E Pop () {e e=peek ();    Top=top.next;    return e;    } public boolean IsEmpty () {return top==null;    } public int size () {if (IsEmpty ()) {return 0;        } node<e> Tmp=top;    int size=0;    while (tmp!=null) {size++;    Tmp=tmp.next;    } return size;  }}public static void Main (string[] args) {mystack<integer> s=new mystack<integer> ();  S.push (52);  S.push (20);  S.push (96);  System.out.println ("Number of elements in stack:" +s.size ()); System.out.println("Stack top element:" +s.pop ()); System.out.println ("Stack top element:" +s.pop ());}}
third, implement queue queues

1, using the array to implement the queue, at this time with the set frame LinkedList is the best method

Import Java.util.arraylist;import Java.util.linkedlist;public class Main {public static class myqueue<e>{PRI Vate linkedlist<e> queue;//is a collection framework for the API, why use LinkedList.    Because LinkedList has AddLast and AddFirst method, can operate at both ends arbitrarily, use ArrayList to compare trouble private int size=0;    Public Myqueue () {queue=new linkedlist<e> ();    } public boolean Enqueue (E e) {queue.addlast (e);    size++;    return true;    } public E Dequeue () {size--;    return Queue.removefirst ();    } public boolean Empty () {return size==0;    } public int size () {return queue.size ();    } public void PrintQueue () {System.out.println (queue.tostring ());  }}public static void Main (string[] args) {myqueue<integer> q=new myqueue<integer> ();  Q.enqueue (52);  Q.enqueue (42);  Q.enqueue (71);  Q.enqueue (23);  Q.printqueue ();  Q.dequeue ();  Q.printqueue ();  Q.dequeue ();  Q.printqueue ();  Q.dequeue ();  Q.printqueue (); /* * [52, 42, 71, 23] [42, 71, 23] [71, 23] [23]   *    * */     }} 
2, the node linked list to implement the queue.

public class Main {public static class node<e>{e data; Node<e> next=null;public Node (E data) {This.data=data;}} public static Class Myqueue<e>{node<e> Head=null,tail=null;public Boolean Enqueue (e e) {node<e> Newnode=new node<e> (E); if (head==null&&tail==null) {Head=newnode;tail=newnode;} Tail.next=newnode;tail=newnode;return true;} Public E Dequeue () {if (head==null) {return null;} e e=head.data;head=head.next;return e;} public Boolean isEmpty () {return head==tail;} public int size () {node<e> tmp=head;int count=0;while (tmp!=null) {count++;tmp=tmp.next;} return count;} public void PrintQueue () {node<e> tmp=head;while (tmp!=null) {System.out.print (tmp.data+ ""); tmp=tmp.next;}    System.out.println (); }}public static void Main (string[] args) {//TODO auto-generated method stub myqueue<integer> q=new Myqueue<integ  Er> ();  Q.enqueue (52);  Q.enqueue (42);  Q.enqueue (71);  Q.enqueue (23);  Q.printqueue ();  Q.dequeue ();  Q.printqueue ();  Q.dequeue (); Q. PrintQueue ();  Q.dequeue (); /*52 23 <span style= "White-space:pre" ></span> *42 q.printqueue 23 <span style= " White-space:pre "></span> *71 23  *23 */}}


Using nodes to implement linked list LinkedList, using arrays and nodes to implement stack stack, using arrays and nodes linked lists to implement queue queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.