-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
#ifndef MSGTYPE_H | ||
#define MSGTYPE_H | ||
|
||
#define MTEXTSIZE 128 | ||
#define VALUESIZE 100 | ||
#define KEYID 2817 | ||
|
||
struct msgbuf { | ||
typedef struct msgbuf { | ||
long mtype; | ||
char mtext[MTEXTSIZE]; | ||
}; | ||
unsigned int key; | ||
char value[VALUESIZE]; | ||
} msgbuf_t; | ||
|
||
#define MSGSIZE (sizeof(msgbuf_t) - sizeof(long)) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef NODE_H | ||
#define NODE_H | ||
|
||
#include "msg.h" | ||
|
||
typedef struct node { | ||
unsigned int key; | ||
char value[VALUESIZE]; | ||
struct node *next; | ||
} node_t; | ||
|
||
node_t init(); | ||
int insert(unsigned int, char[VALUESIZE]); | ||
int delete(unsigned int); | ||
node_t get(unsigned int); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "node.h" | ||
#include "msg.h" | ||
|
||
node_t init() { | ||
node_t *head = NULL; | ||
head = malloc(sizeof(node_t)); | ||
if (head == NULL) { | ||
return NULL; | ||
} | ||
|
||
head->next = NULL; | ||
|
||
return head; | ||
} | ||
|
||
int insert(node_t head, unsigned int key, char value[VALUESIZE]) { | ||
if (head == NULL) { | ||
return -1; | ||
} | ||
|
||
node_t *node = get(key); | ||
|
||
if (node != NULL) { | ||
return -1; | ||
} | ||
|
||
node = malloc(sizeof(node_t)); | ||
|
||
if (node == NULL) { | ||
return -1; | ||
} | ||
|
||
node->key = key; | ||
strcpy(node->value, value); | ||
|
||
node_t *iter = head; | ||
|
||
while (iter->next != NULL) { | ||
iter = iter->next; | ||
} | ||
|
||
iter->next = node; | ||
|
||
return 0; | ||
} | ||
|
||
int delete(node_t head, unsigned int key) { | ||
if (head == NULL) { | ||
return -1; | ||
} | ||
|
||
node_t *iter = head->next; | ||
|
||
while (iter != NULL) { | ||
if (iter->next != NULL && iter->next->key == key) { | ||
break; | ||
} | ||
|
||
iter = iter->next; | ||
} | ||
|
||
if (iter->next == NULL) { | ||
return -1; | ||
} | ||
|
||
node_t *temp = iter->next; | ||
|
||
iter->next = iter->next->next; | ||
|
||
free(temp); | ||
|
||
return 0; | ||
} | ||
|
||
node_t get(node_t head, unsigned int key) { | ||
if (head == NULL) { | ||
return; | ||
} | ||
|
||
node_t *iter = head; | ||
while ((iter = iter->next) != NULL) { | ||
if (iter->key == key) { | ||
break; | ||
} | ||
} | ||
|
||
if (iter == NULL) { | ||
return NULL; | ||
} | ||
|
||
return iter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters