Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "header.h"
- #include <stdlib.h>
- #include <stdio.h>
- int queue_add(void* new_object, queue_object* queue){
- queue_object *new_el = malloc(sizeof(queue_object));
- if (new_object == NULL){
- return 1;
- }
- else
- {
- new_el->object = new_object;
- if(queue->next == NULL){
- queue->next = new_el;
- new_el->next = NULL;
- }
- else{
- queue_object *current_el = queue->next;
- queue_object *next_el;
- while(current_el->next != NULL){
- next_el = current_el->next;
- current_el = next_el;
- }
- current_el->next = new_el;
- new_el->next = NULL;
- }
- return 0;
- }
- }
- void* queue_poll(queue_object* queue){
- if(queue == NULL || queue->next == NULL){//wenn queue leer
- return NULL;
- }
- else{
- queue_object *prev_el = queue->next; //das zu loeschende element
- queue_object *next_el = prev_el->next; //das naechste element
- queue->next = next_el;
- void* obj = prev_el->object;
- free(prev_el); //letztes element loeschen
- return obj;
- }
- }
- queue_object* new_queue(){
- queue_object *head = malloc(sizeof(queue_object));
- head->next = NULL;
- head->object = NULL;
- return head;
- }
- void free_queue(queue_object* queue){
- if (queue == NULL){
- return;
- }
- else{
- queue_object *current_el = queue->next;
- queue_object *next_el;
- while(current_el != NULL){ //¤t_el->next != NULL?
- next_el = current_el->next;
- free(current_el);
- current_el = next_el;
- }
- }
- free(queue);
- }
- void* queue_peek(queue_object* queue){
- if(queue == NULL || queue->next == NULL){//wenn queue leer
- return NULL;
- }
- else{
- queue_object *prev_el = queue->next;
- void* obj = prev_el->object;
- return obj;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement