Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.

Commit 7e20daf

Browse files
committed
Upload files
0 parents  commit 7e20daf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+932
-0
lines changed

.DS_Store

8 KB
Binary file not shown.
54.4 KB
Binary file not shown.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
void RemoveDublicates(struct node *head) {
2+
if (head == NULL) return;
3+
4+
struct node *current = head;
5+
while (current->next != NULL) {
6+
if (current->data == current->next->data) {
7+
struct node* temp = current->next;
8+
current->next = current->next->next;
9+
free(temp);
10+
}
11+
else current = current->next;
12+
}
13+
}

stanfordcslib105/f11_MoveNode.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void MoveNode(struct node** destRef, struct node** sourceRef) {
2+
assert(*sourceRef != NULL);
3+
4+
struct node *temp = *sourceRef;
5+
*sourceRef = (*sourceRef)->next; //Rearrange source
6+
7+
temp->next = *destRef; //Move node
8+
*destRef = temp;
9+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*void AlternatingSplit(struct node *source, struct node **aRef, struct node **bRef) {
2+
struct node *current = source;
3+
for (int i = 0; current != NULL; current = current->next, i++) {
4+
if (i%2 == 0) Push(aRef, current->data);
5+
else Push(bRef, current->data);
6+
}
7+
}
8+
9+
*/
10+
void AlternatingSplit(struct node *source, struct node **aRef, struct node **bRef) {
11+
assert(source != NULL);
12+
struct node *current = source;
13+
14+
for(int i = 0; current != NULL; i++) {
15+
struct node *temp = current;
16+
current = current->next;
17+
18+
if (i % 2 == 0) MoveNode(aRef, &temp );
19+
else MoveNode(bRef, &temp );
20+
}
21+
}

stanfordcslib105/f13_ShuffleMerge.c

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// data is copied
2+
struct node* ShuffleMerge(struct node *a, struct node *b) {
3+
struct node *newHead = NULL, *currA = a, *currB = b;
4+
struct node **tail = &newHead;
5+
6+
while (currA != NULL || currB != NULL){
7+
if (currA != NULL) {
8+
Push(tail, currA->data);
9+
currA = currA->next, tail = &((**tail).next);
10+
}
11+
if(currB != NULL) {
12+
Push(tail, currB->data);
13+
currB = currB->next, tail = &((**tail).next);
14+
}
15+
}
16+
return newHead;
17+
}
18+
19+
//parameters' data wasted, not referenced so not dereferenced as NULL to check, not my choice
20+
/*
21+
struct node* ShuffleMerge(struct node *a, struct node *b) {
22+
struct node *newNode = NULL;
23+
24+
if(a == NULL) return b;
25+
else if(b == NULL) return a;
26+
else {
27+
struct node *recur = ShuffleMerge(a->next, b->next);
28+
29+
newNode = a;
30+
a->next = b;
31+
b->next = recur;
32+
}
33+
return newNode;
34+
}
35+
*/

stanfordcslib105/f14_SortedMerge.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
struct node* SortedMerge(struct node *a, struct node *b) {
3+
struct node *newHead = NULL, *temp;
4+
5+
while (a != NULL || b != NULL) {
6+
if (a != NULL) {
7+
temp = a, a = a->next;
8+
SortedInsert(&newHead, temp);
9+
}
10+
if (b != NULL) {
11+
temp = b, b = b->next;
12+
SortedInsert(&newHead, temp);
13+
}
14+
}
15+
return newHead;
16+
}
17+
*/
18+
19+
/*
20+
//Local Reference
21+
struct node* SortedMerge(struct node *a, struct node *b) {
22+
struct node *newHead = NULL;
23+
struct node **lastRef = &newHead;
24+
25+
while (1) {
26+
if ( a == NULL ) {
27+
*lastRef = b;
28+
break;
29+
}
30+
else if ( b == NULL ){
31+
*lastRef = a;
32+
break;
33+
}
34+
35+
if ( a->data >= b->data ) *lastRef = b, b = b->next, (*lastRef)->next = NULL;
36+
else *lastRef = a, a = a->next, (*lastRef)->next = NULL;
37+
lastRef = &((*lastRef)->next);
38+
}
39+
return newHead;
40+
}
41+
*/
42+
43+
//Recursion
44+
struct node* SortedMerge(struct node *a, struct node *b) {
45+
if ( a == NULL ) return b;
46+
else if ( b == NULL ) return a;
47+
48+
struct node *newHead = NULL;
49+
if ( a->data <= b->data) {
50+
newHead = a;
51+
newHead->next = SortedMerge(a->next, b);
52+
} else {
53+
newHead = b;
54+
newHead->next = SortedMerge(a, b->next);
55+
}
56+
return newHead;
57+
}

stanfordcslib105/f15_MergeSort.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
void MergeSort(struct node **headRef) {
2+
if(*headRef == NULL || (*headRef)->next == NULL) return;
3+
4+
struct node *a = NULL, *b = NULL;
5+
FrontBackSplit(*headRef, &a, &b);
6+
7+
MergeSort(&a);
8+
MergeSort(&b);
9+
10+
*headRef = SortedMerge(a,b);
11+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
struct node* SortedIntersect(struct node *a, struct node (b) {
3+
struct node* newHead = NULL;
4+
while (a != NULL && b != NULL) {
5+
if ( a->data == b->data ) {
6+
PushTail(&newHead, a->data);
7+
a = a->next;
8+
b = b->next;
9+
}
10+
else if ( a->data > b->data) b = b->next;
11+
else a = a->next;
12+
}
13+
return newHead;
14+
}
15+
*/
16+
17+
//Local referance
18+
struct node* SortedIntersect(struct node *a, struct node *b) {
19+
struct node *newHead = NULL;
20+
struct node **tailRef = &newHead;
21+
22+
while (a != NULL && b != NULL) {
23+
if ( a->data == b->data ) {
24+
Push(tailRef, a->data);
25+
tailRef = &((*tailRef)->next);
26+
a = a->next, b = b->next;
27+
}
28+
else if ( a->data < b->data) a = a->next;
29+
else b = b->next;
30+
}
31+
return newHead;
32+
}

stanfordcslib105/f17_Reverse.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
// a.k.a back-middle-front strategy
3+
void Reverse(struct node **headRef) {
4+
assert(Length(*headRef) > 1);
5+
6+
struct node *backPtr = NULL, *mainPtr = *headRef, *frontPtr = mainPtr->next;
7+
while (1){
8+
mainPtr->next = backPtr;
9+
if (frontPtr == NULL) break;
10+
backPtr = mainPtr;
11+
mainPtr = frontPtr;
12+
frontPtr = frontPtr->next;
13+
}
14+
*headRef = mainPtr;
15+
}
16+
*/
17+
18+
// Moving nodes
19+
void Reverse(struct node **headRef) {
20+
assert(*headRef);
21+
22+
struct node *result = 0, *current = *headRef;
23+
do {
24+
struct node *temp = current->next;
25+
current->next = result;
26+
result = current;
27+
current = temp;
28+
} while (current);
29+
*headRef = result;
30+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
void RecursiveReverse(struct node **headRef) {
3+
assert(*headRef); //Empty list case
4+
5+
struct node *initial = *headRef, *rest = initial->next;
6+
if (rest == NULL) return; //End of list, last initial in stack is at (n-1)th node
7+
8+
RecursiveReverse(&rest);
9+
*headRef = rest; //Final rest's value(last node's adress) is initialized via reference
10+
11+
initial->next->next = initial; //Point back
12+
initial->next = NULL;
13+
}
14+
*/
15+
16+
void RecursiveReverse(struct node **headRef) {
17+
struct node *first = *headRef, *rest = first->next;
18+
if (rest == NULL) return;
19+
20+
RecursiveReverse(&rest);
21+
22+
first->next->next = first;
23+
first->next = NULL;
24+
25+
*headRef = rest;
26+
}

stanfordcslib105/f1_Count.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int Count(struct node* head, int searchFor) {
2+
int count = 0;
3+
for(struct node* current = head; current != NULL; current = current->next) if(current->data == searchFor) count++;
4+
return count;
5+
}

stanfordcslib105/f2_GetNth.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int GetNth(struct node* head, int index) {
2+
struct node* current = head;
3+
for (int i = 0; i < index; i++, current = current->next) assert(current != NULL);
4+
return current->data;
5+
}

stanfordcslib105/f3_DeleteList.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int DeleteList(struct node** headRef) {
2+
/* Drags head to end of the list while deallocating */
3+
while (*headRef != NULL) {
4+
struct node* temp = *headRef;
5+
*headRef = (**headRef).next, free(temp);
6+
}
7+
}

stanfordcslib105/f4_Pop.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int Pop(struct node** headRef) {
2+
assert(*headRef != NULL);
3+
struct node* head = *headRef;
4+
int data = head->data;
5+
6+
*headRef = (*headRef)->next, free(head);
7+
return data;
8+
}

stanfordcslib105/f5_InsertNth.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
void InsertNth(struct node** headRef, int index, int data) {
3+
// index is array representation
4+
5+
struct node* newNode = malloc(sizeof(struct node));
6+
newNode->data = data, newNode->next = NULL;
7+
8+
struct node* current = *headRef;
9+
for (int i = 1; i < index; i++) { // current = index-1
10+
current = current->next;
11+
assert(current != NULL);
12+
}
13+
14+
if (index == 0) newNode->next = current, current = newNode;
15+
else {
16+
newNode->next = current->next;
17+
current->next = newNode;
18+
}
19+
}
20+
*/
21+
22+
void InsertNth(struct node** headRef, int index, int data) {
23+
if (index == 0) Push(headRef, data);
24+
else {
25+
struct node* newNode = malloc(sizeof(struct node));
26+
newNode->data = data, newNode->next = NULL;
27+
28+
struct node *current = *headRef;
29+
for(int i = 1; i < index; current = current->next, i++) assert(current != NULL);
30+
assert(current != NULL), Push(&(current->next), data);
31+
}
32+
}

stanfordcslib105/f6_SortedInsert.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
void SortedInsert(struct node** headRef, struct node* newNode) {
3+
if (*headRef == NULL || newNode->data < (*headRef)->data) {
4+
newNode->next = *headRef;
5+
*headRef = newNode;
6+
} else {
7+
struct node* current = *headRef;
8+
while (current->next != NULL && current->next->data < newNode->data) {
9+
current = current->next;
10+
}
11+
newNode->next = current->next;
12+
current->next = newNode;
13+
}
14+
}
15+
*/
16+
17+
void SortedInsert(struct node** headRef, struct node* newNode) {
18+
struct node** currRef = headRef;
19+
while (*currRef != NULL && (*currRef)->data < newNode->data) {
20+
currRef = &((*currRef)->next);
21+
}
22+
newNode->next = *currRef;
23+
*currRef = newNode;
24+
}

stanfordcslib105/f7_InsertSort.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "f6_SortedInsert.c"
2+
void InsertSort(struct node** headRef) {
3+
struct node *current = *headRef, *temp, *result = NULL;
4+
while (current != NULL) {
5+
temp = current->next;
6+
SortedInsert(&result, current);
7+
current = temp;
8+
}
9+
*headRef = result;
10+
}

stanfordcslib105/f8_Append.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void Append(struct node **aRef, struct node **bRef) {
2+
if (*aRef == NULL) *aRef = *bRef;
3+
else {
4+
struct node *current = *aRef;
5+
while (current->next != NULL) current = current->next;
6+
current->next = *bRef;
7+
}
8+
*bRef = NULL;
9+
}

stanfordcslib105/f9_FrontBackSplit.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
void FrontBackSplit(struct node* source, struct node** frontRef, struct node** backRef) {
2+
int len = Length(source);
3+
*frontRef = source;
4+
5+
if (len < 2) return;
6+
else {
7+
int frontend = (len % 2 == 0)? len/2: len/2 + 1;
8+
9+
struct node* current = source;
10+
for(int i = 1; i < frontend; current = current->next, i++);
11+
*backRef = current->next, current->next = NULL; //Cut the lists
12+
}
13+
}

0 commit comments

Comments
 (0)