Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct sampleStruct{
- string data;
- sampleStruct* next;
- };
- sampleStruct* head = NULL;
- void pushData( string data)
- {
- sampleStruct* newData = new sampleStruct;
- newData->data = data;
- newData->next = head;
- head = newData;
- }
- void insertData(int pos, string data)
- {
- sampleStruct* newData = new sampleStruct;
- sampleStruct* temp = head;
- if(pos == 1)
- {
- head = newData;
- head->data = data;
- head->next = temp;
- }
- else{
- for(int a = 1 ; a < pos-1 ; a++)
- {
- temp = temp->next;
- }
- }
- }
- void printData()
- {
- sampleStruct* temp = head;
- while(temp!= NULL)
- {
- cout << temp->data << endl;
- temp = temp->next;
- }
- }
- int main()
- {
- string name = " ";
- int pos = 0;
- cout << "Input minimum of 5" << endl;
- for(int a = 0; a < 5 ; a++)
- {
- cout << "Input name " << a << endl;
- cin >> name;
- pushData(name);
- }
- cout << "These are your inputs " << endl;
- cout << endl;
- printData();
- while(1)
- {
- cout << "Input name and the position to insert at" << endl;
- cout << "Input name" << endl;
- cin >> name;
- cout << "Input pos" << endl;
- cin >> pos;
- insertData(pos , name);
- cout <<" After insertion " <<endl;
- printData();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement