babby's first linked list
This commit is contained in:
parent
bbbe4d7cdc
commit
b62542f302
25
cs161BW8/agenda.org
Normal file
25
cs161BW8/agenda.org
Normal file
|
@ -0,0 +1,25 @@
|
|||
* Agenda
|
||||
** Tuesday
|
||||
+ Pointers as arguments to functions
|
||||
+ Pointers by value
|
||||
+ Pointers by reference
|
||||
+ Structs and pointers to makes lists
|
||||
+ Finally
|
||||
+ Long example: linked lists with pointers
|
||||
+ First: combining structs and pointers
|
||||
+ Pointers to structs
|
||||
+ Structs with pointers
|
||||
+ Second: A recursively defined struct
|
||||
+ Saved from circularity with the power of pointers!
|
||||
+ Third: Traversing our linked list
|
||||
+ Recursive function definitions
|
||||
+ Adding to the front of the list
|
||||
+ Adding to the back of the list
|
||||
+ Removing from the front of the list
|
||||
+ Removing from the end of the list
|
||||
+ Adding/removing from arbitrary positions
|
||||
+ Fourth: bonus operations
|
||||
+ Summing all elements of a list
|
||||
** Thursday
|
||||
+ Linux, linux, and more linux
|
||||
+ Spillover from Tuesday
|
65
cs161BW8/listStruct.cpp
Normal file
65
cs161BW8/listStruct.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Goal:
|
||||
Implement lists not with arrays but with
|
||||
structs that contain pointers
|
||||
|
||||
commonly called linked lists
|
||||
|
||||
|
||||
|1| -> |2| -> |5| -> |6| -> nullptr
|
||||
*/
|
||||
|
||||
|
||||
// bad definition leads to size
|
||||
// equation of the form
|
||||
// size(struct) = size(struct) + 1
|
||||
|
||||
// good definition leads to size equation
|
||||
// of the form
|
||||
// size(struct) = 1 + 1
|
||||
struct Node {
|
||||
int data;
|
||||
Node* next;
|
||||
};
|
||||
|
||||
Node* makeNode(int v){
|
||||
Node* n = new Node;
|
||||
n->data = v;
|
||||
n->next = nullptr;
|
||||
return n;
|
||||
}
|
||||
|
||||
// revist this when we add deletion
|
||||
void append(Node* lst, int v){
|
||||
if(lst->next == nullptr){
|
||||
Node* n = makeNode(v);
|
||||
lst->next = n;
|
||||
}
|
||||
else{
|
||||
append(lst->next,v);
|
||||
}
|
||||
}
|
||||
|
||||
void printList(Node* lst){
|
||||
if(lst != nullptr){
|
||||
cout << lst->data << " ";
|
||||
printList(lst->next);
|
||||
}
|
||||
else{
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
Node* ourList = makeNode(0);
|
||||
append(ourList,1);
|
||||
append(ourList,2);
|
||||
append(ourList,3);
|
||||
|
||||
printList(ourList);
|
||||
|
||||
return 0;
|
||||
}
|
65
cs161BW8/listStruct1.cpp
Normal file
65
cs161BW8/listStruct1.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Goal:
|
||||
Implement lists not with arrays but with
|
||||
structs that contain pointers
|
||||
|
||||
commonly called linked lists
|
||||
|
||||
|
||||
|1| -> |2| -> |5| -> |6| -> nullptr
|
||||
*/
|
||||
|
||||
|
||||
// bad definition leads to size
|
||||
// equation of the form
|
||||
// size(struct) = size(struct) + 1
|
||||
|
||||
// good definition leads to size equation
|
||||
// of the form
|
||||
// size(struct) = 1 + 1
|
||||
struct Node {
|
||||
int data;
|
||||
Node* next;
|
||||
};
|
||||
|
||||
Node* makeNode(int v){
|
||||
Node* n = new Node;
|
||||
n->data = v;
|
||||
n->next = nullptr;
|
||||
return n;
|
||||
}
|
||||
|
||||
// revist this when we add deletion
|
||||
void append(Node* lst, int v){
|
||||
if(lst->next == nullptr){
|
||||
Node* n = makeNode(v);
|
||||
lst->next = n;
|
||||
}
|
||||
else{
|
||||
append(lst->next,v);
|
||||
}
|
||||
}
|
||||
|
||||
void printList(Node* lst){
|
||||
if(lst != nullptr){
|
||||
cout << lst->data << " ";
|
||||
printList(lst->next);
|
||||
}
|
||||
else{
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
Node* ourList = makeNode(0);
|
||||
append(ourList,1);
|
||||
append(ourList,2);
|
||||
append(ourList,3);
|
||||
|
||||
printList(ourList);
|
||||
|
||||
return 0;
|
||||
}
|
65
cs161BW8/listStructStr.cpp
Normal file
65
cs161BW8/listStructStr.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Goal:
|
||||
Implement lists not with arrays but with
|
||||
structs that contain pointers
|
||||
|
||||
commonly called linked lists
|
||||
|
||||
|
||||
|1| -> |2| -> |5| -> |6| -> nullptr
|
||||
*/
|
||||
|
||||
|
||||
// bad definition leads to size
|
||||
// equation of the form
|
||||
// size(struct) = size(struct) + 1
|
||||
|
||||
// good definition leads to size equation
|
||||
// of the form
|
||||
// size(struct) = 1 + 1
|
||||
struct Node {
|
||||
int data;
|
||||
Node* next;
|
||||
};
|
||||
|
||||
Node* makeNode(int v){
|
||||
Node* n = new Node;
|
||||
n->data = v;
|
||||
n->next = nullptr;
|
||||
return n;
|
||||
}
|
||||
|
||||
// revist this when we add deletion
|
||||
void append(Node* lst, int v){
|
||||
if(lst->next == nullptr){
|
||||
Node* n = makeNode(v);
|
||||
lst->next = n;
|
||||
}
|
||||
else{
|
||||
append(lst->next,v);
|
||||
}
|
||||
}
|
||||
|
||||
void printList(Node* lst){
|
||||
if(lst != nullptr){
|
||||
cout << lst->data << " ";
|
||||
printList(lst->next);
|
||||
}
|
||||
else{
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
Node* ourList = makeNode(0);
|
||||
append(ourList,1);
|
||||
append(ourList,2);
|
||||
append(ourList,3);
|
||||
|
||||
printList(ourList);
|
||||
|
||||
return 0;
|
||||
}
|
69
cs161BW8/listStructT.cpp
Normal file
69
cs161BW8/listStructT.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Goal:
|
||||
Implement lists not with arrays but with
|
||||
structs that contain pointers
|
||||
|
||||
commonly called linked lists
|
||||
|
||||
|
||||
|1| -> |2| -> |5| -> |6| -> nullptr
|
||||
*/
|
||||
|
||||
|
||||
// bad definition leads to size
|
||||
// equation of the form
|
||||
// size(struct) = size(struct) + 1
|
||||
|
||||
// good definition leads to size equation
|
||||
// of the form
|
||||
// size(struct) = 1 + 1
|
||||
template <typename T>
|
||||
struct Node {
|
||||
T data;
|
||||
Node<T>* next;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Node<T>* makeNode(T d){
|
||||
Node<T>* n = new Node<T>;
|
||||
n->data = d;
|
||||
n->next = nullptr;
|
||||
return n;
|
||||
}
|
||||
|
||||
// revist this when we add deletion
|
||||
template <typename T>
|
||||
void append(Node<T>* lst, T d){
|
||||
if(lst->next == nullptr){
|
||||
Node<T>* n = makeNode(d);
|
||||
lst->next = n;
|
||||
}
|
||||
else{
|
||||
append(lst->next,d);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void printList(Node<T>* lst){
|
||||
if(lst != nullptr){
|
||||
cout << lst->data << " ";
|
||||
printList(lst->next);
|
||||
}
|
||||
else{
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
Node<int>* ourList = makeNode(0);
|
||||
append(ourList,1);
|
||||
append(ourList,2);
|
||||
append(ourList,3);
|
||||
|
||||
printList(ourList);
|
||||
|
||||
return 0;
|
||||
}
|
41
cs161BW8/pointerFunc.cpp
Normal file
41
cs161BW8/pointerFunc.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
function that takes two pointers as arguments,
|
||||
averages the values stored at the pointers,
|
||||
then swaps the values, returns the average
|
||||
*/
|
||||
int averageAndSwap(int* p1, int* p2){
|
||||
int v1 = *p1; //follow the address in p1 to the value v1
|
||||
int v2 = *p2; //same for p2 and v2
|
||||
|
||||
*p1 = v2;
|
||||
*p2 = v1;
|
||||
|
||||
return (v1 + v2) / 2;
|
||||
}
|
||||
|
||||
int averageAndSwapRef(int &x1, int &x2){
|
||||
int temp = x1;
|
||||
|
||||
x1 = x2;
|
||||
x2 = temp;
|
||||
|
||||
return (x1 + x2)/2;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int var1 = 10;
|
||||
int var2 = 20;
|
||||
|
||||
averageAndSwap(&var1, &var2);
|
||||
|
||||
cout << var1 << " " << var2 << endl;
|
||||
|
||||
averageAndSwapRef(var1,var2);
|
||||
|
||||
cout << var1 << " " << var2 << endl;
|
||||
|
||||
return 0;
|
||||
}
|
43
cs161BW8/pointerFuncOnceMore.cpp
Normal file
43
cs161BW8/pointerFuncOnceMore.cpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int averageAndSwapPtrRef(int* &p1, int* &p2){
|
||||
int* temp = p1;
|
||||
p1 = p2;
|
||||
p2 = temp;
|
||||
|
||||
return (*p1 + *p2)/2;
|
||||
}
|
||||
|
||||
int averageWrong(int* p1, int* p2){
|
||||
int* temp = p1;
|
||||
p1 = p2;
|
||||
p2 = temp;
|
||||
|
||||
return (*p1 + *p2)/2;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
int* p1 = new int;
|
||||
int* p2 = new int;
|
||||
|
||||
*p1 = 10;
|
||||
*p2 = 20;
|
||||
|
||||
cout << "Address in p1: "<< p1 << endl;
|
||||
cout << "Address in p2: "<< p2 << endl;
|
||||
|
||||
averageWrong(p1,p2);
|
||||
|
||||
cout << "Address in p1: "<< p1 << endl;
|
||||
cout << "Address in p2: "<< p2 << endl;
|
||||
|
||||
cout << "Value in p1: " << *p1 << endl;
|
||||
cout << "Value in p2: " << *p2 << endl;
|
||||
|
||||
delete p1;
|
||||
delete p2;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user