Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- struct Node {
- char data[80];
- Node* next;
- };
- void AddWord(Node*& first, char* word)
- {
- if (first == NULL)
- {
- first = new Node;
- strcpy_s(first->data, word);
- first->next = NULL;
- }
- else
- {
- Node* cur = first;
- while (cur->next != NULL)
- cur = cur->next;
- Node* new_cur = new Node;
- strcpy_s(new_cur->data, word);
- cur->next = new_cur;
- new_cur->next = NULL;
- }
- }
- void RemoveWords(Node*& first)
- {
- Node* tmp = first;
- first = first->next;
- delete tmp;
- Node* cur = first;
- bool flag = true;
- while (flag)
- {
- if (cur->next->next->next == NULL)
- {
- tmp = cur->next;
- cur->next = cur->next->next;
- delete tmp;
- flag = false;
- }
- cur = cur->next;
- }
- }
- void ListOutput(Node* first)
- {
- Node* cur = first;
- while (cur != NULL)
- {
- printf("%s ", cur->data);
- cur = cur->next;
- }
- }
- void MemoryClear(Node*& first)
- {
- while (first != NULL)
- {
- Node* tmp = first;
- first = first->next;
- delete tmp;
- }
- }
- int main()
- {
- char str[160], * buffer[160];
- char* ptr;
- Node* first = NULL;
- puts("Enter the string:");
- gets_s(str);
- ptr = strtok_s(str, " ,.", buffer);
- while (ptr != NULL)
- {
- AddWord(first, ptr);
- ptr = strtok_s(NULL, " ,.", buffer);
- }
- puts("\nFormed list:");
- ListOutput(first);
- RemoveWords(first);
- puts("\n\nList with removed elements:");
- ListOutput(first);
- MemoryClear(first);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement