/****************************************************************************/
/* File: queue.h
*/
/****************************************************************************/
/****************************************************************************/
/* */
/* Simple Queue ADT */
/* */
/* Declaration */
/* */
/****************************************************************************/
typedef struct {
int key;
int value;
} data_t;
typedef struct queueNode {
struct queueNode *next, *prev;
data_t *data;
} QueueNode;
typedef struct queueType {
QueueNode *head;
QueueNode *tail;
} Queue;
/********************************************************************
Alright, so above I have a file provided for me called Queue.h, this makes sense to me in some respect as it gives data_t a pair of values (value and key) and stores that in a QueueNode, which is placed in either the Tail or Head of a queue.
The problem I'm having is that I DO NOT KNOW how to use this to create a Class called Queue.c that does the following functions:
voidinitQueue(Queue *self){
}
void enQueue(Queue *self, data_t *data){
}
QueueNode *frontNode(Queue *self){
}
data_t *frontValue(Queue *self){
}
data_t *dequeue(Queue *self){
}
void removeNode(queue *self, queueNode *p){
}
QueueNode *findNode(queue *self, data_t *data){
}
void printQ(queue *self, char *label){
}
With this (not meant to be altered) main():
/*
int main ()
{
Queue myQueue;
QueueNode *p;
data_t data[10], d2;
int i;
initQueue (&myQueue);
for (i = 0; i < 10; i++) {
data[i].key = i;
data[i].value = 10*i;
enQueue (&myQueue, &data[i]);
}
printQ (&myQueue, "MyQueue:" );
}
*/
Now I'm not going to ask anyone to do this for me, that doesn't help me at all in the long run, but I have absolutely no idea what to do.
I have a basic understanding of how a queue works, my problem is how to execute it. I'm also learning java in another class right now and it's begun to confuse me what rule apply to what class (as in school class, not java class)
If someone could walk me through how one's meant to work with a few of these (or at least initQueue) I'd be really grateful, Ive been working on this for 12 hours now and I'm getting desperate....
Edit:
Update: This is what i have now:
void initQueue(Queue *self)
{
self->head = NULL;
self->tail = NULL;
}
void enQueue(Queue *self, data_t *data)
{
self->head = data;
}
Related
I am currently learning some Linux Driver writing, and i try to implement list with the Linux Kernel list API.
My code just allocate a list and add 2 elements to it.
All go Ok until i try to free the memory i allocate, here i got a segmentation fault, and from what i test, it come from list_del call.
But i cannot see where i did wrong.
Here is my code :
struct drv_cdev{
int devno;
};
struct drv_dev_itf {
struct drv_cdev cdev; //DATA
struct list_head cdev_list;
};
struct drv_dev {
int number;
struct list_head list_master;
struct drv_dev_itf drv_dev_itf;
};
void add_node(struct drv_dev *dev, int num)
{
struct drv_dev_itf *new_cdev;
new_cdev = kmalloc(sizeof(struct drv_dev_itf*), GFP_KERNEL);
new_cdev->cdev.devno = num;
INIT_LIST_HEAD(&new_cdev->cdev_list);
list_add(&new_cdev->cdev_list, &dev->list_master);
}
void destroy_list(struct drv_dev *dev)
{
struct list_head *position;
struct drv_dev_itf *data_structure;
list_for_each (position, &dev->list_master) {
data_structure = list_entry(position, struct drv_dev_itf, cdev_list);
printk("Devno testing : %d\n", data_structure->cdev.devno);
//dev_node_release(driver_class, &data_structure->cdev);
list_del(position);
printk("List head deleted\n");
/*kfree(data_structure);
printk("List data_structure deleted\n");*/
}
}
static int __init test_init(void)
{
struct drv_dev *dev;
dev = kmalloc(sizeof(*dev), GFP_KERNEL);
INIT_LIST_HEAD(&dev->list_master);
add_node(dev, 1);
add_node(dev, 2);
destroy_list(dev);
kfree(dev);
return 0;
}
Thanks in advance for your answers, have a nice day.
I have the following code in C, trying to develop an Operating System simulation:
Queue.c:
typedef enum {running,readyproc,waiting,suspended}status;
typedef struct pcb {
long pid;
char* pname;
long priority;
long sleepperiod;
long* context;
status stat;
}PCB;
typedef enum {ready, timer, suspend} queuetype;
typedef struct {
int size;
int capacity;
PCB ** data;
queuetype qt;
}Queue;
void queue_init(Queue *q, queuetype qt){
q->size =0;
q->capacity = QUEUE_INITIAL_CAPACITY ;//100
q->data = (PCB **)calloc(q->capacity,sizeof(PCB*));
q->qt = qt;
}
PCB* queue_pop (Queue* q){
PCB* toReturn;
int i;
toReturn = q->data[0];
for (i=0;i<q->size;i++){
q->data[i]=q->data [i+1];
}
free(q->data[q->size]);
q->size--;
printf ("toReturn id:%ld pname: %s\n", toReturn->pid, toReturn->pname);
return toReturn;
}
Knowing that the queue gets initialized and filled with PCBs. I do always get a segafault on calling:
PCB* pcb = queue_pop(&queue);
EDIT:
Here is the function that would fill the queue:
void queue_append(Queue *q, PCB* value)
{
q->data[q->size++] = value;
}
EDIT2:
the printf before the return in queue_pop returns this:
toReturn id: 2 pname: test1c_a
which corresponds to what I want to pop from that queue.
for (i=0;i<q->size;i++){
q->data[i]=q->data [i+1];
}
If q->size == q->capacity, then you'll run off the end of q->data (it will access q->data[q->capacity], which is one past its allocation length).
P.S. There are much more efficient ways to do this.
Below seems to be a problem.
free(q->data[q->size]);
lets say two elements are in Queue q->size = 2 hold by
q->data[0] and q->data[1]
so when queue_pop called. Above code will free q->data[2]. This may leads to seg-fault.
I have written a program on queues and dynamic memory allocation. This is what my program needs to do - insert values in to the queue and remove it from the queue; that simple.
But my problem is that it just prints the names of the variables the values are assigned to and the program goes not responding.
Here is my program :
#include <stdio.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct newcake *next;
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);
void cake_order(struct cakes *);
void order_out(struct cakes *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes ));
cake_order(&head); //this is a seperate function and it works perfectly
head->next=(struct cakes *)malloc(sizeof(struct cakes));
order_out(&head->next);
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int caked=NULL;
if(!isEmpty(q))
{
caked=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return caked;
}
}
void order_out(struct cakes *order)
{
struct Queue s;
int i;
order->spongecake=20;
order->meringue=75;
order->chocalate=40;
order->red_velvet=30;
init(&s);
for(i=0;i<10;i++)
{
insert(&s,order->chocalate);
insert(&s,order->spongecake);
insert(&s,order->meringue);
insert(&s,order->red_velvet);
}
while(!isEmpty(&s))
{
printf("%d",removes(&s));
}
}
What seems to be the problem here?
I am new to C, so yea am a bit slow when debugging in this language.
Thank you for your time.
Here is the output:
Lots of problems here, first it would be better if main was declared properly as in int main() and then it returned a value at the end e.g. return 0; like:
int main()
{
.... // code
return 0; // normally 0 is returned if execution has been successful
}
There seem to be other problems with the code as I wasn't able to compile it, for example there's no closing brace at the end of order_out() (right after the while loop).
Also would be good if you provided the cake_order() function.
It's also missing the includes for say stdlib.h, and on line 45 (head=(struct cakes *)malloc(sizeof(struct cakes ));) I've noticed you cast the result of malloc, which is not necessary.
And if I may further add, don't remember to free() the memory you've allocated with malloc(). I didn't see a single free() statement in your code.
I got this code and a strange behaviour while printing the id member variable of node.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int id;
int visited;
// struct node *neighbors_[];
};
struct graph
{
struct node nodes[26];
int adjMat[26][26];
};
struct stack_item
{
struct node node;
struct stack_item *next_;
};
struct myStack
{
struct stack_item *anfang_;
};
void initGraph(struct graph *graph_);
void push(struct myStack *stack_, struct node node);
int main()
{
struct graph graph;
struct myStack stack;
char ausgabe[26]="";
initGraph(&graph);
//READ DATA
char line[200];
int firstTime=1,first;
first=0;
push(&stack,graph.nodes[first]);
printf("ID %i\n",stack.anfang_->node.id);
printf("ID %i\n",stack.anfang_->node.id);
//FINISHED DATA READING
//CALL DFS
//dfs(graph,stack,ausgabe);
}
void push(struct myStack *stack_, struct node node)
{
struct stack_item item;
item.node=node;
item.next_=stack_->anfang_;
stack_->anfang_=&item;
}
void initGraph(struct graph *graph_)
{
int i,j;
for(i=0; i<26; i++)
{
struct node node= {i,0};
graph_->nodes[i]=node;
for(j=0; j<26; j++)
{
graph_->adjMat[i][j]=0;
}
}
}
If i execute this, the first print command leads to 'ID 0',the second to 'ID 1980796117'. How can this value change by printing it? Could please anyone help me, i've got really no idea!
void push(struct myStack *stack_, struct node node)
{
struct stack_item item;
item.node=node;
item.next_=stack_->anfang_;
/* BAD! */
stack_->anfang_=&item;
}
item is a local variable which, when the push function returns, goes out of scope. Any existing pointers which refer to this object are now invalid, and dereferencing it results in undefined behavior.
You will need to dynamically allocate item (i.e., malloc) if you need it to persist once the function has returned.
I'm trying to delete all nodes from my queue of structures.
Structure:
struct element{
int id;
int sign;
int year;
int month;
double amount;
struct element *next;
};
struct queue{
struct element *head;
int size;
};
And the function I wrote:
void delete(struct queue *queue) {
if (queue->size == 0){
printf("Structure is empty\n");
}
else {
struct element* this;
struct element* other;
for(this=queue->head;this!=NULL;this=other)
{
other=this->next;
free(this);
}
free(queue);
}
}
It doesn't work, and I'm out of ideas. Any suggestions?
In your delete routine, you do not free the queue if the size is empty, but you do free it if the size is non-empty. You should probably do the same for both cases. That is, either don't free in both places, or free in both places.
It is bothersome to need to figure out what the right thing to do is, because delete can not know how the queue was allocated. Given your current design, a way out may be to pass a flag to delete to indicate what it should do:
void delete(struct queue *queue, int do_free) {
if (queue->size == 0){
printf("Structure is empty\n");
}
else {
struct element* this;
struct element* other;
for(this=queue->head;this!=NULL;this=other) {
other=this->next;
free(this);
}
queue->head = 0;
queue->size = 0;
}
if (do_free) free(queue);
}
struct queue new;
/* ... */
delete(&new, 0); /* don't free the queue */
struct queue *empty_new = malloc(sizeof(struct queue));
empty_new->size = 0;
delete(empty_new, 1); /* free the empty queue */
Here
struct queue new;
//...
delete(&new);
new is allocated on the stack, so don't call free(queue) in delete.
Instead, set queue->head = NULL; queue->size = 0; to indicate that the queue is now empty as mentioned by #kirill.
How about just passing the first element of the queue.
void delete(element *el ) {
if(el) {
delete(el->next );
free(el);
}
}
with
typedef struct _element{
int id;
int sign;
int year;
int month;
double amount;
struct _element *next;
} element;
you might have forgotten to update at the end of the function the pointer to NULL as well as changing the size of the queue to 0.