Eliminar el primer nodo
Haga que el puntero de la cabeza apunte al siguiente nodo
Eliminar el enésimo nodo
Atraviese el nodo que debe eliminarse mientras mantiene un puntero al nodo anterior y luego haga que el nodo anterior apunte al nodo después del nodo que debe eliminarse
Eliminar el nodo final
Atraviese el final del nodo manteniendo un puntero al nodo anterior y haga que el último segundo puntero apunte a NULL
- ¿Soy solo yo o el algoritmo recursivo de Fibonacci es brillantemente complejo?
- ¿Por qué prácticamente todos los algoritmos de ascensor son tan ineficientes y cuáles son las razones por las que aún no se han optimizado?
- ¿Por qué los algoritmos tienen tanta importancia en la programación?
- ¿Cuál es el promotor y algoritmo SEO más importante en 2017?
- ¿Cuál es la diferencia entre el algoritmo memético y el algoritmo genético?
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}
#include
#include
using namespace std; struct node
{
int data;
struct node *next;
}; void display(struct node *head)
{
while((head)!=0)
{
printf("%d->",(head)->data);
(head)=(head)->next;
}
printf("\n");
} void insert(struct node **head,int x)
{
struct node *newnode=new node[sizeof(struct node)];
newnode->data=x;
newnode->next=0; if(*head==0)
{
*head=newnode;
}
else
{
newnode->next=*head;
*head=newnode;
}
} void remove_head(struct node **head)
{
if(*head == 0)
printf("Empty list\n"); struct node *temp = *head;
*head = (*head) -> next;
delete temp; } void remove_end(struct node **head)
{
struct node *itr=(*head)->next;
struct node *prev=*head; while(itr->next != 0)
{
prev = itr;
itr = itr -> next;
} prev->next = 0;
delete itr;
} void remove(struct node **head, int x)
{
if(x == 1)
{
struct node *temp = *head;
*head = (*head) -> next;
delete temp;
return;
} struct node *itr = (*head)->next;
int pos=2;
struct node* prev = *head; while(pos < x && itr != 0)
{
prev=itr;
itr=itr->next;
pos++;
}
if(pos!=x)
{
printf("Invalid Index\n");
return;
}
prev->next = itr->next;
delete itr;
} int main()
{
struct node *head=0;
for(int i=0;i<5;i++)
{
insert(&head,i);
} display(head); remove_head(&head);
display(head); remove(&head,2);
display(head); remove_end(&head);
display(head);
return 0;
}