cs-2124: finish part one of assignment 3
This commit is contained in:
parent
79f15d14dc
commit
2c97b86ab2
225
Spring-2023/CS-2124/Assignment-3/.clang-format
Normal file
225
Spring-2023/CS-2124/Assignment-3/.clang-format
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
# BasedOnStyle: LLVM
|
||||||
|
AccessModifierOffset: -2
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignArrayOfStructures: None
|
||||||
|
AlignConsecutiveAssignments:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: true
|
||||||
|
AlignConsecutiveBitFields:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveDeclarations:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveMacros:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignEscapedNewlines: Right
|
||||||
|
AlignOperands: Align
|
||||||
|
AlignTrailingComments:
|
||||||
|
Kind: Always
|
||||||
|
OverEmptyLines: 0
|
||||||
|
AllowAllArgumentsOnNextLine: true
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: true
|
||||||
|
AllowShortBlocksOnASingleLine: Never
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortEnumsOnASingleLine: true
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
AllowShortLambdasOnASingleLine: All
|
||||||
|
AllowShortLoopsOnASingleLine: false
|
||||||
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: false
|
||||||
|
AlwaysBreakTemplateDeclarations: MultiLine
|
||||||
|
AttributeMacros:
|
||||||
|
- __capability
|
||||||
|
BinPackArguments: true
|
||||||
|
BinPackParameters: true
|
||||||
|
BitFieldColonSpacing: Both
|
||||||
|
BraceWrapping:
|
||||||
|
AfterCaseLabel: false
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: Never
|
||||||
|
AfterEnum: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterObjCDeclaration: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
BeforeLambdaBody: false
|
||||||
|
BeforeWhile: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
SplitEmptyNamespace: true
|
||||||
|
BreakAfterAttributes: Never
|
||||||
|
BreakAfterJavaFieldAnnotations: false
|
||||||
|
BreakArrays: true
|
||||||
|
BreakBeforeBinaryOperators: None
|
||||||
|
BreakBeforeConceptDeclarations: Always
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
BreakBeforeInlineASMColon: OnlyMultiline
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializers: BeforeColon
|
||||||
|
BreakInheritanceList: BeforeColon
|
||||||
|
BreakStringLiterals: true
|
||||||
|
ColumnLimit: 80
|
||||||
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
DisableFormat: false
|
||||||
|
EmptyLineAfterAccessModifier: Never
|
||||||
|
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
FixNamespaceComments: true
|
||||||
|
ForEachMacros:
|
||||||
|
- foreach
|
||||||
|
- Q_FOREACH
|
||||||
|
- BOOST_FOREACH
|
||||||
|
IfMacros:
|
||||||
|
- KJ_IF_MAYBE
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
IncludeCategories:
|
||||||
|
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||||
|
Priority: 2
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
|
||||||
|
Priority: 3
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 1
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
IncludeIsMainRegex: '(Test)?$'
|
||||||
|
IncludeIsMainSourceRegex: ''
|
||||||
|
IndentAccessModifiers: false
|
||||||
|
IndentCaseBlocks: false
|
||||||
|
IndentCaseLabels: false
|
||||||
|
IndentExternBlock: AfterExternBlock
|
||||||
|
IndentGotoLabels: true
|
||||||
|
IndentPPDirectives: None
|
||||||
|
IndentRequiresClause: true
|
||||||
|
IndentWidth: 4
|
||||||
|
IndentWrappedFunctionNames: false
|
||||||
|
InsertBraces: false
|
||||||
|
InsertNewlineAtEOF: false
|
||||||
|
InsertTrailingCommas: None
|
||||||
|
IntegerLiteralSeparator:
|
||||||
|
Binary: 0
|
||||||
|
BinaryMinDigits: 0
|
||||||
|
Decimal: 0
|
||||||
|
DecimalMinDigits: 0
|
||||||
|
Hex: 0
|
||||||
|
HexMinDigits: 0
|
||||||
|
JavaScriptQuotes: Leave
|
||||||
|
JavaScriptWrapImports: true
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
|
LambdaBodyIndentation: Signature
|
||||||
|
LineEnding: DeriveLF
|
||||||
|
MacroBlockBegin: ''
|
||||||
|
MacroBlockEnd: ''
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCBinPackProtocolList: Auto
|
||||||
|
ObjCBlockIndentWidth: 2
|
||||||
|
ObjCBreakBeforeNestedBlockParam: true
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: true
|
||||||
|
PackConstructorInitializers: BinPack
|
||||||
|
PenaltyBreakAssignment: 2
|
||||||
|
PenaltyBreakBeforeFirstCallParameter: 19
|
||||||
|
PenaltyBreakComment: 300
|
||||||
|
PenaltyBreakFirstLessLess: 120
|
||||||
|
PenaltyBreakOpenParenthesis: 0
|
||||||
|
PenaltyBreakString: 1000
|
||||||
|
PenaltyBreakTemplateDeclaration: 10
|
||||||
|
PenaltyExcessCharacter: 1000000
|
||||||
|
PenaltyIndentedWhitespace: 0
|
||||||
|
PenaltyReturnTypeOnItsOwnLine: 60
|
||||||
|
PointerAlignment: Right
|
||||||
|
PPIndentWidth: -1
|
||||||
|
QualifierAlignment: Leave
|
||||||
|
ReferenceAlignment: Pointer
|
||||||
|
ReflowComments: true
|
||||||
|
RemoveBracesLLVM: false
|
||||||
|
RemoveSemicolon: false
|
||||||
|
RequiresClausePosition: OwnLine
|
||||||
|
RequiresExpressionIndentation: OuterScope
|
||||||
|
SeparateDefinitionBlocks: Leave
|
||||||
|
ShortNamespaceLines: 1
|
||||||
|
SortIncludes: CaseSensitive
|
||||||
|
SortJavaStaticImport: Before
|
||||||
|
SortUsingDeclarations: LexicographicNumeric
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: true
|
||||||
|
SpaceAroundPointerQualifiers: Default
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCaseColon: false
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeParensOptions:
|
||||||
|
AfterControlStatements: true
|
||||||
|
AfterForeachMacros: true
|
||||||
|
AfterFunctionDefinitionName: false
|
||||||
|
AfterFunctionDeclarationName: false
|
||||||
|
AfterIfMacros: true
|
||||||
|
AfterOverloadedOperator: false
|
||||||
|
AfterRequiresInClause: false
|
||||||
|
AfterRequiresInExpression: false
|
||||||
|
BeforeNonEmptyParentheses: false
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceBeforeSquareBrackets: false
|
||||||
|
SpaceInEmptyBlock: false
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 1
|
||||||
|
SpacesInAngles: Never
|
||||||
|
SpacesInConditionalStatement: false
|
||||||
|
SpacesInContainerLiterals: true
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInLineCommentPrefix:
|
||||||
|
Minimum: 1
|
||||||
|
Maximum: -1
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
Standard: Latest
|
||||||
|
StatementAttributeLikeMacros:
|
||||||
|
- Q_EMIT
|
||||||
|
StatementMacros:
|
||||||
|
- Q_UNUSED
|
||||||
|
- QT_REQUIRE_VERSION
|
||||||
|
TabWidth: 8
|
||||||
|
UseTab: Never
|
||||||
|
WhitespaceSensitiveMacros:
|
||||||
|
- BOOST_PP_STRINGIZE
|
||||||
|
- CF_SWIFT_NAME
|
||||||
|
- NS_SWIFT_NAME
|
||||||
|
- PP_STRINGIZE
|
||||||
|
- STRINGIZE
|
||||||
|
...
|
||||||
|
|
8
Spring-2023/CS-2124/Assignment-3/.gitignore
vendored
Normal file
8
Spring-2023/CS-2124/Assignment-3/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
bin
|
||||||
|
CMakeFiles
|
||||||
|
Makefile
|
||||||
|
CMakeCache.txt
|
||||||
|
cmake_install.cmake
|
||||||
|
README.pdf
|
||||||
|
*.tex
|
||||||
|
.tex-out
|
BIN
Spring-2023/CS-2124/Assignment-3/Assignment.pdf
Normal file
BIN
Spring-2023/CS-2124/Assignment-3/Assignment.pdf
Normal file
Binary file not shown.
9
Spring-2023/CS-2124/Assignment-3/CMakeLists.txt
Normal file
9
Spring-2023/CS-2124/Assignment-3/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.25)
|
||||||
|
|
||||||
|
set(SOURCES src/lib/vec.h src/lib/lib.h src/lib/lib.c)
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin)
|
||||||
|
|
||||||
|
|
||||||
|
project(AssignmentOne LANGUAGES C)
|
||||||
|
|
||||||
|
add_executable(PartOne src/part_one.c ${SOURCES})
|
55
Spring-2023/CS-2124/Assignment-3/README.org
Normal file
55
Spring-2023/CS-2124/Assignment-3/README.org
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
* Assignment 2
|
||||||
|
|
||||||
|
- ABC123: =zfp106=
|
||||||
|
- Name: =Price Hiller=
|
||||||
|
- Course: =CS2124=
|
||||||
|
- Section: =0C3=
|
||||||
|
- Semester: =Spring 2024=
|
||||||
|
|
||||||
|
** Source Code
|
||||||
|
The full source code for this project can be found at
|
||||||
|
[[https://git.orion-technologies.io/Price/college/src/branch/Development/Spring-2023/CS-2124/Assignment-3]]
|
||||||
|
|
||||||
|
** Running the Programs
|
||||||
|
1. Install [[https://cmake.org/download/][cmake]] version 3.25 or greater.
|
||||||
|
2. Ensure you have a recent version of ~make~ at the time of writing. This project successfully
|
||||||
|
compiles with ~GNU make~ version ~4.4.1~.
|
||||||
|
3. Go the directory with ~CMakeLists.txt~ and run ~cmake .~ to generate a Makefile.
|
||||||
|
4. Run ~make all~ to compile all the programs.
|
||||||
|
5. Go into the newly created ~bin~ directory where all the compiled programs will be output to
|
||||||
|
6. Programs will be named ~PartOne~, ~PartTwo~, and ~PartThree~
|
||||||
|
|
||||||
|
** Questions/Prompts with Answers
|
||||||
|
|
||||||
|
*** Part One: Priority Queue
|
||||||
|
- Prompt
|
||||||
|
1. Students are to implement a priority queue using C language
|
||||||
|
2. Ask user for number of elements for the Priority Queue
|
||||||
|
3. User input elements and their Priority
|
||||||
|
4. Display elements with their Priority
|
||||||
|
5. Ask use for Dequeue.
|
||||||
|
6. If user input dequeue, the element with highest priority should be removed from the
|
||||||
|
existing list and then display the new list.
|
||||||
|
7. 5 is the highest priority, 1 is the lowest priority for a process
|
||||||
|
8. Students can use any data structure to implement the Priority Queue
|
||||||
|
- Image of Program running:
|
||||||
|
[[./assets/PartOne/img1.png]]
|
||||||
|
As a note I implemented this using a stack via a vector/arraylist/dynamically resized
|
||||||
|
array/rain dance for the rain gods down in Africa for resizable drops that was implemented by
|
||||||
|
me for a previous assignment (assignment 2).
|
||||||
|
|
||||||
|
As part of this I chose to sort the list and use a custom ~pop~ method to grab the highest
|
||||||
|
priority item off the top.
|
||||||
|
|
||||||
|
*** Part Two: Huffman Encoding
|
||||||
|
Create a Huffman Encoding Table and Tree for your first or last name
|
||||||
|
|
||||||
|
1. Huffman Code/bit representation Table of your name.
|
||||||
|
a. Fixed bit representation
|
||||||
|
b. Variable bit representation
|
||||||
|
2. Huffman Tree of your name. (You mut use a software to draw the tree i.e. MS word, Visio etc.
|
||||||
|
Do not hand draw the tree)
|
||||||
|
a. Fixed bit representation
|
||||||
|
b. Variable bit representation
|
||||||
|
c. Highlight which Huffman bit representation requires less bits for encoding i.e. Fixed bit
|
||||||
|
representation or variable bit representation, just like in the lecture slides
|
BIN
Spring-2023/CS-2124/Assignment-3/assets/PartOne/img1.png
Normal file
BIN
Spring-2023/CS-2124/Assignment-3/assets/PartOne/img1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 59 KiB |
57
Spring-2023/CS-2124/Assignment-3/src/lib/lib.c
Normal file
57
Spring-2023/CS-2124/Assignment-3/src/lib/lib.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
// Credit to
|
||||||
|
// https://stackoverflow.com/questions/40159892/using-asprintf-on-windows/49873938#49873938
|
||||||
|
// with modifications by me
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdarg.h> /* needed for va_* */
|
||||||
|
#include <stdio.h> /* needed for vsnprintf */
|
||||||
|
#include <stdlib.h> /* needed for malloc, free */
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#include <direct.h>
|
||||||
|
#else
|
||||||
|
#include <linux/limits.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int vscprintf(const char *format, va_list ap) {
|
||||||
|
va_list ap_copy;
|
||||||
|
va_copy(ap_copy, ap);
|
||||||
|
int retval = vsnprintf(NULL, 0, format, ap_copy);
|
||||||
|
va_end(ap_copy);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* asprintf, vasprintf:
|
||||||
|
* MSVC does not implement these, thus we implement them here
|
||||||
|
* GNU-C-compatible compilers implement these with the same names, thus we
|
||||||
|
* don't have to do anything
|
||||||
|
*/
|
||||||
|
int vasprintf(char **strp, const char *format, va_list ap) {
|
||||||
|
int len = vscprintf(format, ap);
|
||||||
|
if (len == -1)
|
||||||
|
return -1;
|
||||||
|
char *str = (char *)malloc((size_t)len + 1);
|
||||||
|
if (!str)
|
||||||
|
return -1;
|
||||||
|
int retval = vsnprintf(str, len + 1, format, ap);
|
||||||
|
if (retval == -1) {
|
||||||
|
free(str);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*strp = str;
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* fmtstr(const char *format, ...) {
|
||||||
|
char *str = NULL;
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, format);
|
||||||
|
vasprintf(&str, format, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_header() {
|
||||||
|
printf("Price Hiller, zfp106 (Spring 2024)\n");
|
||||||
|
}
|
35
Spring-2023/CS-2124/Assignment-3/src/lib/lib.h
Normal file
35
Spring-2023/CS-2124/Assignment-3/src/lib/lib.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Credit to
|
||||||
|
// https://stackoverflow.com/questions/40159892/using-asprintf-on-windows/49873938#49873938
|
||||||
|
// with modifications by me
|
||||||
|
|
||||||
|
#ifndef FMTSTR_H
|
||||||
|
#define FMTSTR_H
|
||||||
|
|
||||||
|
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
|
||||||
|
#define _GNU_SOURCE /* needed for (v)asprintf, affects '#include <stdio.h>' */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdarg.h> /* needed for va_* */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* vscprintf:
|
||||||
|
* MSVC implements this as _vscprintf, thus we just 'symlink' it here
|
||||||
|
* GNU-C-compatible compilers do not implement this, thus we implement it here
|
||||||
|
*/
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define vscprintf _vscprintf
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
int vasprintf(char **strp, const char *format, va_list ap);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
int vscprintf(const char *format, va_list ap);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
char* fmtstr(const char *format, ...);
|
||||||
|
void print_header();
|
||||||
|
|
||||||
|
#endif
|
79
Spring-2023/CS-2124/Assignment-3/src/lib/vec.h
Normal file
79
Spring-2023/CS-2124/Assignment-3/src/lib/vec.h
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#pragma once
|
||||||
|
#define VECTOR_LIB
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// This code is pretty bad in all honesty, but I *really* didn't want to play
|
||||||
|
// the void pointer game.
|
||||||
|
#define DECL_VEC(type_name, type) \
|
||||||
|
typedef struct { \
|
||||||
|
size_t elem_count; \
|
||||||
|
size_t capacity; \
|
||||||
|
type *data; \
|
||||||
|
} type_name; \
|
||||||
|
\
|
||||||
|
type_name type_name##_new(size_t initial_capacity) { \
|
||||||
|
initial_capacity = (initial_capacity < 1) ? 1 : initial_capacity; \
|
||||||
|
type_name v = {.capacity = initial_capacity, \
|
||||||
|
.elem_count = 0, \
|
||||||
|
.data = malloc(sizeof(type *) * initial_capacity)}; \
|
||||||
|
return v; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
bool type_name##_alloc(type_name *v, size_t extra_capacity) { \
|
||||||
|
if (extra_capacity == 0) { \
|
||||||
|
return false; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
int desired_size = v->capacity + extra_capacity; \
|
||||||
|
type *newData = realloc(v->data, sizeof(type *) * desired_size); \
|
||||||
|
if (newData == NULL) \
|
||||||
|
return false; \
|
||||||
|
v->data = newData; \
|
||||||
|
v->capacity = desired_size; \
|
||||||
|
return true; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
bool type_name##_ensure_capacity(type_name *v, size_t capacity) { \
|
||||||
|
if (v->capacity < capacity) \
|
||||||
|
return type_name##_alloc(v, capacity - v->capacity); \
|
||||||
|
return true; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void type_name##_push(type_name *v, type item) { \
|
||||||
|
type_name##_ensure_capacity(v, v->elem_count + 1); \
|
||||||
|
v->data[v->elem_count++] = item; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
type *type_name##_get(type_name *v, size_t index) { \
|
||||||
|
if (index >= 0 && index < v->elem_count) \
|
||||||
|
return &v->data[index]; \
|
||||||
|
return NULL; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void type_name##_del(type_name *v, size_t index) { \
|
||||||
|
if (index >= 0 && index < v->elem_count) { \
|
||||||
|
for (int i = index; (i < v->elem_count - 1); ++i) { \
|
||||||
|
v->data[i] = v->data[i + 1]; \
|
||||||
|
} \
|
||||||
|
v->elem_count--; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
type type_name##_pop(type_name *v) { \
|
||||||
|
type popped_value = *type_name##_get(v, v->elem_count - 1); \
|
||||||
|
type_name##_del(v, v->elem_count - 1); \
|
||||||
|
return popped_value; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
type type_name##_first(type_name *v) { \
|
||||||
|
type found_value = *type_name##_get(v, 0); \
|
||||||
|
type_name##_del(v, 0); \
|
||||||
|
return found_value; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
type *type_name##_peek(type_name *v) { \
|
||||||
|
return type_name##_get(v, v->elem_count - 1); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void type_name##_free(type_name *v) { free(v->data); }
|
76
Spring-2023/CS-2124/Assignment-3/src/part_one.c
Normal file
76
Spring-2023/CS-2124/Assignment-3/src/part_one.c
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#include "lib/lib.h"
|
||||||
|
#include "lib/vec.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct Item {
|
||||||
|
int priority;
|
||||||
|
int data;
|
||||||
|
} Item;
|
||||||
|
|
||||||
|
DECL_VEC(ItemVec, Item);
|
||||||
|
|
||||||
|
// Really lazy linear sort, 🤷
|
||||||
|
void sort_queue(ItemVec *queue) {
|
||||||
|
for (int i = 0; i < queue->elem_count; i++) {
|
||||||
|
if (i + 1 >= queue->elem_count)
|
||||||
|
break;
|
||||||
|
Item curr_element = *ItemVec_get(queue, i);
|
||||||
|
Item next_element = *ItemVec_get(queue, i + 1);
|
||||||
|
if (curr_element.priority > next_element.priority) {
|
||||||
|
queue->data[i] = next_element;
|
||||||
|
queue->data[i + 1] = curr_element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_item(Item *item) {
|
||||||
|
printf("(Element: %d, Priority: %d)\n", item->data, item->priority);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_queue(ItemVec *queue) {
|
||||||
|
for (int i = 0; i < queue->elem_count; i++) {
|
||||||
|
Item *item = ItemVec_get(queue, i);
|
||||||
|
print_item(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
print_header();
|
||||||
|
printf("--- P-Queue: 5 = Highest, 1 = Lowest ---\n");
|
||||||
|
int queue_len = 0;
|
||||||
|
printf("Enter P-Queue Length = ");
|
||||||
|
scanf("%d", &queue_len);
|
||||||
|
ItemVec queue = ItemVec_new(queue_len);
|
||||||
|
|
||||||
|
for (int idx = 0; idx < queue_len; idx++) {
|
||||||
|
int element_num = idx + 1;
|
||||||
|
int element = 0;
|
||||||
|
int element_priority = 1;
|
||||||
|
|
||||||
|
printf("Enter element %d: ", element_num);
|
||||||
|
scanf("%d", &element);
|
||||||
|
printf("Enter priority of element %d: ", element_num);
|
||||||
|
scanf("%d", &element_priority);
|
||||||
|
Item item = {.priority = element_priority, .data = element};
|
||||||
|
ItemVec_push(&queue, item);
|
||||||
|
}
|
||||||
|
|
||||||
|
sort_queue(&queue);
|
||||||
|
printf("--- Priority Queue Full ---\n");
|
||||||
|
printf("Priority Queue Before Dequeue (Sorted by Ascending Priority) :\n");
|
||||||
|
print_queue(&queue);
|
||||||
|
while (queue.elem_count != 0) {
|
||||||
|
printf("---------------------------\n");
|
||||||
|
printf("Dequeue (1 for yes, 0 for exit): ");
|
||||||
|
int option = 0;
|
||||||
|
scanf("%d", &option);
|
||||||
|
if (option == 0)
|
||||||
|
break;
|
||||||
|
ItemVec_pop(&queue);
|
||||||
|
printf("Priority Queue After Dequeue :\n");
|
||||||
|
print_queue(&queue);
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
@ -11,7 +11,7 @@ DEADLINE: <2024-02-22 Thu>
|
|||||||
DEADLINE: <2024-02-29 Thu>
|
DEADLINE: <2024-02-29 Thu>
|
||||||
|
|
||||||
* TODO Assignment 3 :college:cs2124:
|
* TODO Assignment 3 :college:cs2124:
|
||||||
DEADLINE: <2024-03-18 Mon>
|
DEADLINE: <2024-03-25 Mon>
|
||||||
* TODO Assignment 4 :college:cs2124:
|
* TODO Assignment 4 :college:cs2124:
|
||||||
DEADLINE: <2024-04-01 Mon>
|
DEADLINE: <2024-04-01 Mon>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user