SHARE
TWEET

Untitled

a guest Oct 18th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2.  
  3. /* typedef declarations */
  4. typedef struct SinglyNode {
  5.     struct SinglyNode *next;
  6.     int data;
  7. } SinglyNode;
  8. typedef struct DoublyNode {
  9.     struct DoublyNode *next, *prev;
  10.     int data;
  11. } DoublyNode;
  12. typedef struct LinkedQueue {
  13.     SinglyNode *front, *rear;
  14. } LinkedQueue;
  15. typedef struct LinkedStack {
  16.     SinglyNode *top;
  17. } LinkedStack;
  18.  
  19. /* operations */
  20. /* singly linked list */
  21. void sllInsert(SinglyNode **head, int data) {
  22.     SinglyNode *tempNode;
  23.     if (*head == NULL) {
  24.         *head = (SinglyNode *)malloc(sizeof(SinglyNode));
  25.         (*head)->data = data;
  26.         (*head)->next = NULL;
  27.     }
  28.     else {
  29.         tempNode = *head;
  30.         while(tempNode->next != NULL) {
  31.             tempNode = tempNode->next;
  32.         }
  33.         tempNode->next = (SinglyNode *)malloc(sizeof(SinglyNode));
  34.         tempNode->next->data = data;
  35.         tempNode->next->next = NULL;
  36.     }
  37. }
  38. int sllDelete(SinglyNode **head) {
  39.     int data;
  40.     SinglyNode *tempNode;
  41.     if ((*head)->next == NULL) {
  42.         data = (*head)->data;
  43.         free(*head);
  44.         *head = NULL;
  45.     }
  46.     else {
  47.         tempNode = *head;
  48.         while(tempNode->next->next != NULL) {
  49.             tempNode = tempNode->next;
  50.         }
  51.         data = tempNode->next->data;
  52.         free(tempNode->next);
  53.         tempNode->next = NULL;
  54.     }
  55.     return data;
  56. }
  57. int sllIsEmpty(SinglyNode *head) {
  58.     return head == NULL;
  59. }
  60.  
  61. /* doubly linked list */
  62. void dllInsert(DoublyNode **head, int data) {
  63.     DoublyNode *tempNode;
  64.     if (*head == NULL) {
  65.         *head = (DoublyNode *)malloc(sizeof(DoublyNode));
  66.         (*head)->data = data;
  67.         (*head)->next = NULL;
  68.         (*head)->prev = NULL;
  69.     }
  70.     else {
  71.         tempNode = *head;
  72.         while(tempNode->next != NULL) {
  73.             tempNode = tempNode->next;
  74.         }
  75.         tempNode->next = (DoublyNode *)malloc(sizeof(DoublyNode));
  76.         tempNode->next->data = data;
  77.         tempNode->next->next = NULL;
  78.         tempNode->next->prev = tempNode;
  79.     }
  80. }
  81. int dllDelete(DoublyNode **head) {
  82.     int data;
  83.     DoublyNode *tempNode;
  84.     if ((*head)->next == NULL) {
  85.         data = (*head)->data;
  86.         free(*head);
  87.         *head = NULL;
  88.     }
  89.     else {
  90.         tempNode = *head;
  91.         while(tempNode->next->next != NULL) {
  92.             tempNode = tempNode->next;
  93.         }
  94.         data = tempNode->next->data;
  95.         free(tempNode->next);
  96.         tempNode->next = NULL;
  97.     }
  98.     return data;
  99. }
  100. int dllIsEmpty(DoublyNode *head) {
  101.     return head == NULL;
  102. }
  103.  
  104. /* circular linked list */
  105. void cllInsert(SinglyNode **head, int data) {
  106.     SinglyNode *tempNode;
  107.     if (*head == NULL) {
  108.         *head = (SinglyNode *)malloc(sizeof(SinglyNode));
  109.         (*head)->data = data;
  110.         (*head)->next = *head;
  111.     }
  112.     else {
  113.         tempNode = *head;
  114.         while(tempNode->next != *head) {
  115.             tempNode = tempNode->next;
  116.         }
  117.         tempNode->next = (SinglyNode *)malloc(sizeof(SinglyNode));
  118.         tempNode->next->data = data;
  119.         tempNode->next->next = *head;
  120.     }
  121. }
  122. int cllDelete(SinglyNode **head) {
  123.     int data;
  124.     SinglyNode *tempNode;
  125.     if ((*head)->next == NULL) {
  126.         data = (*head)->data;
  127.         free(*head);
  128.         *head = NULL;
  129.     }
  130.     else {
  131.         tempNode = *head;
  132.         while(tempNode->next->next != NULL) {
  133.             tempNode = tempNode->next;
  134.         }
  135.         data = tempNode->next->data;
  136.         free(tempNode->next);
  137.         tempNode->next = *head;
  138.     }
  139.     return data;
  140. }
  141. int cllIsEmpty(SinglyNode *head) {
  142.     return head == NULL;
  143. }
  144.  
  145. /* Linked Queue operations */
  146. void enqueue(LinkedQueue *queue, int data) {
  147.     if (queue->rear == NULL) {
  148.         queue->rear = (SinglyNode *)malloc(sizeof(SinglyNode));
  149.         queue->rear->data = data;
  150.         queue->rear->next = NULL;
  151.         queue->front = queue->rear;
  152.     }
  153.     else {
  154.         queue->rear->next = (SinglyNode *)malloc(sizeof(SinglyNode));
  155.         queue->rear->next->data = data;
  156.         queue->rear->next->next = NULL;
  157.         queue->rear = queue->rear->next;
  158.     }
  159. }
  160. int dequeue(LinkedQueue *queue) {
  161.     int data = queue->front->data;
  162.     SinglyNode *tempNode = queue->front;
  163.     queue->front = queue->front->next;
  164.     free(tempNode);
  165.     return data;
  166. }
  167. int queueIsEmpty(LinkedQueue *queue) {
  168.     return queue->front == NULL;
  169. }
  170.  
  171. /* Linked Stack operations */
  172. void push(LinkedStack *stack, int data) {
  173.     SinglyNode *tempNode;
  174.     if (stack->top == NULL) {
  175.         stack->top = (SinglyNode *)malloc(sizeof(SinglyNode));
  176.         stack->top->next = NULL;
  177.         stack->top->data = data;
  178.     }
  179.     else {
  180.         tempNode = (SinglyNode *)malloc(sizeof(SinglyNode));
  181.         tempNode->data = data;
  182.         tempNode->next = stack->top;
  183.         stack->top = tempNode;
  184.     }
  185. }
  186. int pop(LinkedStack *stack) {
  187.     int data = stack->top->data;
  188.     SinglyNode *tempNode = stack->top;
  189.     stack->top = stack->top->next;
  190.     free(tempNode);
  191.     return data;
  192. }
  193. int stackIsEmpty(LinkedStack *stack) {
  194.     return stack->top == NULL;
  195. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top