Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef struct node{
- string data;
- node *next;
- }node;
- node *create(string key){
- node *newnode = new node;
- newnode->data=key;
- newnode->next=nullptr;
- return newnode;
- }
- node *ins(node *root,string key){
- if(root==nullptr) return create(key);
- node *newnode = create(key);
- if(key<root->data) {
- newnode->next=root;
- return newnode;
- }
- node *head=root;
- while(root->next!=nullptr && root->next->data<key){
- root=root->next;
- }
- newnode->next=root->next;
- root->next=newnode;
- return head;
- }
- void printlist(node *root){
- while(root!=nullptr){
- cout<<root->data<<"\n";
- root=root->next;
- }
- }
- int main(){
- node *root=nullptr;
- int x;
- scanf("%d",&x);
- if(x==2) printf("Empty");
- else {
- while(x==1){
- scanf("%d",&x);
- if(x==1){
- string s;
- cin>>s;
- root=ins(root,s);
- }
- }
- }
- printlist(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment