1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include <iostream>
#include <stdlib.h>
using namespace std;
int opcion;
struct nodo{
string info;
string name;
nodo *next;
nodo *prev;
};
void AddtoEnd(nodo **head, nodo **last);
void AddtoStart(nodo **head, nodo **last);
void AddtoStart2(nodo **head, nodo **last);
void AddAfter(nodo **head, nodo **last); //despuesde
void AddBefore(nodo **head, nodo **last);
void show(nodo *head);
void showx(nodo *last);
void EliminarFinal(nodo **head, nodo **last);
void AddAfter(nodo **head, nodo **last);
int main()
{
nodo *c=NULL,*f=NULL; //head pointer and last/end pointer
AddtoStart(&c, &f);
AddtoStart(&c, &f);
AddtoStart(&c, &f);
show(c);
AddAfter(&c, &f);
AddAfter(&c, &f);
show(c);
}
void AddtoStart(nodo **head, nodo **last){
nodo *A;
string name;
cout<<"Add name to Start"<<endl;
cin>>name;
if((*head)==NULL){
*head = new nodo;//
(*head)->info =name; //
(*head)->next=NULL;//
(*head)->prev=NULL;
(*last)=(*head); //
}
else{
A = new nodo;
A->info = name;
A->next = *head;
(*head) = A;
}
}
void AddtoStart2(nodo **head, nodo **last){
//nodo *A;
nodo *B;
string name;
cout<<"Add name to Star(no last pointer)"<<endl;
cin>>name;
if((*head)==NULL){
*head = new nodo;//assing nodo direction to head
(*head)->info =name; //store name in nodo
(*head)->next=NULL; //
(*last)=(*head); // start and end are the same
}
else{
B = new nodo;
B->info = name;
B->next = *head;
B->prev = NULL;
(*head) = B;
}
}
void AddAfter(nodo **head, nodo **last){
int position, i=2;
string name;
nodo *A,*B;
A=new nodo;
B=NULL;
cout<<"\nAdd name (after): ";
cin>>name;
A->info=name;
A->next=NULL;
A->prev=NULL;
cout<<"Add name after position #: ";
cin>>position;
if(position==1){
if((head)==NULL){
(*head)=A;
(*last)=A;
}
else {
A->next=(*head)->next;
A->prev=(*head);
(*head)->next->prev=A;
(*head)->next=A;
}
}
else
{
B=*head;
while (i<=position){
B=B->next;
i++;
}
A->next=B->next;
A->prev=B;
B->next->prev=A;
B->next=A;
}
}
void show(nodo *head)
{
cout<<"Elements in the list"<<endl;
nodo* temp;
temp=head;
while (temp!= NULL)
{
cout<<temp->info<<" ";
temp=temp->next;
}
}
void showx(nodo *last)
{
cout<<"Elements in the list"<<endl;
nodo* temp;
temp=last;
while (temp!= NULL)
{
cout<<temp->info<<" ";
temp=temp->prev;
}
}
|