Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <stdlib.h>
- typedef struct item {
- int value;
- item* next;
- } item;
- typedef struct item_2 {
- int value;
- item_2* next;
- item_2* prev;
- } item_2;
- /*typedef struct queue {
- item* list;
- } queue;
- void add_in_q(queue* q, int value) {
- add_in_tail(q->list, value);
- }
- int remove_from_q(queue* q) {
- int temp = get_tail(q->list);
- remove_head(q->list);
- return temp;
- }*/
- item* add_in_head(item* head, int new_value) {
- item* new_item = (item*)malloc(sizeof(item));
- new_item->value = new_value;
- new_item->next = head;
- return new_item;
- }
- item* add_in_tail(item* head, int new_value) {
- item* new_item = (item*)malloc(sizeof(item));
- new_item->value = new_value;
- new_item->next = NULL;
- if (!head)
- return new_item;
- item* current = head;
- while (current->next) {
- current = current->next;
- }
- current->next = new_item;
- return head;
- }
- item* remove_head(item* head) {
- if (!head)
- return NULL;
- item* temp = head;
- head = head->next;
- free(temp);
- return head;
- }
- item* remove_tail(item* head) {
- if (!head)
- return NULL;
- if (!head->next) {
- free(head);
- return NULL;
- }
- item* current = head;
- while (current->next->next) {
- current = current->next;
- }
- free(current->next);
- current->next = NULL;
- return head;
- }
- void print_list(item* head) {
- item* current = head;
- while (current) {
- printf("%4d", current->value);
- current = current->next;
- }
- printf("\n");
- return;
- }
- void reverse_print(item* head) {
- if (head->next)
- reverse_print(head->next);
- printf("%4d", head->value);
- return;
- }
- item* remove_by_value(item* head, int value) {
- if (!head)
- return NULL;
- if (head->value == value) {
- return remove_head(head);
- }
- item* current = head;
- while (current->next && current->next->value != value) {
- current = current->next;
- }
- if (!current->next)
- return head;
- item* temp = current->next;
- current->next = current->next->next;
- free(temp);
- return head;
- }
- item* clear_list(item* head) {
- item* current = head;
- item* temp;
- unsigned len;
- unsigned j;
- // get_len();
- while (j < len) {//(current) {
- /*temp = current;
- current = current->next;
- free(temp);*/
- current = remove_head(current);
- j++;
- }
- return NULL;
- }
- int main()
- {
- item* head = NULL;
- head = add_in_head(head, 0);
- head = add_in_head(head, 2);
- head = add_in_head(head, 4);
- head = add_in_head(head, 6);
- head = add_in_tail(head, 3);
- head = add_in_tail(head, 3);
- head = add_in_tail(head, 8);
- head = add_in_tail(head, 3);
- head = add_in_tail(head, 8);
- print_list(head);
- head = remove_by_value(head, 8);
- print_list(head);
- reverse_print(head);
- head = clear_list(head);
- print_list(head);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement