HERE IS THE CODE
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; struct node * getNode(int); void addq(struct node **, struct node **, int); int delq(struct node **, struct node **); void main() { struct node *front = NULL, *rear = NULL; int i, n, data; printf("Enter the number of elements\n"); scanf("%d", &n); printf("Enter the elements\n"); for (i=0;i<n;i++) { scanf("%d", &data); addq(&front, &rear, data); } printf("Enter the number of elements to pop\n"); scanf("%d", &n); for (i=0;i<n;i++) { printf("element deleted - %d\n", delq(&front, &rear)); } } int delq(struct node **front, struct node **rear) { int data; struct node *temp; if (*front == NULL) { printf("Underflow\n"); return -1; } if (*rear == *front) { data = (*front) -> data; free(*front); *front = *rear = NULL; return data; } else { data = (*front) -> data; temp = *front; *front = (*front) -> next; free(temp); return data; } } void addq(struct node **front, struct node **rear, int data) { struct node *temp; if (*front == NULL) { *rear = *front = getNode(data); } else { temp = *front; while (temp -> next != NULL) { temp = temp -> next; } temp -> next = getNode(data); *rear = temp -> next; } } struct node* getNode(int data) { struct node *temp = (struct node *)malloc(sizeof(struct node)); temp -> data = data; temp -> next = NULL; return temp; }
No comments:
Post a Comment