feat: update structure

This commit is contained in:
2024-01-22 14:27:40 +08:00
parent 7836c9185c
commit 3544a28a2e
559 changed files with 120846 additions and 4102 deletions

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include "queue.h"
int main() {
double v;
for(int i = 0; i<= MAX_Q_SIZE; i++) {
v = ((double) i / 10.0);
printf("Adding %3.2f\n", v);
enq(v);
}
for(int i = 0; i<= MAX_Q_SIZE; i++) {
v = deq();
printf("Element %d is %3.2f\n", i, v);
}
}

View File

@@ -0,0 +1,31 @@
#include <stdio.h>
int (*fptr)(int);
int func(int x) {
return 2 * x;
}
int y = 10;
int *(*pfptr)();
int *func2() {
return &y;
}
int main() {
printf("Calling func with value 6: %d\n", func(6));
printf("Now setting fptr to point to func.\n");
fptr = func;
printf("Caling fptr with value 6: %d\n", fptr(6));
printf("\nNow caling func2 which returns the address of global variable y: %p\n", func2());
printf("Pointing pfptr to func2.\n");
pfptr = func2;
printf("Now calling fpfptr: %p\n", pfptr());
}

View File

@@ -0,0 +1,83 @@
#include "queue.h"
#include <stdio.h>
static double _queue[MAX_Q_SIZE];
static int _front = 0, _rear = 0;
void enq(double data) {
if ((_front + 1) % MAX_Q_SIZE == _rear){
printf("Error: Queue is full. Item value %3.2f is not added.\n", data);
}
else {
_queue[_front] = data;
_front = (_front + 1) % MAX_Q_SIZE;
}
}
double deq() {
double val = -1;
if (_rear == _front){
printf("Error: Queue is empty. Nothing to return\n");
}
else {
val = _queue[_rear];
_rear = (_rear + 1) % MAX_Q_SIZE;
}
return val;
}
/* This section is for the function pointers exercise */
static double _res;
void sum(double x) {
_res += x;
}
void prod(double x) {
_res *= x;
}
void clear_sum() {
_res = 0;
}
void clear_prod() {
_res = 1.0;
}
double reduce() {
int ndx = _rear;
clear_sum();
while(ndx != _front) {
sum(_queue[ndx]);
ndx = (ndx + 1) % MAX_Q_SIZE;
}
return _res;
}
/* Implement flex_reduce here:
double flex_reduce(clear, op){
// clear(); // Clear _res to either 0 or 1
// for every element in queue:
//Call op with element.
return _res;
}
*/
double flex_reduce(void (*clear)(), void (*op)(double)) {
int ndx = _rear;
clear();
while(ndx != _front) {
op(_queue[ndx]);
ndx = (ndx + 1) % MAX_Q_SIZE;
}
return _res;
}

View File

@@ -0,0 +1,17 @@
// Maximum number of elements in a queue
//
#define MAX_Q_SIZE 10
// Place function prototypes here.
void enq(double);
double deq();
void sum(double);
void prod(double);
void clear_sum();
void clear_prod();
double reduce();
double flex_reduce(void (*)(), void (*)(double));

View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#include "queue.h"
int main() {
int i;
for(i=1; i<=9; i++)
{
printf("Enqueing %d\n", i);
enq((double) i);
}
printf("\nCalling reduce result is %3.2f\n", reduce());
/* Uncomment the following two statements to test flex_reduce */
printf("Calling flex reduce with sum. Result is %3.2f\n", flex_reduce(clear_sum, sum));
printf("Calling flex reduce with prod. Result is %3.2f\n", flex_reduce(clear_prod, prod));
}

View File

@@ -0,0 +1,42 @@
#include <stdio.h>
int *p1, *p2, *p3, *p4;
void fun1(int x, int y) {
static int w;
int z;
w = x * y;
z = x + y;
printf("Inside fun1\n");
p1 = &w;
p2 = &x;
p3 = &y;
p4 = &z;
printf("Address of p1 = %p, address of p2 = %p, address of p3 = %p, address of p4 = %p\n", &p1, &p2, &p3, &p4);
printf("Address of w = %p, address of x = %p, address of y = %p, address of z = %p\n", p1, p2, p3, p4);
printf("w = %d, x = %d, y = %d, z = %d\n", w, x, y, z);
}
int fun2(int f, int g, int h) {
printf("\nInside fun2\n");
printf("w = %d, x = %d, y = %d, z = %d\n", *p1, *p2, *p3, *p4);
return f + g + h;
}
int main() {
int a = 5, b = 6, c;
fun1(a, b);
printf("\nOutside fun1\n");
printf("w = %d, x = %d, y = %d, z = %d\n", *p1, *p2, *p3, *p4);
fun2(1, 2, 3);
printf("\nOutside fun2\n");
printf("w = %d, x = %d, y = %d, z = %d\n", *p1, *p2, *p3, *p4);
}

View File

