Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define TRUE 1
- #define FALSE 0
- typedef struct _node {
- int value;
- struct _node* next;
- } Node;
- /*
- *
- */
- void show_nodes(Node* node) {
- while(node != NULL) {
- printf("Node value: %d\n", node->value);
- node = node->next;
- }
- }
- void clear_nodes(Node* node) {
- while(node != NULL) {
- Node* tmp = node->next;
- free(node);
- node = tmp;
- }
- }
- int ex17(){
- Node* first = NULL;
- Node* last = NULL;
- int finished = FALSE;
- while(finished == FALSE) {
- int value;
- int qty = scanf("%d", &value);
- if(qty <= 0) {
- finished = TRUE;
- } else {
- Node* current = (Node*) malloc(sizeof(Node));
- current->value = value;
- current->next = NULL;
- if (first == NULL) {
- first = current;
- last = current;
- } else {
- Node* oldfirst = NULL;
- oldfirst = first;
- first = current;
- first->next = oldfirst;
- }
- }
- }
- show_nodes(first);
- clear_nodes(first);
- }
- int ex18(){
- Node* first = NULL;
- Node* last = NULL;
- int end = FALSE;
- while(end == FALSE){
- int value;
- int qty = scanf("%d", &value);
- if(qty <= 0){
- end = TRUE;
- }
- else{
- Node* current = (Node*) malloc(sizeof(Node));
- current->value = value;
- current->next = NULL;
- if (first == NULL){
- first = current;
- last = current;
- }
- else{
- last->next = current;
- last = current;
- }
- }
- }
- show_nodes(first);
- clear_nodes(first);
- }
- int ex19(){
- Node* first = NULL;
- Node* last = NULL;
- int end = FALSE;
- while(end == FALSE){
- int value;
- int qty = scanf("%d", &value);
- if(qty <= 0){
- end = TRUE;
- }
- else{
- Node* current = (Node*) malloc(sizeof(Node));
- current->value = value;
- current->next = NULL;
- if (first == NULL){
- first = current;
- last = current;
- }
- else{
- Node* compare_with = first;
- int search = TRUE;
- if (current->value < first->value){
- Node* oldfirst = NULL;
- oldfirst = first;
- first = current;
- first->next = oldfirst;
- }
- else{
- if (current->value > last->value){
- last->next = current;
- last = current;
- }
- else{
- while(search == TRUE){
- if (compare_with->next->value < current->value){
- compare_with = compare_with->next;
- }
- else{
- Node* next_node = NULL;
- next_node = compare_with->next;
- compare_with->next = current;
- current->next = next_node;
- search = FALSE;
- }
- }
- }
- }
- }
- }
- }
- show_nodes(first);
- clear_nodes(first);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement