#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;
return newnode;
}
Node* InsertAtPos( Node* root, string s, int pos) {
if ( pos == 0 ) {
return InsertAtBegin( root, s) ;
}
Node* newnode = new Node( ) ;
newnode- > s = s;
newnode- > next = NULL ;
Node* curr = root;
for ( int i = 1 ; i < pos && curr! = NULL ; i++ ) {
curr = curr- > next;
}
if ( curr== NULL )
{
cout << "Position out of bounds" << endl;
delete newnode;
return root;
}
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 ;
if ( root== NULL || s< root- > s)
{
newnode- > next= root;
return newnode;
}
Node* curr= root;
while ( curr- > next! = NULL && curr- > next- > s< s)
{
curr= curr- > next;
}
newnode- > next= curr- > next;
curr- > next= newnode;
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)
{
if ( root== NULL )
{
cout << "List is empty" << endl;
return root;
}
if ( root- > s== s)
{
Node* temp= root;
root= root- > next;
delete temp;
return root;
}
Node* curr= root;
while ( curr- > next! = NULL && curr- > next- > s! = s)
{
curr= curr- > next;
}
if ( curr- > next== NULL )
{
cout << "Value is not found" << endl;
return root;
}
Node* temp= curr- > next;
curr- > next= temp- > next;
delete temp;
return root;
}
void DeleteList( Node* & root)
{
while ( root! = NULL )
{
Node* temp= root;
root= root- > next;
delete temp;
}
}
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) ;
DeleteList( root) ;
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgTm9kZSB7CiAgICBzdHJpbmcgczsKICAgIE5vZGUqIG5leHQ7Cn07CgoKTm9kZSogSW5zZXJ0QXRCZWdpbihOb2RlKiByb290LCBzdHJpbmcgcykgewogICAgTm9kZSogbmV3bm9kZSA9IG5ldyBOb2RlKCk7CiAgICBuZXdub2RlLT5zID0gczsKICAgIG5ld25vZGUtPm5leHQgPSByb290OwogICAgcmV0dXJuIG5ld25vZGU7CiAgICAKfQpOb2RlKiBJbnNlcnRBdFBvcyhOb2RlKiByb290LCBzdHJpbmcgcywgaW50IHBvcykgewogICAgCiAgICBpZiAocG9zID09IDApIHsKICAgICAgICByZXR1cm4gSW5zZXJ0QXRCZWdpbihyb290LCBzKTsKICAgIH0KCiAgICBOb2RlKiBuZXdub2RlID0gbmV3IE5vZGUoKTsKICAgIG5ld25vZGUtPnMgPSBzOwogICAgbmV3bm9kZS0+bmV4dCA9IE5VTEw7CgogICAgTm9kZSogY3VyciA9IHJvb3Q7CiAgICBmb3IgKGludCBpID0gMTsgaSA8IHBvcyAmJiBjdXJyIT1OVUxMO2krKykgewogICAgICAgIAogICAgICAgIGN1cnIgPSBjdXJyLT5uZ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