zoukankan      html  css  js  c++  java
  • 【DataStructure】Some useful methods about linkedList(三)

    Method 4: Gets the value of element number i

    For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then get(list, 2) will return 44.

    Solution 1:
    	static int get(Node list, int i) {
    		if (i < 0) {
    			throw new IllegalArgumentException();
    		}
    		for (int j = 0; j < i; j++) {
    			if (list == null) {
    				throw new IllegalStateException();
    			}
    			list = list.next;
    		}
    		return list.data;
    	}
    Solution 2:
    	static int get(Node list, int i)
    	{
    		Node p = list;
    		int j = 0;
    		while (j < i && p != null) {
    		++j;
    		p = p.next;
    		}
    		if(p == null)
    		{
    			throw new java.util.NoSuchElementException();
    		}
    		return p.data;
    	}

    The output is as follows:


    Method 5:inserts x as element number i;

    For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then put(list, 3, 50) will change List to {22, 33, 44, 50, 55, 66, 44, 88, 99}. 

    Hint: if i= 0, replace the value of the first node With x, and insert a new node immediately after it that contains the previous fist value.

    Solution 1:
    
    	static void put(Node list, int i, int x) {
    		if (list == null) {
    			throw new java.util.NoSuchElementException("List is Empty");
    		} else if (i ==0) {
    			list.next = new Node(list.data,list);
    			list.data = x;
    		} else {
    			Node p = list;
    			int j = 1;
    			while (j < i && p != null) {
    				++j;
    				p = p.next;
    			}
    			if (p == null)
    			{
    				String error = String.format("the list has only %d elements", j-1);;
    				throw new java.util.NoSuchElementException(error);
    			}
    			p.next = new Node(x, p.next);
    		}
    	}

    The output is as follows:


    Method 6:Swap the i element with the j element 

    For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then swap(list, 2, 5) will change List to {22, 33, 77, 55, 66, 44, 88, 99}.

    static void swap(Node list, int i, int j) {
    		if (i < 0 || j < 0) {
    			throw new IllegalArgumentException();
    		} else if (i == j) {
    			return;
    		}
    		Node p = list, q = list;
    		for (int ii = 0; ii < i; ii++) {
    			if (p == null) {
    				throw new IllegalStateException();
    			}
    			p = p.next;
    		}
    		for (int jj = 0; jj < j; jj++) {
    			if (q == null) {
    				throw new IllegalStateException();
    			}
    			q = q.next;
    		}
    		int pdata = p.data, qdata = q.data;
    		p.data = qdata;
    		q.data = pdata;
    		return;
    	}

    The output is as follows:



    Method 7: Gets a new list that contains all the elements of list1 and list2 in ascending order. List1 and list2 are both in ascending order. 

    For example, if list1is {22, 33, 55, 88} and  list2is {44, 66, 77, 99}, then merged(list1, list2)will return the new list {22, 33, 44, 55, 66, 77, 88, 99}. 

    Note that the three lists should be completely independent of each other. Changing one list should have no effect upon the others.

    static Node merged(Node list1, Node list2) {
    		Node list = new Node(0);
    		Node p = list, p1 = list1, p2 = list2;
    		while (p1 != null && p2 != null) {
    			if (p1.data < p2.data) {
    				p = p.next = new Node(p1.data);
    				p1 = p1.next;
    			} else {
    				p = p.next = new Node(p2.data);
    				p2 = p2.next;
    			}
    		}
    		while (p1 != null) {
    			p = p.next = new Node(p1.data);
    			p1 = p1.next;
    		}
    		while (p2 != null) {
    			p = p.next = new Node(p2.data);
    			p2 = p2.next;
    		}
    		return list.next;
    	}
    The output is as follows:


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    C# winfrom ListView控件实现自由设置每一行字体及背景色等
    VS2017Enterprise版本安装ImageWatch 2017问题解决
    C++ 对于函数名的操作,函数名本身和取*以及取&的区别
    C++数组和指针
    相机像素和显示分辨率
    (最简单详细)IronPython下载、安装及简单使用
    DataRow修改某一Cell的值
    DataGridView控件绑定数据之后,置顶操作
    DataGridVIew控件绑定数据之后的,增、插、删操作
    毕向东java基础课学习笔记4——标识符、常量、变量
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4828900.html
Copyright © 2011-2022 走看看