Stage Operation of Programming : 657839

Question:

Discuss about the Stage Operation of Programming.

Answer:

IMPLEMENTATION

The below screenshot shows the answer for  assignment  2 –question 2(linked list)

 

6

 

The output of the code is given below

When addition operation is chosen, it adds and asks for size ,data.

Once it is added, it says success.

Printing operation is performed and it prints all the nodes. It is shown below in the given screenshot.

Similarly, the delete and change operation performs as well.

7

The below screenshot shows the answer for  assignment  2 –question 3(storage)

8

 

Storage operation is also performed and screenshot is provided here.

9

#include <stdbool.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

 

#define SIZE 256

 

void* container[SIZE];

 

int read_int() {

char buf[10];

fgets(buf, sizeof(buf), stdin);

return atoi(buf);

}

 

void allocate() {

int index;

for (index = 0; index < SIZE; index++) {

if (container[index] == NULL)

break;

}

 

if (index == SIZE) {

printf(“No available storage\n”);

return;

}

 

printf(“Memory size?\n”);

 

int size = read_int();

container[index] = malloc(size);

 

if (container[index] == NULL)

printf(“Error : Out of memory\n”);

else

printf(“Success : Index %d\n”, index);

}

 

void deallocate() {

printf(“Index to free?\n”);

int index = read_int();

 

if (container[index] == NULL)

printf(“Error : Not allocated index\n”);

else

{

free(container[index]);

container[index] = NULL;

printf(“Success\n”);

}

}

 

void write_to_storage() {

printf(“Index to write?\n”);

int index = read_int();

 

if (container[index] == NULL) {

printf(“Error : Not allocated\n”);

return;

}

 

printf(“Data size?\n”);

int size = read_int();

char* memory = container[index];

 

printf(“Data?\n”);

int ssize = read(0, memory, size);

if (ssize < 0) {

printf(“Error : read\n”);

}

else {

printf(“Success\n”);

}

}

 

void read_from_storage() {

printf(“Index to read?\n”);

int index = read_int();

 

if (container[index] == NULL) {

printf(“Error : Not allocated\n”);

return;

}

 

printf(“Data : “);

printf(“%s\n”, (char*)container[index]);

printf(“\n”);

}

 

int main() {

setvbuf(stdin ,NULL, _IONBF, 0);

setvbuf(stdout, NULL, _IONBF, 0);

 

printf(“Welcome to Storage\n”);

 

while(true) {

printf(“\n1. Allocate\n2. Deallocate\n3. Write to storage\n”

“4. Read from storage\n5. Exit\n”);

switch(read_int()) {

 

case 1:

allocate();

break;

case 2:

deallocate();

break;

case 3:

write_to_storage();

break;

case 4:

read_from_storage();

break;

case 5:

printf(“Bye\n”);

return 0;

}

}

}

 

 

 

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <stdint.h>

 

#define MAX_SIZE 0x10000

 

typedef struct _node {

struct _node* next;

char data[0];

} node;

 

node* head = NULL;

uint32_t read_int() {

char buf[10];

ssize_t size = read(0, buf, sizeof(buf) – 1);

buf[size] = ‘\0’;

uint32_t res = atoi(buf);

return res;

}

 

void read_with_null(int fd, char* buf, size_t size) {

ssize_t read_bytes = read(0, buf, size);

if (buf[read_bytes – 1] == ‘\n’)

buf[read_bytes – 1] = ‘\0’;

else

buf[read_bytes] = ‘\0’;

}

 

void add() {

printf(“Size?\n”);

uint32_t size = read_int();

 

if (size > MAX_SIZE) {

printf(“Too large size\n”);

exit(-1);

}

 

node* new = (node*)malloc(sizeof(node) + size);

if (new == NULL) {

printf(“Error : Out of memory\n”);

return;

}

new->next = NULL;

 

printf(“Data?\n”);

read_with_null(0, new->data, size);

 

if (head == NULL)

head = new;

else {

node *ptr = head;

while (ptr->next != NULL)

ptr = ptr->next;

ptr->next = new;

}

printf(“Success\n”);

}

 

void print() {

node* ptr = head;

uint32_t i = 0;

printf(“Print all nodes\n\n”);

while(ptr != NULL) {

printf(“Node(%d): %s\n”, i, ptr->data);

ptr = ptr->next;

i++;

}

}

 

void delete() {

int i;

if (head == NULL) {

printf(“Error : Nothing to delete\n”);

return;

}

 

printf(“Index to delete?\n”);

uint32_t index = read_int();

node *ptr = head, *prev;

 

if (index == 0) {

// delete head

head = ptr->next;

free(ptr);

printf(“Success : Remove head\n”);

return;

}

 

for (i= 0; i < index && ptr; i++) {

prev = ptr;

ptr = ptr->next;

}

 

if (ptr == NULL) {

printf(“Error : Invalid index\n”);

return;

}

 

prev->next = ptr->next;

free(ptr);

printf(“Success : Remove %dth entry\n”, index);

}

 

void change() {

uint32_t i;

if (head == NULL) {

printf(“Error : Nothing to change\n”);

return;

}

 

printf(“Index to change?\n”);

uint32_t index = read_int();

node *ptr = head;

 

for (i = 0; i < index && ptr;i++) {

ptr = ptr->next;

}

 

if (ptr == NULL) {

printf(“Error : Invalid index\n”);

return;

}

 

printf(“Size?\n”);

int size = read_int();

 

printf(“Data?\n”);

read_with_null(0, ptr->data, size);

 

printf(“Success : Change %dth entry\n”, index);

}

 

int main() {

setvbuf(stdin ,NULL, _IONBF, 0);

setvbuf(stdout, NULL, _IONBF, 0);

 

printf(“Welcome to Linked list\n”);

 

while(true) {

printf(“\n1. Add\n2. Delete\n3. Change\n4. Print\n5. Exit\n”);

switch(read_int()) {

case 1:

add();

break;

case 2:

delete();

break;

case 3:

change();

break;

case 4:

print();

break;

case 5:

exit(0);

}

}

}