- Google+ Tools
-
Make Google+ profile picture
Make Google plus banners for profile
Create and share your Google Plus profile banners.
- This snippet gives a doubly linked list with an int part that is ordered ascendant or descendant
Ordered Double Linked List Using head and tail
#include <iostream>
using namespace std;
struct Node{ //Declaracion de la estructura
Node *next; //La direccion del siguiente
Node *prev; //La direccion del anterior
int data; //tipo de dato que contendra
};
void push(Node *&head, Node *&tail, int num){ //Declaracion de la funcion que insertara datos
Node *t=NULL, *t2=NULL, *t3=NULL;
t=new Node; //crea nuevos elementos tipo nodo
t2=head;
t3=tail;
t->data=num; //La parte entera del temporal sera igual al nuevo dato
t->next=NULL;
t->prev=NULL;
if(head==NULL || num<head->data){ // si el numero es menor al inicio o si el inicio es nulo
if(tail==NULL){ //si el final es nulo
head=tail=t; //el inicio y el final van a ser iguales al temporal
}
else{ //sino
head->prev=t;
t->next=head; //el temporal en su parte next va a ser igual al inicio
t->prev=NULL; //temporal en su parte prev va a ser igual a nulo ya que es el primero de la lista
head=t; //el incio va a ser igual al temporal
}
}else{
if(num>tail->data){ //si el dato es mayor al final de la lista
tail->next=t; //final en su parte siguiente apunta al temporal
t->prev=tail; //el temporal en su anterior apuntara al ultimo elemento
tail=t; //el temporal sera el ultimo elemento
}
else{
while(t->data>t2->next->data){ //mientras que el elemento sea mayor al otro seguira buscando donde meterse
t2=t2->next; //cambia de nodo en nodo comenzando en el inicio
}
t->next=t2->next; //la parte siguiente del temporal sera la siguiente del temporal 2
t2->next->prev=t; //La parte anterior del temporal sera el temporal 2
t2->next=t; //temporal 2 en su parte siguiente apuratara al temporal
t->prev=t2; //temporal 2 en su parte anterior apuntara al temporal
}
}
}
void display(Node *t){ //funcion que despliega desde el inicio
while(t!=NULL){ //miestras que el inicio sea diferente de nulo
cout<<t->data<<endl; // mostrara el dato que se encuentran en el nodo
t=t->next;//se movera de nodo
}
}
void display2(Node *t){//funcion que despliega desde la cola
while(t!=NULL){ //mienstras que la cola sea diferente de nula
cout<<t->data<<endl; //mostrara el dato
t=t->prev; // se movera de nodo
}
}
void pop(Node *&head, Node *&tail, int num){
Node *tmp=NULL, *temp=NULL, *t=NULL, *t2=NULL; //declaracion de los temporales
tmp=head; //tmp es igual al inicio
temp=tail;//temp es igual al fin
if(num==tmp->data){//si el numero es igual al dato del inicio
cout<<"The popped number is: "<<tmp->data<<endl;//muestra el dato
head=tmp->next; //el inicio se mueve a su proximo nodo
tmp->next->prev=NULL; // vuelve nula la parte previa del siguiente nodo para que el inicio en su parte previa apunte a nulo
delete tmp; //se elimina el nodo que se mostro
return;
}
if(num==temp->data){//si el numero es igual al dato del fin
cout<<"The popped number is: "<<temp->data<<endl;//muestra el dato
tail=temp->prev;//el fin se mueve al siguiente nodo
tail->next=NULL;//vuelve nula la parte siguiente del nodo anterior para que el fin en su parte siguiente sea nulo
delete temp;
return;
}
if(num==tmp->data && num == temp->data){//si es el ultimo elemento en la lista
cout<<"The last number in the list is: "<<temp->data<<endl;
cout<<"List is now empty"<<endl;
head=NULL;//vuelve nulo el inicio
tail=NULL;//vuelve nula la cola
delete temp; //borra el elemento
delete tmp;//borra el elemento
return;
}
while(tmp!=NULL){ // mientras que tmp sea diferente de nulo
if(tmp->data==num){ //si el dato en tmp es igual al numero
cout<<"The popped number is: "<<tmp->data<<endl;//muestra el dato
tmp->prev->next=tmp->next;//enlaza al elemento anterior al siguiente elemento de la lista
tmp->next->prev=tmp->prev;//enlaza el siguiente elemento al elemento anterior en la lista
delete tmp; //borra el elemento
return;
}
tmp=tmp->next;//va cambiando de nodo
}
cout<<"The number is not in the list"<<endl;
}
void destroy(Node *&head){
Node *tmp=NULL;
while(head!=NULL){
tmp=head->next;
head->next=NULL;
head->prev=NULL;
delete head;
head=tmp;
}
delete tmp;
cout<<"List is clear"<<endl;
}
void show(Node *&head, Node *&tail){//funcion para tener opciones de despliegue
char res='0';
while(res!='3'){
cout<<"How do you want to show the list? "<<endl;
cout<<"1. head to tail?"<<endl;
cout<<"2. tail to head?"<<endl;
cout<<"3. Continue"<<endl;
cout<<"Answer: ";
cin>>res;
if(res=='1'){
display(head);
}else{
if(res=='2'){
display2(tail);
}else{
if(res=='3')
cout<<"Next"<<endl;
else{
cout<<"Please enter a valid choice"<<endl;
show(head,tail);
}
}
}
}
}
int main(){
Node *head=NULL, *tail=NULL;
int num, ans;
char res, res2;
cout<<"Imput the number 0 to exit"<<endl;
while(true){
cout<<"Number: ";
cin>>num;
if(num==0)
break;
push(head,tail,num);
}
show(head, tail);
while(true){
cout<<"\nDo you want to remove an element from the list? y or n"<<endl;
cout<<"\nAnswer: ";
cin>>res;
if(res=='y'||res=='Y'){
cout<<"\nWhich number do you want to delete? "<<endl;
cout<<"\nAnswer: ";
cin>>ans;
pop(head, tail, ans);
show(head, tail);
}else{
if(res=='n'||res=='N'){
cout<<"\nDo you want to remove the list? y or n"<<endl;
cout<<"\nAnswer: ";
cin>>res2;
if(res2=='y'||res2=='Y'){
destroy(head);
break;
}
else
break;
}
}
}
system("pause");
return 0;
}
Comments
blog comments powered by Disqus