Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct Node {
- int data;
- Node *next;
- Node *prev;
- };
- typedef struct DLinkedList *List;
- struct DLinkedList {
- Node *first;
- };
- void AppendEnd( int v );
- bool Search( int value );
- void Delete( int value );
- List list;
- // test
- int main()
- {
- AppendEnd(4);
- AppendEnd(6);
- if(Search(4)) cout << "true";
- else cout << "false";
- if(Search(6)) cout << "\ttrue\n";
- Delete(6);
- if(Search(6)) cout << "true";
- return 0;
- }
- // Gan gia tri value vao cuoi cua linked-list
- void AppendEnd( int value ) {
- Node *node = new Node;
- node->data = value;
- node->next = NULL;
- node->prev = NULL;
- if( list == NULL ) {
- list = new DLinkedList;
- list->first = node;
- }
- else {
- Node *curr = list->first;
- while( curr->next != NULL )
- curr = curr->next;
- curr->next = node;
- node->prev = curr;
- }
- }
- // tim xem trong linked list co gia tri value hay ko
- bool Search( int value ) {
- Node *curr = list->first;
- while( curr != NULL ) {
- if( curr->data == value )
- return true;
- curr = curr->next;
- }
- return false;
- }
- // Xoa element dau tien co gia tri value trong list
- void Delete( int value ) {
- Node *curr = list->first;
- while( curr != NULL ) {
- if( curr->data == value ) {
- curr->next->prev = curr->prev;
- curr->prev->next = curr->next;
- delete curr;
- return;
- }
- curr = curr->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement