Java single-chain table implementation

Source: Internet
Author: User
/*****/Package COM. handy. DS;/*** @ author handy 2012-3-15 */class node {int data; node next; Public node () {} public node (INT data, node next) {This. data = data; this. next = next ;}} public class singlelinkedlist {private node head;/*** @ return the head */Public node gethead () {return head ;} /*** @ Param head * the head to set */Public void sethead (node head) {This. head = head;} public singlelinkedlist () {Head = new node ();} public Boolean isempty () {If (head. next = NULL) return true; elsereturn false;} public Boolean addtolast (int elem) {If (isempty () {head. next = new node (ELEM, null); Return true;} else {node curr = head. next, Prev = NULL; while (curr! = NULL) {Prev = curr; curr = curr. next;} node newnode = new node (ELEM, null); Prev. next = newnode; return true ;}} public Boolean addtofirst (int elem) {If (isempty () {head. next = new node (ELEM, null); Return true;} else {node P = new node (ELEM, head. next); head. next = P; return true ;}} public void printlist () {If (head. next = NULL) {system. out. print ("null");} For (node P = head. next; P! = NULL; P = P. next) system. out. print (P. data + ","); system. out. println ();} public Boolean insert (INT posvalue, int ELEM) {If (isempty () & posvalue = 0) {head. next = new node (ELEM, null); Return true;} else {node Prev = head, curr = head. next, newnode = new node (ELEM, null); While (curr! = NULL & curr. Data! = Posvalue) {If (curr. next = NULL) return false; Prev = curr; curr = curr. next;} newnode. next = curr; Prev. next = newnode; return true;} public int removefirst () {If (isempty () {return-1;} else {node temp = head. next; If (temp. next! = NULL) // there are more than two nodes head = temp. next; elsehead. next = NULL; // return temp for a node. data ;}} public int removelast () {If (isempty () {return-1;} else {int temp = 0; node Prev = head, curr = head. next; while (curr! = NULL) {If (curr. next = NULL) {temp = curr. data; Prev. next = NULL; // system. out. println (1);} Prev = curr; curr = curr. next; return temp;} public Boolean remove (int elem) {If (isempty () return false; else {node Prev = head, curr = head. next; while (curr! = NULL & curr. Data! = ELEM) {If (curr. next = NULL) {return false;} Prev = curr; curr = curr. next;} Prev. next = curr. next; return true ;}// return the number of duplicate nodes public int getsamenumber (INT value) {int COUNT = 0; node curr = head. next; while (curr! = NULL) {If (curr. data = value) Count ++; curr = curr. next;} return count;} // remove the public Boolean removesame () {node P = head; node curr1 = head. next; node curr2 = NULL; int temp; while (curr1! = NULL) {If (getsamenumber (curr1.data)> 1) {temp = curr1.data; curr2 = curr1; while (curr2! = NULL) {// remove all nodes whose values are temp (temp); curr2 = curr2.next;} curr1 = curr1.next;} return true;} // 1. set public Boolean reverse1 () {If (isempty () return false; node P = head, q = head. next; node R; while (Q! = NULL) {r = Q. next; q. next = P; P = Q; q = r;} head. next. next = NULL; head. next = P; return true;} // 2. reverse public Boolean reverse2 () {If (isempty () return false; int size = 0; For (node P = head. next; P! = NULL; P = P. next) size ++; int [] array = new int [size]; int I; node P; For (P = head. next, I = 0; P! = NULL & I <size; P = P. next, I ++) array [I] = P. data; For (P = head. next, I = 0; P! = NULL & I <size; P = P. next, I ++) p. data = array [size-1-I]; return true;} // checks whether a circular public Boolean iscontainsloop () {node P = head, n = P; while (N. next! = NULL & P! = NULL) {P = P. next; n = n. next. next; If (P = N) return true;} return false;} // change the linked list to public void turntolooplink () {node P = head; while (P. next! = NULL) {P = P. next;} p. next = head. next;} // remove the circular public void turntonolooplink () {node P = head. next; while (P. next! = NULL & P. Next! = Head. next) {P = P. next;} p. next = NULL;} // return the linked list size public int getsize () {node P = head; int size = 0; while (P. next! = NULL) {size ++; P = P. next;} return size;} // a single-chain table is sorted by data in ascending order. Public void sortlist () {node P = head. next; int size = This. getsize (); For (INT I = 1; I <size; I ++) {P = head. next; For (Int J = 0; j <size-I; j ++) {If (P. next! = NULL & P. DATA> P. next. data) {int temp = P. data; p. data = P. next. data; p. next. data = temp;} p = P. next ;}}}}

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.