#include "stdafx.h"
#include <iostream>
using namespace std;
struct item
{
int len;
char *str;
item *next;
};
item* listOfStrings = 0;
item* lastItem = 0;
item *input()
{
cout << "Input string or '0' to stop input" << endl;
while (1)
{
int lenght = 0;
if (!scanf_s("%d", &lenght, sizeof(lenght))) { break; }
if ((0 == lenght)&&(!isdigit(lenght))) { break; }
if ((lenght > 9)&&(lenght/10 > 1)) { break; }
item *currentItem = new item;
currentItem->len = lenght;
currentItem->str = new char[lenght + 1];
currentItem->str[lenght] = 0;
currentItem->next = NULL;
if (!scanf_s("%s", currentItem->str, currentItem->len + 1)) { break; }
if (0 == listOfStrings)
{
listOfStrings = currentItem;
lastItem = currentItem;
}
else
{
lastItem->next = currentItem;
lastItem = currentItem;
}
}
return lastItem;
}
item *output()
{
if (listOfStrings == 0)
{
exit(1);
}
item * currentItem = listOfStrings;
while (currentItem)
{
printf_s("%s\n", currentItem->str, currentItem->len);
currentItem = currentItem->next;
}
}
item *outputNthString(int n)
{
if (listOfStrings == 0)
{
exit(1);
}
item * currentItem = listOfStrings;
while ((currentItem)&&(n!=0))
{
currentItem = currentItem->next;
n--;
}
printf_s("%s\n", currentItem->str, currentItem->len);
}
int main()
{
int choose;
while (true)
{
cout << "1)Input data" << endl;
cout << "2)Output data" << endl;
cout << "3)Output Nth string" << endl;
cin >> choose;
switch (choose)
{
case 1: {if (!input()){exit(0); }; continue; }
case 2: {output(); continue; }
case 3:
{
int n;
cout << "Input number of string" << endl;
cin >> n;
outputNthString(n-1);
continue;
}
default: {exit(0); }
}
}
system("pause");
return 0;
}