//list.h
#ifndef List_h
#define List_h
#include "Node.h"
class List
{
public:
List()
{
size=0;
head =NULL;//
final=NULL;//
}
~List()
{
delete head;
}
void add(int x);
void deleete(int x);
void print();
private:
Node *head;//baslangic
Node *final;//son
int size;
};
#endif
//node.h
#ifndef Node_h
#define Node_h
#include <iostream.h>
class Node
{
public:
int x;//data
Node *next;//kendinden sora geleni gosterio
Node()
{
next=NULL;
}
~Node()
{
if(next!=NULL)delete next;
}
};
#endif
//list.cpp
#include "List.h"
void List::add(int x)
{
Node *temp;
temp=new Node;
temp->x=x;
if (head==NULL)//ilk girile sayiyi head e esitliyor
{
head=temp;
final=temp;
}
else
{
if (temp->x >= final->x)//eger son degerden de buyukse en saga ekliyor
{
final->next=temp;
final=temp;
final->next=NULL;
//final->x=temp->x;
}
else//aradaysa aradakileri teker teker karsilastiriyor*/
{
if(temp->x <= head->x)
{
temp->next=head;
head=temp;
}
else
{
Node *headptr=head;
for (int i=0; i<size; i++)
{
if((temp->x >= headptr->x) && (temp->x <= headptr->next->x))
{
temp->next=headptr->next;
headptr->next=temp;
break;
}
else
headptr=headptr->next;
}
}
}
}
size=size+1;
cout<<"List::add("<<temp->x<<")."<<endl;
}
void List::deleete(int x)
{
Node *temp=head,*prev=head;
int t=0;
for (int i=0; i<size;i++)
{
if(temp->x==x)
{
prev->next=temp->next;
if(head==temp)
head=temp->next;
temp->next=NULL;
delete temp;
i=size;
t++;
}
else
{
prev=temp;
temp=temp->next;
}
}
if(t!=0)
{
size--;
cout<<"List::del("<<x<<")."<<endl;
}
else
cout<<endl<<" yanlis sayi girildi "<<endl;
}
void List::print()
{
Node *temp;
temp=head;
cout<<"List::print() output: ";
do {
cout<<"["<<temp->x<<"]-->";
temp=temp->next;
}while (temp!=NULL);
cout<<"NULL"<<endl;
}
//main
#include "List.h"
int main()
{
List list;
int x,y;
do
{
cout<<"\n1) Sayi gir \n2) Sayi sil\n3) Print\n";
cout<<endl<<"type -1 to exit"<<endl;
cin>>x;
switch (x)
{
case 1:
cout<<"\nSayi girin : ";
cin>>y;
list.add(y);
break;
case 2:
cout<<"\nhangi sayiyi silsin ";
cin>>y;
list.deleete(y);
break;
case 3:
list.print();
break;
default:
cout<<" \nyanlis girdiniz \n" ;
}
}while (x!=-1);
return 0;
}