Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C C++ Linked list.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- #include <conio.h>
- #include <stdlib.h>
- typedef struct Node
- {
- char data[30];
- struct Node* next;
- } Node;
- void AddNode(Node** current, char data[30]);
- void PrintList(Node* start);
- void RemoveList(Node* start);
- int main()
- {
- Node* start;
- Node** current = (Node**)malloc(sizeof(Node*));
- if (current)
- {
- (*current) = (Node*)malloc(sizeof(Node));
- if (*current)
- {
- sprintf_s((*current)->data, sizeof(char[2]), "%s", "1");
- (*current)->next = NULL;
- start = (*current);
- char data[30];
- int k;
- printf_s("%s", "Enter number quantity from 2 to 999\n");
- scanf_s("%s", data);
- k = atoi(data);
- if (k < 2)
- k = 2;
- else if (k > 999)
- k = 999;
- for (int i = 1; i < k; i++)
- {
- sprintf_s(data, sizeof(i), "%i", i + 1);
- AddNode(current, data);
- }
- PrintList(start);
- RemoveList(start);
- }
- free(current);
- }
- printf_s("%s", "\nThe program successfully created the list and deleted it!\n\n");
- system("pause");
- return 0;
- }
- void AddNode(Node** current, char data[30])
- {
- (*current)->next = (Node*)malloc(sizeof(Node));
- (*current) = (*current)->next;
- if (*current)
- {
- strcpy_s((*current)->data, sizeof(data), data);
- (*current)->next = NULL;
- }
- }
- void PrintList(Node* start)
- {
- while(start)
- {
- printf_s("%s\n", start->data);
- start = start->next;
- }
- }
- void RemoveList(Node* start)
- {
- Node* pointer;
- while (start)
- {
- pointer = start->next;
- free(start);
- start = pointer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement