Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct _node {
- int data;
- struct _node *next;
- } node;
- node *addNode(node *head, int num)
- {
- node * el = head;
- node * n = (node*) malloc(sizeof(node));
- n->next = NULL;
- n->data = num;
- if (head)
- {
- while (el->next)
- el = el->next;
- el->next = n;
- return head;
- }
- else
- {
- return n;
- }
- }
- node *addBeforeHead(node *head, int data)
- {
- node *nNode;
- nNode = (node*) malloc(sizeof(node));
- nNode->data = data;
- nNode->next = NULL;
- if (head)
- nNode->next = head;
- head = nNode;
- return head;
- }
- void removeNode(node **head, node *del)
- {
- node *el = *head;
- if (*head == del)
- {
- *head = del->next;
- }
- else
- {
- while (el->next && el->next != del)
- el = el->next;
- if (!el->next) return;
- el->next = del->next;
- }
- free(del);
- }
- void printList(node *head)
- {
- node *el = head;
- while (el)
- {
- printf("%d, ", el->data);
- el = el->next;
- }
- }
- int main()
- {
- int n, i, fi, t;
- node *head = NULL, *f = NULL, *h2 = NULL;
- while (1)
- {
- scanf("%d", &t);
- if (t == 0) break;
- head = addNode(head, t);
- }
- f = head;
- while (f)
- {
- int tmp;
- if (f->data < 0)
- {
- tmp = f->data;
- removeNode(&head, f);
- h2 = addBeforeHead(h2, tmp);
- f = head;
- }
- f = f->next;
- }
- f = head;
- while (f->next)
- f = f->next;
- f->next = h2;
- printList(head);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment