-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 双向链表(Intnode中的prev指针)(project1 待完成) | ||
|
||
# DList中会使用泛型,还没搞明白 | ||
|
||
# Lecture2_1 guide中如何不使用new操作符,就使得L的每个元素都加1,还不改变原来L的值? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="CheckStyle-IDEA-Module"> | ||
<option name="configuration"> | ||
<map /> | ||
</option> | ||
</component> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @author Li Zezhong | ||
* @create 2021-12-07 21:58 | ||
*/ | ||
public class IntNode { | ||
public int first; | ||
public IntNode rest; | ||
|
||
public IntNode(int f, IntNode r) { | ||
first = f; | ||
rest = r; | ||
} | ||
|
||
/** Return the size of the list using... recursion! */ | ||
public int size() { | ||
if(rest ==null){ | ||
return 1; | ||
} | ||
return 1+rest.size(); | ||
} | ||
|
||
/** Return the size of the list using no recursion! */ | ||
public int iterativeSize() { | ||
IntNode p = this; | ||
int size = 1; | ||
while(p.rest != null){ | ||
size +=1; | ||
p = p.rest; | ||
} | ||
return size; | ||
} | ||
|
||
/** Returns the ith value in this list.*/ | ||
public int get(int i) { | ||
if(i==0){ | ||
return first; | ||
} | ||
return rest.get(i-1); | ||
} | ||
|
||
public static void main(String[] args) { | ||
IntNode L = new IntNode(15, null); | ||
L = new IntNode(10, L); | ||
L = new IntNode(5, L); | ||
|
||
// System.out.println(L.iterativeSize()); | ||
// System.out.println(L.get(0)); | ||
System.out.println(L.iterativeSize()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @author Li Zezhong | ||
* @create 2021-12-07 20:15 | ||
*/ | ||
public class Lists1Exercises { | ||
/** Returns an IntList identical to L, but with | ||
* each element incremented by x. L is not allowed | ||
* to change. */ | ||
public static IntNode incrList(IntNode L, int x) { | ||
/* Your code here. */ | ||
int i=L.size()-1; | ||
IntNode p=new IntNode(L.get(i)+x,null); | ||
while(i>0){ | ||
i -=1; | ||
p = new IntNode(L.get(i)+x,p); | ||
} | ||
return p; | ||
} | ||
|
||
|
||
/** Returns an IntList identical to L, but with | ||
* each element incremented by x. Not allowed to use | ||
* the 'new' keyword. */ | ||
public static IntNode dincrList(IntNode L, int x) { | ||
IntNode p = L; | ||
while(p !=null){ | ||
p.first -=x; | ||
p = p.rest; | ||
} | ||
return L; | ||
} | ||
|
||
public static void main(String[] args) { | ||
IntNode L = new IntNode(5, null); | ||
L.rest = new IntNode(7, null); | ||
L.rest.rest = new IntNode(9, null); | ||
|
||
// System.out.println(L.size()); | ||
// System.out.println(L.iterativeSize()); | ||
|
||
// Test your answers by uncommenting. Or copy and paste the | ||
// code for incrList and dincrList into IntList.java and | ||
// run it in the visualizer. | ||
// System.out.println(L.get(1)); | ||
IntNode p = Lists1Exercises.incrList(L,2); | ||
int i=0; | ||
// System.out.println(p.size()); | ||
while(i<p.size()){ | ||
System.out.println(p.get(i)); | ||
i +=1; | ||
} | ||
i=0; | ||
while(i<L.size()){ | ||
System.out.println(L.get(i)); | ||
i +=1; | ||
} | ||
IntNode m = Lists1Exercises.dincrList(L,2); | ||
i=0; | ||
while(i<L.size()){ | ||
System.out.println(L.get(i)); | ||
i +=1; | ||
} | ||
// System.out.println(dincrList(L, 3)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* @author Li Zezhong | ||
* @create 2021-12-07 21:56 | ||
*/ | ||
public class SLList { | ||
private static class IntNode { | ||
public int item; | ||
public IntNode next; | ||
|
||
public IntNode(int i, IntNode n) { | ||
item = i; | ||
next = n; | ||
} | ||
} | ||
|
||
/* The first item (if it exists) is at sentinel.next. */ | ||
private IntNode sentinel; | ||
private int size; | ||
|
||
private static void lectureQuestion() { | ||
SLList L = new SLList(); | ||
IntNode n = new IntNode(5, null); | ||
} | ||
|
||
/** Creates an empty SLList. */ | ||
public SLList() { | ||
sentinel = new IntNode(63, null); | ||
size = 0; | ||
} | ||
public SLList(int [] a){ | ||
sentinel = new IntNode(63,null); | ||
IntNode p = sentinel; | ||
size = 0; | ||
int i=0; | ||
while(i<a.length){ | ||
p.next = new IntNode(a[i],null); | ||
p = p.next; | ||
i +=1; | ||
size +=1; | ||
} | ||
} | ||
|
||
public int get(int i){ | ||
IntNode p = sentinel.next; | ||
while(i!=0){ | ||
p = p.next; | ||
i -=1; | ||
} | ||
return p.item; | ||
} | ||
|
||
public SLList(int x) { | ||
sentinel = new IntNode(63, null); | ||
sentinel.next = new IntNode(x, null); | ||
size = 1; | ||
} | ||
|
||
/** Adds x to the front of the list. */ | ||
public void addFirst(int x) { | ||
sentinel.next = new IntNode(x, sentinel.next); | ||
size = size + 1; | ||
} | ||
|
||
/** Returns the first item in the list. */ | ||
public int getFirst() { | ||
return sentinel.next.item; | ||
} | ||
|
||
/** Adds x to the end of the list. */ | ||
public void addLast(int x) { | ||
size = size + 1; | ||
IntNode p = sentinel; | ||
|
||
/* Advance p to the end of the list. */ | ||
while (p.next != null) { | ||
p = p.next; | ||
} | ||
|
||
p.next = new IntNode(x, null); | ||
} | ||
|
||
/** Returns the size of the list. */ | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public void deleteFirst(){ | ||
sentinel.next = sentinel.next.next; | ||
size -=1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
/* Creates a list of one integer, namely 10 */ | ||
SLList L = new SLList(); | ||
L.addLast(20); | ||
L.addLast(10); | ||
L.addLast(5); | ||
int i = 0; | ||
while(i<L.size()){ | ||
System.out.println(L.get(i)); | ||
i +=1; | ||
} | ||
System.out.println(); | ||
L.deleteFirst(); | ||
i=0; | ||
while(i<L.size()){ | ||
System.out.println(L.get(i)); | ||
i +=1; | ||
} | ||
System.out.println(); | ||
i=0; | ||
int [] q = new int []{1,2,3,4,5}; | ||
SLList n = new SLList(q); | ||
while(i<n.size()){ | ||
System.out.println(n.get(i)); | ||
i +=1; | ||
} | ||
} | ||
} |