Created
April 26, 2012 02:44
-
-
Save goctave/2495318 to your computer and use it in GitHub Desktop.
CareerCup_2.3@1point3acres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
struct node | |
{ | |
int data; | |
node *next; | |
node(): data(0), next(NULL){} | |
}; | |
void del_node(node *&head, node *p) | |
{ | |
if(p == NULL || head == NULL) | |
return; | |
if(p == head) | |
{ | |
head = head->next; | |
delete p; | |
} | |
else | |
{ | |
node *t = p->next; | |
p->data = t->data; | |
p->next = t->next; | |
delete t; | |
} | |
} | |
void insert(node *&head, int d) | |
{ | |
node *p = new node; | |
p->data = d; | |
if(head == NULL) | |
{ | |
head = p; | |
} | |
else | |
{ | |
node *tmp = head; | |
while(tmp->next) | |
tmp = tmp->next; | |
tmp->next = p; | |
} | |
} | |
int main() | |
{ | |
node *head = NULL; | |
for(int i = 1; i <= 10; i++) | |
insert(head, i); | |
node *tmp = head; | |
while(tmp->data != 4) | |
tmp = tmp->next; | |
del_node(head, tmp); | |
tmp = head; | |
while(tmp) | |
{ | |
cout << tmp->data << " "; | |
tmp = tmp->next; | |
} | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment