Wednesday 8 June 2016

Linked List FIFO

FIFO List

#include <stdio.h>
#include <stdlib.h>

struct node {
        int             data;
        struct node *next;
};

void InsertToFIFO (struct node **head, struct node **tail, int value)
{
        struct node *newNode = NULL;

        newNode = (struct node *) malloc(sizeof(struct node));
        if (newNode == NULL)
                return;

        newNode->data = value;
        newNode->next = NULL;

        if (*head == NULL) {
                        *head = newNode;
                        *tail = newNode;
        } else {
                        (*tail)->next = newNode;
                        *tail   = newNode;
        }

        return;
}

void PrintList (struct node **head)
{
        struct node *temp = *head;

        while (temp) {
                printf("%d\r\n", temp->data);
                temp = temp->next;
        }

        return;
}

int main(void)
{
        int i;
        struct node *head = NULL, *tail = NULL;

        for (i = 1; i <= 10; i++) {
                InsertToFIFO(&head, &tail, i);
        }
        printf("FIFO List \r\n");
        PrintList(&head);

        return 0;
}

Makefile:
CC = gcc

CFLAGS = -g -Wall

all:  queue_list
queue_list: queue_list.o
        ${CC} ${CFLAGS} -o $@ queue_list.o

clean:
        -@rm *.o

distclean: clean
         -@rm    queue_list

Output:
FIFO List
1
2
3
4
5
6
7
8
9
10

No comments:

Post a Comment