@@ -0,0 +1,21 @@
#include <stdio.h>
int accumulate(int x) {
// Modify this function so that it accumulates values passed to it to
// a value called "acc".
static int acc = 0;
acc = acc + x;
printf("acc is now %d\n", acc);
}
int main() {
// Call fun2 to accumulate from 1 to 10
for(int i=1; i<=10; i++)
accumulate(i);
}

View File

@@ -0,0 +1,210 @@
#include <stdlib.h>
#include <stdio.h>
#include "bintree.h"
/* ------------------------- DO NOT IMPLEMENT THESE ----------------------
These are provided "free" to you, and you do not need to implement them,
though you MAY need to understand what they do. Some give you hints
on how to work with pointers to pointers. You MAY find some of these
functions useful.
------------------------------------------------------------------------- */
// Searches for the node containing "name" in the tree whose root is at "root",
// returning both the node ifself in "node", and its parent in "prevnode"
// (useful for deleting node). Both "node" and "prevnode" are set to NULL
// if name is not found in the tree.
void findNode(char *name, TTreeNode *root, TTreeNode **node, TTreeNode **prevnode)
{
TTreeNode *trav = root;
TTreeNode *prev = NULL;
while (trav != NULL)
{
int cmp = strcmp(trav->name, name);
if (cmp == 0)
{
*node = trav;
*prevnode = prev;
return;
}
prev = trav;
if (cmp < 0)
trav = trav->right;
else
trav = trav->left;
}
*node = NULL;
*prevnode = NULL;
}
// Searches for the node with the smallest value ("smallest node") in the tree originating at "node".
// Returns the smallest node and its parent in "smallest_node" and "parent"
// respectively.
void findSmallest(TTreeNode *node, TTreeNode **smallest_node, TTreeNode **parent)
{
TTreeNode *trav = node;
TTreeNode *prev = NULL;
if (trav == NULL)
return;
while (trav->left != NULL)
{
prev = trav;
trav = trav->left;
}
*smallest_node = trav;
*parent = prev;
}
// Delete a node at "node" with parent node "prevnode"
void delNode(TTreeNode *node, TTreeNode *prevnode)
{
// This is a leaf node
if (node->left == NULL && node->right == NULL)
{
// See whether node is on parent's left or right, and NULL the
// corresponding pointer
int cmp = strcmp(prevnode->name, node->name);
// Previous node is smaller than this one. This
// node is on the right
if (cmp < 0)
prevnode->right = NULL;
else
prevnode->left = NULL;
freenode(node);
return;
}
// This has a child on the left only
if (node->right == NULL)
{
// Copy the right child over
node = node->left;
freenode(node->left);
return;
}
// This has a child on the right only
if (node->left == NULL)
{
node = node->right;
freenode(node->right);
return;
}
// This has children on both nodes
TTreeNode *smallest, *smallest_parent;
findSmallest(node->right, &smallest, &smallest_parent);
node = smallest;
smallest_parent->left = NULL;
freenode(smallest);
}
/* ---------------------------- IMPLEMENT THESE --------------------------- */
// Create a new node with name set to "name" and
// phoneNum set to "phoneNum".
void delTree(TTreeNode *root)
{
// Implement deleting the entire tree, whose
// root is at "root".
if (root == NULL)
return;
if (root->left != NULL)
delTree(root->left);
if (root->right != NULL)
delTree(root->right);
freenode(root);
}
TTreeNode *makeNewNode(char *name, char *phoneNum)
{
TTreeNode *node = malloc(sizeof(TTreeNode));
node->name = malloc(strlen(name) + 1);
strcpy(node->name, name);
strcpy(node->phoneNum, phoneNum);
node->left = NULL;
node->right = NULL;
return node;
// Implement makeNewNode to create a new
// TTreeNode containing name and phoneNum
}
// Add a new node to the tree.
// Note that "root" is a POINTER to the tree's root,
// not the root itself.
void addNode(TTreeNode **root, TTreeNode *node)
{
if (*root == NULL)
{
*root = node;
}
TTreeNode **trav = root;
while (1)
{
int cmp = strcmp((*trav)->name, node->name);
if (cmp == 0)
return;
if (cmp > 0)
{
if ((*trav)->left == NULL)
(*trav)->left = node;
else
{
trav = &((*trav)->left);
continue;
}
}
else if (cmp < 0)
{
if ((*trav)->right == NULL)
(*trav)->right = node;
else
{
trav = &((*trav)->right);
continue;
}
}
}
// Add a new node to the tree, where root is
// the POINTER to the tree's root.
}
void freenode(TTreeNode *node)
{
free(node->name);
free(node);
// Frees the memory used by node.
}
void print_inorder(TTreeNode *node)
{
// Implement in-order printing of the tree
// Recursion is probably best here.
if (node == NULL)
return;
if (node->left != NULL)
print_inorder(node->left);
printf("Name is %s Number is %s\n", node->name, node->phoneNum);
if (node->right != NULL)
print_inorder(node->right);
}

View File

@@ -0,0 +1,17 @@
#include <string.h>
#include <stdlib.h>
typedef struct tn {
char *name;
char phoneNum[9];
struct tn *left, *right;
} TTreeNode;
void findNode(char *, TTreeNode *, TTreeNode **, TTreeNode **);
void findSmallest(TTreeNode *, TTreeNode **, TTreeNode **);
void delNode(TTreeNode *, TTreeNode *);
void delTree(TTreeNode *);
TTreeNode *makeNewNode(char *, char *);
void addNode(TTreeNode **, TTreeNode *);
void freenode(TTreeNode *);
void print_inorder(TTreeNode *);

Binary file not shown.

View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
int *p;
void dfun1(int x, int y)
{
int *z;
z = (int *) malloc(sizeof(int));
p = z;
*z = x + y;
printf("Inside dfun1: &x = %p, &y = %p, &z = %p, &p = %p\n", &x, &y, &z, &p);
printf("Address stored in z: %p\n", z);
}
void dfun2() {
printf("Inside dfun2: The value p is pointing to is: %d\n", *p);
}
int main() {
printf("Address stored in p before calling dfun1: %p\n", p);
dfun1(10, 20);
printf("Address stored in p after calling dfun1: %p\n", p);
printf("Value pointed to by p: %d\n", *p);
dfun2();
free(p);
}

View File

@@ -0,0 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
int main() {
int *p = (int *) malloc(sizeof(int));
*p = 5;
printf("p is %p and *p is %d\n", p, *p);
free(p);
}

View File

@@ -0,0 +1,44 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct {
char *name;
int age;
} TPerson;
TPerson *makeNewNode(char *name, int age) {
TPerson *p = (TPerson *) malloc(sizeof(TPerson));
strcpy(p->name, name);
p->age = age;
return p;
}
void freeNode(TPerson *node) {
free(node);
}
#define NUM_PERSONS 3
int main() {
TPerson persons[NUM_PERSONS] = {{"Tan Ah Kow", 65}, {"Sio Bak Pau", 23},
{"Aiken Dueet", 21}};
TPerson *list[NUM_PERSONS];
int i;
printf("ADDING PERSONS\n");
for(i=0; i<NUM_PERSONS; i++) {
printf("Adding %s aged %d\n", persons[i].name, persons[i].age);
list[i] = makeNewNode(persons[i].name, persons[i].age);
}
printf("\nDELETING PERSONS\n");
for(i=0; i<NUM_PERSONS; i++) {
printf("Deleting %s aged %d\n", list[i]->name, list[i]->age);
freeNode(list[i]);
}
}

View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include "phonebook.h"
#include "bintree.h"
static TTreeNode *_root = NULL;
char *findPerson(char *name)
{
TTreeNode *node, *prev;
findNode(name, _root, &node, &prev);
if (node != NULL)
return node->phoneNum;
else
return NULL;
}
void print_phonebook()
{
print_inorder(_root);
}
void addPerson(char *name, char *phoneNum)
{
if (findPerson(name) == NULL)
{
TTreeNode *node = makeNewNode(name, phoneNum);
addNode(&_root, node);
}
else
printf("%s is already in phonebook.\n", name);
}
void delPerson(char *name)
{
TTreeNode *node, *prevnode;
findNode(name, _root, &node, &prevnode);
if (node == NULL)
{
printf("Unable to find %s\n", name);
return;
}
delNode(node, prevnode);
}
void delPhonebook()
{
delTree(_root);
_root = NULL;
}

View File

@@ -0,0 +1,5 @@
char *findPerson(char *);
void addPerson(char *, char *);
void delPerson(char *);
void print_phonebook();
void delPhonebook();

View File

@@ -0,0 +1,60 @@
#include <stdio.h>
#include "phonebook.h"
#define ITEMS 5
typedef struct d
{
char *name, *tel;
} TData;
void printResult(char *name)
{
char *result;
printf("Looking for %s ", name);
result = findPerson(name);
if (result == NULL)
printf("NOT FOUND\n");
else
printf(" Number is %s\n", result);
}
int main()
{
TData data[ITEMS] = {{"Fred Astaire", "95551234"}, {"Jean Valjean", "95558764"}, {"Gal Gadot", "95551123"}, {"Aiken Dueet", "95558876"}, {"Victor Hugo", "95524601"}};
int i;
for (i = 0; i < ITEMS; i++)
{
printf("Adding %s, phone number %s\n", data[i].name, data[i].tel);
addPerson(data[i].name, data[i].tel);
}
printf("\nNow retrieiving stored data.\n");
char *result;
for (i = 0; i < ITEMS; i++)
{
printResult(data[i].name);
}
printf("\nRetrieving unknown person.\n");
printResult("Wee Tu Loh");
printf("\nPrinting entire phonebook.\n");
print_phonebook();
printf("\nDeleting Aiken Dueet.\n");
delPerson("Aiken Dueet");
print_phonebook();
printf("\nDeleting Victor Hugo.\n");
delPerson("Victor Hugo");
print_phonebook();
printf("\nDeleting entire phone book.\n");
delPhonebook();
print_phonebook();
}