Advertisement
backlog

list.spl

Jul 19th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.56 KB | None | 0 0
  1. // A simple C program for traversal of a linked list
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4.  
  5. struct Node
  6. {
  7.   int data;
  8.   struct Node *next;
  9. };
  10.  
  11. // This function prints contents of linked list starting from
  12. // the given node
  13. void printList(struct Node *n)
  14. {
  15.   while (n != NULL)
  16.   {
  17.      printf(" %d ", n->data);
  18.      n = n->next;
  19.   }
  20. }
  21.  
  22. int main()
  23. {
  24.   struct Node* head = NULL;
  25.   struct Node* second = NULL;
  26.   struct Node* third = NULL;
  27.    
  28.   // allocate 3 nodes in the heap  
  29.   head  = (struct Node*)malloc(sizeof(struct Node));
  30.   second = (struct Node*)malloc(sizeof(struct Node));
  31.   third  = (struct Node*)malloc(sizeof(struct Node));
  32.  
  33.   head->data = 1; //assign data in first node
  34.   head->next = second; // Link first node with second  
  35.  
  36.   second->data = 2; //assign data to second node
  37.   second->next = third;  
  38.  
  39.   third->data = 3; //assign data to third node
  40.   third->next = NULL;
  41.    
  42.   printList(head);
  43.  
  44.   return 0;
  45. }
  46.  
  47. ///////////
  48.  
  49.  
  50. #include<bits/stdc++.h>
  51. using namespace std;
  52. struct node
  53. {
  54.   int data;
  55.   struct node *next;
  56. };
  57. void  printlist(struct node *n)
  58. {   int co=0;
  59.     while(n !=NULL)
  60.     {   co=co+1;
  61.         cout<<n->data<<"  "<<endl;
  62.         n=n->next;
  63.     }
  64.     cout<<endl<<co<<endl;
  65. }
  66. int main()
  67. {
  68.     node *head,*second,*third=NULL;
  69.     //node *second=NULL;
  70.     // node *third=NULL;
  71.  
  72.     head=(struct node*)malloc(sizeof(struct node));
  73.     second=(struct node*)malloc(sizeof(struct node));
  74.     third=(struct node*)malloc(sizeof(struct node));
  75.  
  76.  
  77.  
  78.     head->data=1;
  79.     head->next=second;
  80.  
  81.         second->data=5;
  82.     second->next=third;
  83.  
  84.         third->data=8;
  85.       third->next=NULL;
  86.  
  87.       printlist(head);
  88.  
  89. }
  90. /////////////////
  91.  
  92.  
  93. //insert value at  first
  94. #include<bits/stdc++.h>
  95. using namespace std;
  96. struct node
  97. {
  98.     int data;
  99.     struct node *link;
  100. };
  101. struct node* head;
  102. void print()
  103. {
  104.     struct node *temp;
  105.     temp=head;
  106.     while(temp !=NULL)
  107.     {
  108.         cout<<temp->data<<"  ";
  109.         temp=temp->link;
  110.     }
  111.  
  112. }
  113. void insertt(int value)
  114. {
  115.     struct node* temp=(struct node*)malloc(sizeof(struct node));
  116.     temp->data=value;
  117.     temp->link=head;
  118.     head=temp;}
  119.  
  120. int main()
  121. {
  122.     head=NULL;
  123.     insertt(1);
  124.      insertt(2);
  125.       insertt(7);
  126.  
  127.      print();
  128.  }
  129.  
  130. ///////////
  131.  
  132. //insert at the end
  133. #include<bits/stdc++.h>
  134. using namespace std;
  135. struct node
  136. {
  137.     int data;
  138.     struct node *link;
  139. };
  140. struct node* head;
  141. void print()
  142. {
  143.     struct node *temp;
  144.     temp=head;
  145.     while(temp !=NULL)
  146.     {
  147.         cout<<temp->data<<"  ";
  148.         temp=temp->link;
  149.     }
  150.  
  151. }
  152. void insertt(int value)
  153. {
  154.     struct node* temp=(struct node*)malloc(sizeof(struct node));
  155.     temp->data=value;
  156.     temp->link=NULL;
  157.     if(head==NULL)
  158.     {
  159.         head=temp;
  160.     }
  161.     else
  162.     {
  163.         struct node *t;
  164.         t=head;
  165.         while(t->link!=NULL)
  166.         {
  167.            t=t->link;
  168.         }
  169.         t->link=temp;
  170.     }
  171. }
  172.  
  173. int main()
  174. {
  175.     head=NULL;
  176.     insertt(1);
  177.      insertt(2);
  178.       insertt(7);
  179.  
  180.      print();
  181.  }
  182.  
  183. ////////
  184.  
  185. ////////////////////////////////////////////////////
  186. //insert after a given position
  187. #include<bits/stdc++.h>
  188. using namespace std;
  189.  
  190. struct node
  191. {  int data;
  192.     struct node* next;
  193. };
  194. struct node* head;
  195. void insert(int data,int n)
  196. {
  197. node * temp1=(struct node*)malloc(sizeof(struct node));
  198. temp1->data=data;
  199. temp1->next=NULL;
  200. if(n==1)
  201. {
  202. temp1->next=head;
  203. head=temp1;
  204. return;
  205. }
  206. node* temp2=head;
  207. for(int i=0;i<n-2;i++)
  208. {temp2=temp2->next;}
  209. temp1->next=temp2->next;
  210. temp2->next=temp1;
  211. }
  212. void print()
  213. {
  214. node *temp=head;
  215. while(temp!=NULL)
  216. {cout<<temp->data <<"  ";
  217. temp=temp->next;
  218. }
  219. cout<<endl;
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. int main()
  227. {
  228. head=NULL;
  229. insert(2,1);//2
  230. insert(3,2);//2,3
  231. insert(4,1);//4,2,3
  232. insert(5,2);//4,5,2,3
  233.  
  234. print();
  235. }
  236.  
  237.  
  238. ///////////////
  239.  
  240.  
  241.  
  242. //insert value at an sorted array
  243. #include<bits/stdc++.h>
  244. using namespace std;
  245. struct node
  246. {
  247.     int data;
  248.     struct node *link;
  249. };
  250. struct node* head;
  251. void print()
  252. {
  253.     struct node *temp;
  254.     temp=head;
  255.     while(temp !=NULL)
  256.     {
  257.         cout<<temp->data<<"  ";
  258.         temp=temp->link;
  259.     }
  260.  
  261. }
  262. void insertt(int value)
  263. {
  264.     struct node* temp=(struct node*)malloc(sizeof(struct node));
  265.     temp->data=value;
  266.     temp->link=head;
  267.     head=temp;
  268.  
  269. }
  270. void insertn(int value)
  271. {
  272.     struct node* temp1=(struct node*)malloc(sizeof(struct node));
  273.     temp1->data=value;
  274.    if(head==NULL || temp1->data < head->data)
  275.    {
  276.        temp1->link=head;
  277.        head=temp1;
  278.    }
  279.     else
  280. {
  281.         struct node* pred=head;
  282.         struct node* p=pred->link;
  283.         while(p!=NULL && temp1->data > p->data)
  284.         {
  285.             pred=p;
  286.             p=p->link;
  287.         }
  288.         pred->link=temp1;
  289.         temp1->link=p;
  290.     }
  291.  
  292. }
  293.  
  294. void deletnode(int position)
  295. {
  296.     if(position==1)
  297.     {
  298.         struct node* temp;
  299.         temp=head;
  300.         head=head->link;
  301.         free(temp);
  302.     }
  303.     else
  304.     {
  305.         struct node *temp1;
  306.         temp1=head;
  307.         for(int i=1;i<=position-2;i++)
  308.         {
  309.             temp1=temp1->link;
  310.         }
  311.         struct node* temp2;
  312.         temp2=temp1->link;
  313.         temp1->link=temp2->link;
  314.         free(temp2);
  315.     }
  316. }
  317. int main()
  318. {
  319.     head=NULL;
  320.     insertt(9);
  321.      insertt(7);
  322.       insertt(6);
  323.        insertt(1);
  324.       print();
  325.     insertn(8); // deletnode(2);
  326.      cout<<endl;
  327.  
  328.      print();
  329.  }
  330. //
  331.  
  332. /////////////////
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. //insert and delet value
  341. #include<bits/stdc++.h>
  342. using namespace std;
  343. struct node
  344. {
  345.     int data;
  346.     struct node *link;
  347. };
  348. struct node* head;
  349. void print()
  350. {
  351.     struct node *temp;
  352.     temp=head;
  353.     while(temp !=NULL)
  354.     {
  355.         cout<<temp->data<<"  ";
  356.         temp=temp->link;
  357.     }
  358.  
  359. }
  360. void insertt(int value)
  361. {
  362.     struct node* temp=(struct node*)malloc(sizeof(struct node));
  363.     temp->data=value;
  364.     temp->link=head;
  365.     head=temp;
  366.  
  367.  
  368. }
  369. void deletnode(int position)
  370. {
  371.     if(position==1)
  372.     {
  373.         struct node* temp;
  374.         temp=head;
  375.         head=head->link;
  376.         free(temp);
  377.     }
  378.     else
  379.     {
  380.         struct node *temp1;
  381.         temp1=head;
  382.         for(int i=1;i<=position-2;i++)
  383.         {
  384.             temp1=temp1->link;
  385.         }
  386.         struct node* temp2;
  387.         temp2=temp1->link;
  388.         temp1->link=temp2->link;
  389.         free(temp2);
  390.     }
  391. }
  392. int main()
  393. {
  394.     head=NULL;
  395.     insertt(1);
  396.      insertt(2);
  397.       insertt(7);
  398.        insertt(9);
  399.       print();
  400.       deletnode(2);
  401.      cout<<endl;
  402.  
  403.      print();
  404.  }
  405.  
  406.  
  407. //////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement