Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct _Node
- {
- int value;
- struct _Node *next;
- } Node;
- Node *createNode(int value)
- {
- Node *newNode = (Node *)malloc(sizeof(Node));
- newNode->value = value;
- newNode->next = NULL;
- return newNode;
- }
- void pushFront(Node **list, int value)
- {
- Node *nextElem = createNode(value);
- nextElem->next = (*list);
- (*list) = nextElem;
- }
- Node *getTail(Node *list)
- {
- if (list == NULL)
- return NULL;
- while (list->next)
- list = list->next;
- return list;
- }
- Node* getPenultimate(Node* list)
- {
- while(list->next->next)
- list=list->next;
- return list;
- }
- void pushBack(Node **list, int value)
- {
- Node *tail = getTail((*list));
- Node *elem = createNode(value);
- elem->next = NULL;
- tail->next = elem;
- }
- void popFront(Node **list)
- {
- if ((*list) == NULL)
- return;
- Node *next = (*list)->next;
- free((*list));
- (*list) = next;
- }
- void popBack(Node **list)
- {
- if ((*list) == NULL)
- return;
- Node* tail = getPenultimate((*list));
- free(tail->next);
- tail->next = NULL;
- }
- int main()
- {
- Node *n = createNode(12);
- printf("front value after createNode(12): %d \n", n->value);
- pushFront(&n, 32);
- printf("tail value after pushFront(&n, 32): %d \n", getTail(n)->value);
- pushBack(&n, 64);
- printf("tail value after pushBack(&n, 64): %d \n", getTail(n)->value);
- printf("front value after pushBack(&n, 64): %d \n", n->value);
- popBack(&n);
- printf("tail value after popBack(&n): %d \n", getTail(n)->value);
- popFront(&n);
- printf("front value after popFront(&n): %d \n", n->value);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement