3rd semester files
This commit is contained in:
18
3rd-Semester-Fall-2022/PDS/Homework04/.vscode/c_cpp_properties.json
vendored
Executable file
18
3rd-Semester-Fall-2022/PDS/Homework04/.vscode/c_cpp_properties.json
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"/usr/include/c++/12"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/usr/bin/gcc",
|
||||
"cStandard": "gnu17",
|
||||
"cppStandard": "c++17",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"configurationProvider": "ms-vscode.makefile-tools"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
7
3rd-Semester-Fall-2022/PDS/Homework04/.vscode/launch.json
vendored
Executable file
7
3rd-Semester-Fall-2022/PDS/Homework04/.vscode/launch.json
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": []
|
||||
}
|
7
3rd-Semester-Fall-2022/PDS/Homework04/.vscode/settings.json
vendored
Executable file
7
3rd-Semester-Fall-2022/PDS/Homework04/.vscode/settings.json
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"C_Cpp.errorSquiggles": "Enabled",
|
||||
"files.associations": {
|
||||
"iostream": "cpp",
|
||||
"string": "cpp"
|
||||
}
|
||||
}
|
28
3rd-Semester-Fall-2022/PDS/Homework04/.vscode/tasks.json
vendored
Executable file
28
3rd-Semester-Fall-2022/PDS/Homework04/.vscode/tasks.json
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: g++ build active file",
|
||||
"command": "/usr/bin/g++",
|
||||
"args": [
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${fileDirname}/${fileBasenameNoExtension}"
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"detail": "Task generated by Debugger."
|
||||
}
|
||||
],
|
||||
"version": "2.0.0"
|
||||
}
|
12
3rd-Semester-Fall-2022/PDS/Homework04/HW04_input.txt
Executable file
12
3rd-Semester-Fall-2022/PDS/Homework04/HW04_input.txt
Executable file
@ -0,0 +1,12 @@
|
||||
126 5 350 95 164 1 136 31 468 129
|
||||
473 303 248 88 166 26 330 476 159 225
|
||||
343 463 311 356 104 162 27 497 320 294
|
||||
36 73 301 255 207 265 41 151 410 480
|
||||
351 339 499 181 215 487 324 449 443 409
|
||||
316 152 179 134 209 402 20 42 317 411
|
||||
13 200 456 346 106 101 444 144 247 331
|
||||
491 67 249 270 387 360 185 172 383 61
|
||||
451 119 28 107 244 455 267 110 277 199
|
||||
454 290 397 123 30 481 435 140 333 448
|
||||
344 488 258 103 148 305 483 427 457 143
|
||||
485 33 133 40 167 349 413 268 198 450
|
41
3rd-Semester-Fall-2022/PDS/Homework04/Makefile
Executable file
41
3rd-Semester-Fall-2022/PDS/Homework04/Makefile
Executable file
@ -0,0 +1,41 @@
|
||||
TARGET_EXEC ?= a.out
|
||||
|
||||
BUILD_DIR ?= ./build
|
||||
SRC_DIRS ?= ./src
|
||||
|
||||
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
|
||||
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
|
||||
DEPS := $(OBJS:.o=.d)
|
||||
|
||||
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
|
||||
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
|
||||
|
||||
CPPFLAGS ?= $(INC_FLAGS) -MMD -MP
|
||||
|
||||
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
|
||||
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
|
||||
|
||||
# assembly
|
||||
$(BUILD_DIR)/%.s.o: %.s
|
||||
$(MKDIR_P) $(dir $@)
|
||||
$(AS) $(ASFLAGS) -c $< -o $@
|
||||
|
||||
# c source
|
||||
$(BUILD_DIR)/%.c.o: %.c
|
||||
$(MKDIR_P) $(dir $@)
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# c++ source
|
||||
$(BUILD_DIR)/%.cpp.o: %.cpp
|
||||
$(MKDIR_P) $(dir $@)
|
||||
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
|
||||
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD_DIR)
|
||||
|
||||
-include $(DEPS)
|
||||
|
||||
MKDIR_P ?= mkdir -p
|
BIN
3rd-Semester-Fall-2022/PDS/Homework04/build/a.out
Executable file
BIN
3rd-Semester-Fall-2022/PDS/Homework04/build/a.out
Executable file
Binary file not shown.
1
3rd-Semester-Fall-2022/PDS/Homework04/build/src/HW04_Aidan_Sharpe.cpp.d
Executable file
1
3rd-Semester-Fall-2022/PDS/Homework04/build/src/HW04_Aidan_Sharpe.cpp.d
Executable file
@ -0,0 +1 @@
|
||||
build/./src/HW04_Aidan_Sharpe.cpp.o: src/HW04_Aidan_Sharpe.cpp
|
BIN
3rd-Semester-Fall-2022/PDS/Homework04/build/src/HW04_Aidan_Sharpe.cpp.o
Executable file
BIN
3rd-Semester-Fall-2022/PDS/Homework04/build/src/HW04_Aidan_Sharpe.cpp.o
Executable file
Binary file not shown.
134
3rd-Semester-Fall-2022/PDS/Homework04/src/HW04_Aidan_Sharpe.cpp
Executable file
134
3rd-Semester-Fall-2022/PDS/Homework04/src/HW04_Aidan_Sharpe.cpp
Executable file
@ -0,0 +1,134 @@
|
||||
/****************************************************************
|
||||
* Name: Aidan Sharpe
|
||||
* Course: Principles of Data Structures
|
||||
* Class: CS04225
|
||||
* Assignment Date: October 17, 2022
|
||||
* File Name: HW04_Aidan_Sharpe.cpp
|
||||
*****************************************************************
|
||||
* ID: Homework 4 Problem 1
|
||||
* Purpose: Read values to a linked list and rewrite them in reversed order
|
||||
*****************************************************************/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
struct LL_Node
|
||||
{
|
||||
int val;
|
||||
LL_Node *next;
|
||||
|
||||
LL_Node(int d, LL_Node *p=nullptr)
|
||||
{
|
||||
val = d;
|
||||
next = p;
|
||||
}
|
||||
};
|
||||
|
||||
/// @brief Adds a new node to the end of a linked list starting at memory address head
|
||||
/// @param head address of first node
|
||||
/// @param val value to add
|
||||
void AddNodeEnd(LL_Node *&head, int val);
|
||||
|
||||
/// @brief Adds a new node to the begining of a linked list starting at memory address head
|
||||
/// @param head address of first node
|
||||
/// @param val value to add
|
||||
void AddNodeBegin(LL_Node *&head, int val);
|
||||
|
||||
/// @brief prints a linked list in format [m0, m1, ... , mn]
|
||||
/// @param head address of first node
|
||||
void PrintLL(LL_Node *head);
|
||||
|
||||
/// @brief write a linked list to a space-separated text file
|
||||
/// @param head address of first list node
|
||||
/// @param fname file path relative to source directory
|
||||
void WriteLLToFile(LL_Node *head, std::string fname);
|
||||
|
||||
/// @brief read LL from space/newline separated file
|
||||
/// @param head address of first list node
|
||||
/// @param fname file path relative to source directory
|
||||
/// @param reversed whether or not to reverse the order of numbers
|
||||
void ReadLLFromFile(LL_Node *&head, std::string fname, bool reversed=false);
|
||||
|
||||
int main()
|
||||
{
|
||||
LL_Node *head = nullptr;
|
||||
std::string in, out;
|
||||
std::cout << "Input data file name (path relative to makefile directory):\n";
|
||||
std::cin >> in;
|
||||
ReadLLFromFile(head, in, true);
|
||||
std::cout << "Output data file name (path relative to makefile directory):\n";
|
||||
std::cin >> out;
|
||||
WriteLLToFile(head, out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void AddNodeEnd(LL_Node *&head, int val)
|
||||
{
|
||||
if (head == nullptr)
|
||||
head = new LL_Node(val);
|
||||
else
|
||||
{
|
||||
LL_Node* p = head;
|
||||
while (p->next != nullptr)
|
||||
p = p->next;
|
||||
p->next = new LL_Node(val);
|
||||
}
|
||||
}
|
||||
|
||||
void AddNodeBegin(LL_Node *&head, int val)
|
||||
{
|
||||
LL_Node *p = new LL_Node(val);
|
||||
p->next = head;
|
||||
head = p;
|
||||
}
|
||||
|
||||
void PrintLL(LL_Node *head)
|
||||
{
|
||||
LL_Node *p = head;
|
||||
std::cout << "[";
|
||||
while (p != nullptr)
|
||||
{
|
||||
std::cout << p->val;
|
||||
p = p->next;
|
||||
std::cout << ((p != nullptr) ? ", " : "]\n"); // separate elements by ',' and add close bracket if it's the last node
|
||||
}
|
||||
}
|
||||
|
||||
void WriteLLToFile(LL_Node *head, std::string fname)
|
||||
{
|
||||
std::ofstream outfile;
|
||||
outfile.open(fname);
|
||||
LL_Node* p = head;
|
||||
while(p != nullptr)
|
||||
{
|
||||
outfile << p->val << ' ';
|
||||
p = p->next;
|
||||
}
|
||||
outfile.close();
|
||||
}
|
||||
|
||||
void ReadLLFromFile(LL_Node *&head, std::string fname, bool reversed)
|
||||
{
|
||||
std::ifstream infile;
|
||||
std::string line;
|
||||
infile.open(fname);
|
||||
while (std::getline(infile, line))
|
||||
{
|
||||
std::string snum;
|
||||
for (char c : line)
|
||||
{
|
||||
if (isdigit(c))
|
||||
snum.push_back(c);
|
||||
else if (c == ' ' || c == '\n')
|
||||
{
|
||||
if (reversed)
|
||||
AddNodeBegin(head, std::stoi(snum));
|
||||
else
|
||||
AddNodeEnd(head, std::stoi(snum));
|
||||
snum = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
infile.close();
|
||||
}
|
Reference in New Issue
Block a user