durus
January 19th, 2007, 08:30 PM
Hi
I'm taking a course in C and wants tips for programs to do in C. I have programmed C++ some years ago, but i do not remember much, and i have done heaps / queues in python in another course.
So i did heaps / queues in C today.
#ifndef __NODE__H__
#define __NODE__H__
typedef struct Node Node;struct Node{
char *data;
Node *next;
};
#endif //__NODE__H__
#ifndef __QUEUE__H__
#define __QUEUE__H__
#include <stdlib.h>
#include "node.h"
// #define QUEUE_DEBUG(x) printf(" QUEUE_DEBUG\t%s\n",x)
#define QUEUE_DEBUG(x)
typedef struct{
Node *front;
Node *back;
}Queue;
void QueueInit(Queue *queue){
queue -> front = NULL;
queue -> back = NULL;
void (*func)(int, int, int);
}
char QueueIsEmpty(Queue *queue){
if(queue ->front == NULL)
return 1;
else
return 0;
}
void QueuePut(Queue *queue, char *data){
Node *node = malloc(sizeof(Node));
node -> data = data;
node -> next = NULL;
if(QueueIsEmpty(queue)){
queue -> back = node;
queue -> front = node;
QUEUE_DEBUG("Första noden tillagd");
}else{
queue -> back -> next = node;
queue -> back = node;
QUEUE_DEBUG("Till nod tillagd");
}
}
char* QueueGet(Queue *queue){
if(QueueIsEmpty(queue) == 0){
Node *p = queue -> front;
queue -> front = p -> next;
char *temp = p -> data;
free(p);
return temp;
}
}
#endif //__QUEUE__H__
#ifndef __HEAP__H__
#define __HEAP__H__
#include <stdlib.h>
#include "node.h"
// #define HEAP_DEBUG(x) printf(" DEBUG\t%s\n",x)
#define HEAP_DEBUG(x)
typedef struct{
Node *front;
}Heap;
void HeapInit(Heap *heap);
char HeapIsEmpty(Heap *heap);
void HeapPut(Heap *heap, char *data);
char* HeapGet(Heap *heap);
void HeapInit(Heap *heap){
heap -> front = NULL;
}
char HeapIsEmpty(Heap *heap){
if(heap -> front == NULL)
return 1;
else
return 0;
}
void HeapPut(Heap *heap, char *data){
Node *node = malloc(sizeof(Node));
node -> data = data;
if(HeapIsEmpty(heap)){
node -> next = NULL;
heap -> front = node;
HEAP_DEBUG("first Node added");
}else{
node -> next = heap -> front;
heap -> front = node;
HEAP_DEBUG("More node added");
}
}
char *HeapGet(Heap *heap){
HEAP_DEBUG("Running 'HeapGet()'");
if(HeapIsEmpty(heap) == 0){
HEAP_DEBUG("Node poped");
char *data = heap -> front -> data;
Node *node = heap -> front;
heap -> front = node -> next;
free(node);
return data;
}
HEAP_DEBUG("Heap NOT POPED");
}
#endif //__HEAP__H__
Can you give me tips on some fun things to do that should be a little bit harder than this, and do you also have tips on free online books in C ?
And I'm glad if you give me tips on how i can improve the code above, or if i'm doing stuff in some "wrong/bad" way
I'm taking a course in C and wants tips for programs to do in C. I have programmed C++ some years ago, but i do not remember much, and i have done heaps / queues in python in another course.
So i did heaps / queues in C today.
#ifndef __NODE__H__
#define __NODE__H__
typedef struct Node Node;struct Node{
char *data;
Node *next;
};
#endif //__NODE__H__
#ifndef __QUEUE__H__
#define __QUEUE__H__
#include <stdlib.h>
#include "node.h"
// #define QUEUE_DEBUG(x) printf(" QUEUE_DEBUG\t%s\n",x)
#define QUEUE_DEBUG(x)
typedef struct{
Node *front;
Node *back;
}Queue;
void QueueInit(Queue *queue){
queue -> front = NULL;
queue -> back = NULL;
void (*func)(int, int, int);
}
char QueueIsEmpty(Queue *queue){
if(queue ->front == NULL)
return 1;
else
return 0;
}
void QueuePut(Queue *queue, char *data){
Node *node = malloc(sizeof(Node));
node -> data = data;
node -> next = NULL;
if(QueueIsEmpty(queue)){
queue -> back = node;
queue -> front = node;
QUEUE_DEBUG("Första noden tillagd");
}else{
queue -> back -> next = node;
queue -> back = node;
QUEUE_DEBUG("Till nod tillagd");
}
}
char* QueueGet(Queue *queue){
if(QueueIsEmpty(queue) == 0){
Node *p = queue -> front;
queue -> front = p -> next;
char *temp = p -> data;
free(p);
return temp;
}
}
#endif //__QUEUE__H__
#ifndef __HEAP__H__
#define __HEAP__H__
#include <stdlib.h>
#include "node.h"
// #define HEAP_DEBUG(x) printf(" DEBUG\t%s\n",x)
#define HEAP_DEBUG(x)
typedef struct{
Node *front;
}Heap;
void HeapInit(Heap *heap);
char HeapIsEmpty(Heap *heap);
void HeapPut(Heap *heap, char *data);
char* HeapGet(Heap *heap);
void HeapInit(Heap *heap){
heap -> front = NULL;
}
char HeapIsEmpty(Heap *heap){
if(heap -> front == NULL)
return 1;
else
return 0;
}
void HeapPut(Heap *heap, char *data){
Node *node = malloc(sizeof(Node));
node -> data = data;
if(HeapIsEmpty(heap)){
node -> next = NULL;
heap -> front = node;
HEAP_DEBUG("first Node added");
}else{
node -> next = heap -> front;
heap -> front = node;
HEAP_DEBUG("More node added");
}
}
char *HeapGet(Heap *heap){
HEAP_DEBUG("Running 'HeapGet()'");
if(HeapIsEmpty(heap) == 0){
HEAP_DEBUG("Node poped");
char *data = heap -> front -> data;
Node *node = heap -> front;
heap -> front = node -> next;
free(node);
return data;
}
HEAP_DEBUG("Heap NOT POPED");
}
#endif //__HEAP__H__
Can you give me tips on some fun things to do that should be a little bit harder than this, and do you also have tips on free online books in C ?
And I'm glad if you give me tips on how i can improve the code above, or if i'm doing stuff in some "wrong/bad" way