#include <bits/stdc++.h>
using namespace std;
struct Node {
string s;
Node* next;
};
Node* InsertAtBegin(Node* root, string s) {
Node* newnode = new Node();
newnode->s = s;
newnode->next = root;
root = newnode;
return root;
}
Node* InsertAtPos(Node* root, string s, int pos) {
if (pos == 0) {
root=InsertAtBegin(root, s);
}
Node* newnode = new Node();
newnode->s = s;
newnode->next = NULL;
Node* curr = root;
for (int i = 1; i < pos; i++) {
curr = curr->next;
}
newnode->next = curr->next;
curr->next = newnode;
return root;
}
Node* InsertAtEnd(Node* root, string s) {
Node* newnode = new Node();
newnode->s = s;
newnode->next = NULL;
if (root == NULL) {
return newnode;
}
Node* curr = root;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = newnode;
return root;
}
Node* SortedInsert(Node* root, string s)
{
Node* newnode = new Node();
newnode->s = s;
newnode->next = NULL;
Node* curr = root;
Node* prev = NULL;
if(root==NULL)
{
root=newnode;
return root;
}
if(s<root->s)
{
newnode->next=root;
root=newnode;
return root;
}
while (curr != NULL) {
if(curr->s<s)
{
prev = curr;
curr = curr->next;
}
else
{
prev->next = newnode;
newnode->next = curr;
return root;
}
}
prev->next=newnode;
newnode->next=NULL;
return root;
}
int Search(Node* root, string s) {
int pos = 0;
Node* curr = root;
while (curr != NULL) {
if (curr->s == s) {
return pos;
}
curr = curr->next;
pos++;
}
return -1;
}
Node* Delete(Node* root, string s)
{
Node* curr;
Node* prev;
curr=root;
prev=NULL;
while(curr!=NULL)
{
if(curr->s!=s)
{
prev=curr;
curr=curr->next;
}
else
{
if (curr == root) {
root = root->next;
delete(curr);
}
else
{
prev->next = curr->next;
delete (curr);
}
break;
}
}
return root;
}
void Print(Node* root) {
Node* curr = root;
while (curr != NULL) {
cout << curr->s << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
Node* root = NULL;
root = InsertAtBegin(root, "data");
root = InsertAtBegin(root, "cse");
root = InsertAtBegin(root, "Math");
Print(root);
root = InsertAtPos(root, "hello", 2);
Print(root);
root = InsertAtEnd(root, "end");
Print(root);
root = SortedInsert(root, "alpha");
Print(root);
cout<<"positions of cse:"<<Search(root,"cse")<<endl;
cout<<"positions of I:"<<Search(root,"I")<<endl;
root = Delete(root, "hello");
root=Delete(root,"yoU");
Print(root);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgTm9kZSB7CiAgICBzdHJpbmcgczsKICAgIE5vZGUqIG5leHQ7Cn07CgoKTm9kZSogSW5zZXJ0QXRCZWdpbihOb2RlKiByb290LCBzdHJpbmcgcykgewogICAgTm9kZSogbmV3bm9kZSA9IG5ldyBOb2RlKCk7CiAgICBuZXdub2RlLT5zID0gczsKICAgIG5ld25vZGUtPm5leHQgPSByb290OwogICAgcm9vdCA9IG5ld25vZGU7CiAgICByZXR1cm4gcm9vdDsKfQpOb2RlKiBJbnNlcnRBdFBvcyhOb2RlKiByb290LCBzdHJpbmcgcywgaW50IHBvcykgewogICAgCiAgICBpZiAocG9zID09IDApIHsKICAgICAgICByb290PUluc2VydEF0QmVnaW4ocm9vdCwgcyk7CiAgICB9CgogICAgTm9kZSogbmV3bm9kZSA9IG5ldyBOb2RlKCk7CiAgICBuZXdub2RlLT5zID0gczsKICAgIG5ld25vZGUtPm5leHQgPSBOVUxMOwoKICAgIE5vZGUqIGN1cnIgPSByb290OwogICAgZm9yIChpbnQgaSA9IDE7IGkgPCBwb3M7IGkrKykgewogICAgICAgIAogICAgICAgIGN1cnIgPSBjdXJyLT5uZXh0OwogICAgfQogICAgbmV3bm9kZS0+bmV4dCA9IGN1cnItPm5leHQ7CiAgICBjdXJyLT5uZXh0ID0gbmV3bm9kZTsKCiAgICByZXR1cm4gcm9vdDsKfQoKCk5vZGUqIEluc2VydEF0RW5kKE5vZGUqIHJvb3QsIHN0cmluZyBzKSB7CiAgICBOb2RlKiBuZXdub2RlID0gbmV3IE5vZGUoKTsKICAgIG5ld25vZGUtPnMgPSBzOwogICAgbmV3bm9kZ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Y3Vyci0+cyA9PSBzKSB7CiAgICAgICAgICAgIHJldHVybiBwb3M7CiAgICAgICAgfQogICAgICAgIGN1cnIgPSBjdXJyLT5uZXh0OwogICAgICAgIHBvcysrOwogICAgfQoKICAgIHJldHVybiAtMTsKfQoKCk5vZGUqIERlbGV0ZShOb2RlKiByb290LCBzdHJpbmcgcykKIHsKICAgIE5vZGUqIGN1cnI7CiAgICBOb2RlKiBwcmV2OwoKICAgCmN1cnI9cm9vdDsKcHJldj1OVUxMOwp3aGlsZShjdXJyIT1OVUxMKQp7CmlmKGN1cnItPnMhPXMpCnsKcHJldj1jdXJyOwpjdXJyPWN1cnItPm5leHQ7Cn0KZWxzZQp7CiAgICAgICAgICAgIGlmIChjdXJyID09IHJvb3QpIHsKICAgICAgICAgICAgICAgIHJvb3QgPSByb290LT5uZXh0OwpkZWxldGUoY3Vycik7CiAgICAgICAgICAgIH0KIGVsc2UgCnsKICAgICAgICAgICAgICAgIHByZXYtPm5leHQgPSBjdXJyLT5uZXh0OwogICAgICAgICAgIAogICAgICAgICAgICBkZWxldGUgKGN1cnIpOwp9CmJyZWFrOwp9CiAgICB9ICAgICAgICAKICAgIHJldHVybiByb290Owp9CgoKdm9pZCBQcmludChOb2RlKiByb290KSB7CiAgICBOb2RlKiBjdXJyID0gcm9vdDsKICAgIHdoaWxlIChjdXJyICE9IE5VTEwpIHsKICAgICAgICBjb3V0IDw8IGN1cnItPnMgPDwgIiAiOwogICAgICAgIGN1cnIgPSBjdXJyLT5uZXh0OwogICAgfQogICAgY291dCA8PCBlbmRsOwp9CgppbnQgbWFpbigpIHsKICAgIE5vZGUqIHJvb3QgPSBOVUxMOwoKICAgIAogICAgcm9vdCA9IEluc2VydEF0QmVnaW4ocm9vdCwgImRhdGEiKTsKICAgIHJvb3QgPSBJbnNlcnRBdEJlZ2luKHJvb3QsICJjc2UiKTsKICAgIHJvb3QgPSBJbnNlcnRBdEJlZ2luKHJvb3QsICJNYXRoIik7CiAgICBQcmludChyb290KTsgCgogICAgcm9vdCA9IEluc2VydEF0UG9zKHJvb3QsICJoZWxsbyIsIDIpOwogICAgUHJpbnQocm9vdCk7IAoKICAgIHJvb3QgPSBJbnNlcnRBdEVuZChyb290LCAiZW5kIik7CiAgICBQcmludChyb290KTsKCiAgICByb290ID0gU29ydGVkSW5zZXJ0KHJvb3QsICJhbHBoYSIpOwogICAgUHJpbnQocm9vdCk7IAoKICAgIGNvdXQ8PCJwb3NpdGlvbnMgb2YgY3NlOiI8PFNlYXJjaChyb290LCJjc2UiKTw8ZW5kbDsKY291dDw8InBvc2l0aW9ucyBvZiBJOiI8PFNlYXJjaChyb290LCJJIik8PGVuZGw7CgogICAgcm9vdCA9IERlbGV0ZShyb290LCAiaGVsbG8iKTsKcm9vdD1EZWxldGUocm9vdCwieW9VIik7CiAgICBQcmludChyb290KTsgCgogICAgCgogICAgcmV0dXJuIDA7Cn0=