Skip to content

Commit

Permalink
增加lecture_2_guide
Browse files Browse the repository at this point in the history
  • Loading branch information
lzz19980125 committed Dec 9, 2021
1 parent 237eaf8 commit 8b9cd94
Show file tree
Hide file tree
Showing 30 changed files with 906 additions and 5 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions lecture/.idea/$PROJECT_FILE$

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lecture/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions lecture/.idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lecture/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lecture/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

465 changes: 465 additions & 0 deletions lecture/.idea/qaplug_profiles.xml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions lecture/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added lecture/out/production/lecture/IntList.class
Binary file not shown.
Binary file added lecture/out/production/lecture/SList$IntNode.class
Binary file not shown.
Binary file added lecture/out/production/lecture/SList.class
Binary file not shown.
Binary file added lecture/out/production/lecture/lecture2_1.class
Binary file not shown.
Binary file added lecture/out/production/lecture/lecture2_2.class
Binary file not shown.
Binary file added lecture/out/production/lecture/lecture2_4.class
Binary file not shown.
5 changes: 5 additions & 0 deletions lecture_2_未完成的任务.md
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的值?
16 changes: 16 additions & 0 deletions lecture_guide/lecture_guide.iml
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>
50 changes: 50 additions & 0 deletions lecture_guide/src/IntNode.java
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());
}
}
65 changes: 65 additions & 0 deletions lecture_guide/src/Lists1Exercises.java
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));
}
}
119 changes: 119 additions & 0 deletions lecture_guide/src/SLList.java
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;
}
}
}
Loading

0 comments on commit 8b9cd94

Please sign in to comment.