Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int val;
- struct node *NEXT;
- struct node *PREV;
- } node_t;
- node_t *first, *last, *temp;
- void create () {
- first = (node_t*) malloc (sizeof(node_t));
- last = (node_t*) malloc (sizeof (node_t));
- first = last = NULL;
- }
- int isEmpty() {
- if (first == NULL) return 1;
- else return 0;
- }
- void display () {
- temp = first;
- if (isEmpty ()) printf("The list is empty.");
- while (temp != NULL) {
- printf("%d ", temp->val);
- temp = temp->NEXT;
- }
- printf("\n");
- }
- void reverseDisplay() {
- temp = last;
- if (isEmpty ()) printf("The list is empty.");
- while (temp != NULL) {
- printf("%d ", temp->val);
- temp = temp->PREV;
- }
- printf("\n");
- }
- void insertFirst (int num) {
- temp = (node_t*) malloc (sizeof (node_t));
- temp->val = num;
- temp->NEXT = first;
- temp->PREV = NULL;
- if (first!=NULL) first->PREV = temp;
- else last = temp;
- first = temp;
- }
- void insertLast (int num) {
- temp = (node_t*) malloc (sizeof (node_t));
- temp->val = num;
- temp->PREV = last;
- temp->NEXT = NULL;
- if (last!=NULL )last->NEXT = temp;
- else first = temp;
- last = temp;
- }
- int popFirst() {
- int toBeReturned=0;
- if (!isEmpty()) {
- toBeReturned = first->val;
- temp = (node_t*) malloc (sizeof (node_t));
- temp = first->NEXT;
- if (first->NEXT == NULL) {
- first = last = NULL;
- }
- else {
- free(first);
- temp->PREV = NULL;
- first = temp;
- }
- }
- return toBeReturned;
- }
- int popLast () {
- int toBeReturned=0;
- if (!isEmpty()) {
- toBeReturned = last->val;
- temp = (node_t*) malloc (sizeof (node_t));
- temp = last->PREV;
- if (first->NEXT == NULL) {
- first = last = NULL;
- }
- else {
- free(last);
- temp->NEXT = NULL;
- last = temp;
- }
- }
- return toBeReturned;
- }
- int displayFirst() {
- if (isEmpty()) return 0;
- return first->val;
- }
- int displayLast() {
- if (isEmpty()) return 0;
- return last->val;
- }
- void menu() {
- while (1) {
- printf("Press 1 to insert at the beginning.\n");
- printf("Press 2 to insert at the end.\n");
- printf("Press 3 to pop an element from the beginning.\n");
- printf("Press 4 to pop an element from the end.\n");
- printf("Press 5 to show the first element.\n");
- printf("Press 6 to show the last element.\n");
- printf("Press 7 to display the list from first to last.\n");
- printf("Press 8 to display the list in reverse order.\n");
- printf("Press 0 to Exit.\n");
- int n;
- scanf("%d", &n);
- if (n==0) break;
- else if (n==1) {
- printf("Enter the number: ");
- int num;
- scanf("%d", &num);
- insertFirst(num);
- }
- else if (n==2) {
- printf("Enter the number: ");
- int num;
- scanf("%d", &num);
- insertLast(num);
- }
- else if (n==3) {
- if (!isEmpty()) printf("Popped value = %d\n", popFirst());
- else printf("The linked list is empty.\n");
- }
- else if (n==4) {
- if (!isEmpty()) printf("Popped value = %d\n", popLast());
- else printf("The linked list is empty.\n");
- }
- else if (n==5) {
- if (!isEmpty()) printf("The first element = %d\n", displayFirst());
- else printf("The linked list is empty.\n");
- }
- else if (n==6) {
- if (!isEmpty()) printf("The last element = %d\n", displayLast());
- else printf("The linked list is empty.\n");
- }
- else if (n==7) display();
- else if (n==8) reverseDisplay();
- else {
- printf("Invalid choice! Try again.\n");
- }
- }
- }
- int main() {
- create();
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment