Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<time.h>
- #include <stdio.h>
- #include <stdlib.h>
- using namespace std;
- struct Node{
- int val;
- Node *next;
- };
- void InsertLast(Node *&head,int n)
- {
- if(head==NULL)
- {
- Node * aux=new Node;
- aux->val=n;
- aux->next=aux;
- head=aux;
- return;
- }
- if(head->next==head)
- {
- Node * aux=new Node;
- aux->val=n;
- head->next=aux;
- aux->next=head;
- return;
- }
- Node * aux=new Node;
- aux->val=n;
- aux->next=head->next;
- head->next=aux;
- int temp=aux->val;
- aux->val=head->val;
- head->val=temp;
- head=aux;
- }
- void ShowList(Node *head)
- {
- Node *aux=head;
- while(aux!=NULL)
- {
- cout<<aux->val<<" ";
- aux=aux->next;
- }
- }
- void Insert_One(Node *&head, int n) // Studentul Popescu
- {
- if(head==NULL)
- {
- Node * aux=new Node;
- aux->val=n;
- aux->next=aux;
- head=aux;
- return;
- }
- Node *aux=head;
- while(aux->next!=head)
- {
- aux=aux->next;
- }
- Node *temp=new Node;
- temp->val=n;
- aux->next=temp;
- temp->next=head;
- }
- void Insert_Two(Node *&last,int n) // Studentul Ionescu
- {
- if(last==NULL)
- {
- Node * aux=new Node;
- aux->val=n;
- aux->next=aux;
- last=aux;
- return;
- }
- Node *aux=new Node;
- aux->val=n;
- aux->next=last->next;
- last->next=aux;
- last=aux;
- }
- int main(void)
- {
- Node *head=NULL,*tail=NULL;
- clock_t start, finish;
- long loop;
- double result,r2,elapsed_time;
- start=clock();
- for(int i=0;i<10000;++i)
- {
- Insert_One(head,i);
- }
- finish=clock();
- elapsed_time = (double)(finish - start) / CLOCKS_PER_SEC;
- cout<<"Popescu: "<<elapsed_time<<"\n";
- start=clock();
- for(int i=0;i<10000;++i)
- {
- Insert_Two(head,i);
- }
- finish=clock();
- elapsed_time = (double)(finish - start) / CLOCKS_PER_SEC;
- cout<<"Ionescu: "<<elapsed_time<<"\n";
- //ShowList(list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement