Linked list initial implementation
modified: src/nms.c
This commit is contained in:
parent
d0bec878b3
commit
238c8095e2
78
src/nms.c
78
src/nms.c
@ -3,6 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define SPACE 040
|
#define SPACE 040
|
||||||
#define NEWLINE 012
|
#define NEWLINE 012
|
||||||
@ -12,24 +13,81 @@ int getTermSizeCols(void);
|
|||||||
void clearTermWindow(int, int);
|
void clearTermWindow(int, int);
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
struct winpos {
|
||||||
|
char source;
|
||||||
|
int row;
|
||||||
|
int col;
|
||||||
|
struct winpos *next;
|
||||||
|
};
|
||||||
|
struct winpos start;
|
||||||
|
struct winpos *list_pointer = &start;
|
||||||
|
struct winpos *temp; // Used for free()ing the list
|
||||||
int termSizeRows = getTermSizeRows();
|
int termSizeRows = getTermSizeRows();
|
||||||
int termSizeCols = getTermSizeCols();
|
int termSizeCols = getTermSizeCols();
|
||||||
char *inputBuffer;
|
|
||||||
|
// Geting input
|
||||||
|
int c, x = 1, y = 1;
|
||||||
|
bool first = true;
|
||||||
|
while ((c = getchar()) != EOF) {
|
||||||
|
switch (c) {
|
||||||
|
case '\n':
|
||||||
|
++y;
|
||||||
|
x = 1;
|
||||||
|
break;
|
||||||
|
case ' ':
|
||||||
|
case '\t':
|
||||||
|
case '\f':
|
||||||
|
case '\r':
|
||||||
|
case '\v':
|
||||||
|
++x;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (first) {
|
||||||
|
start.source = c;
|
||||||
|
start.row = y;
|
||||||
|
start.col = x;
|
||||||
|
start.next = (struct winpos *) 0;
|
||||||
|
first = false;
|
||||||
|
++x;
|
||||||
|
} else {
|
||||||
|
// Allocate space for the new struct in our linked list
|
||||||
|
// and point *next to it.
|
||||||
|
list_pointer->next = malloc(sizeof(struct winpos));
|
||||||
|
|
||||||
|
// Now let's point list_pointer to the next structure
|
||||||
|
// and populate it.
|
||||||
|
list_pointer = list_pointer->next;
|
||||||
|
list_pointer->source = c;
|
||||||
|
list_pointer->row = y;
|
||||||
|
list_pointer->col = x;
|
||||||
|
list_pointer->next = (struct winpos *) 0;
|
||||||
|
|
||||||
|
++x;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
clearTermWindow(termSizeRows, termSizeCols);
|
clearTermWindow(termSizeRows, termSizeCols);
|
||||||
|
|
||||||
// Geting input
|
list_pointer = &start;
|
||||||
int c, i = 0;
|
while (list_pointer != (struct winpos *) 0) {
|
||||||
while ((c = getchar()) != EOF) {
|
printf("row: %i, ", list_pointer->row);
|
||||||
inputBuffer = realloc(inputBuffer, sizeof(char) * (i + 2));
|
printf("col: %i, ", list_pointer->col);
|
||||||
inputBuffer[i] = c;
|
printf("char: %c\n", list_pointer->source);
|
||||||
++i;
|
list_pointer = list_pointer->next;
|
||||||
}
|
}
|
||||||
inputBuffer[i] = '\0';
|
|
||||||
|
|
||||||
printf("%s", inputBuffer);
|
// Freeing the list. Starting with the second list item because
|
||||||
|
// the first one was not created with malloc
|
||||||
|
list_pointer = start.next;
|
||||||
|
while (list_pointer != (struct winpos *) 0) {
|
||||||
|
temp = list_pointer;
|
||||||
|
list_pointer = list_pointer->next;
|
||||||
|
free(temp);
|
||||||
|
}
|
||||||
|
|
||||||
free(inputBuffer);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user