Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef struct Node
- {
- int data;
- struct Node *next;
- } Node;
- // create new node
- Node *makeNode(int key)
- {
- Node *newNode = (Node *)malloc(sizeof(Node));
- if (!newNode)
- return NULL;
- newNode->data = key;
- newNode->next = NULL;
- return newNode;
- }
- // adds a new node item to the end of the list
- Node *add_last(Node *head, Node *newItem)
- {
- Node *currItem;
- if (!head)
- return newItem;
- currItem = head;
- while (currItem->next)
- currItem = currItem->next;
- currItem->next = newItem;
- return head;
- }
- int help(Node *head, int data, int flag)
- {
- if (!head)
- return 1;
- if (data >= head->data)
- flag = help(head->next, data, flag);
- else
- return 0;
- return flag;
- }
- // (2)->(7)->(1)->(3)->(6)
- Node *delMax(Node *head)
- {
- if (!head->next)
- return;
- if (help(head->next, head->data, 0))
- return head->next;
- if (help(head, head->next->data, 0))
- {
- head->next = head->next->next;
- return;
- }
- delMax(head->next);
- return head;
- }
- void main()
- {
- Node *res;
- Node *head;
- head = makeNode(2); // (2)->(7)->(1)->(3)->(6)
- head = add_last(head, makeNode(7));
- head = add_last(head, makeNode(1));
- head = add_last(head, makeNode(3));
- head = add_last(head, makeNode(6));
- res = delMax(head);
- Node *current = res;
- while (current)
- {
- printf("%d ", current->data);
- current = current->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement