Guest User

Untitled

a guest
Mar 23rd, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef struct node{
  4. string data;
  5. node *next;
  6. }node;
  7. node *create(string key){
  8. node *newnode = new node;
  9. newnode->data=key;
  10. newnode->next=nullptr;
  11. return newnode;
  12. }
  13. node *ins(node *root,string key){
  14. if(root==nullptr) return create(key);
  15. node *newnode = create(key);
  16. if(key<root->data) {
  17. newnode->next=root;
  18. return newnode;
  19. }
  20. node *head=root;
  21. while(root->next!=nullptr && root->next->data<key){
  22. root=root->next;
  23. }
  24. newnode->next=root->next;
  25. root->next=newnode;
  26. return head;
  27. }
  28. void printlist(node *root){
  29. while(root!=nullptr){
  30. cout<<root->data<<"\n";
  31. root=root->next;
  32. }
  33. }
  34. int main(){
  35. node *root=nullptr;
  36. int x;
  37. scanf("%d",&x);
  38. if(x==2) printf("Empty");
  39. else {
  40. while(x==1){
  41. scanf("%d",&x);
  42. if(x==1){
  43. string s;
  44. cin>>s;
  45. root=ins(root,s);
  46. }
  47. }
  48. }
  49. printlist(root);
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment