| | |
"doubly linked list" question
![]() |
•
•
Join Date: Sep 2005
Posts: 14
Reputation:
Solved Threads: 0
I did a "single linked list" header & Implementation & Main files & everything worked very good. But now i must Implementation for the "doubly linked list" & i just need to know what i should add to my functions so they will work good, should i change all the functions Implementation ???
i have these function,
addToHead(int)
addToTail(int)
deleteFromHead()
deleteFromTail()
deleteNode(int)
isInList(int)
printForward()
printBackward()
clear()
anyone can help, plz ??? :!:
i have these function,
addToHead(int)
addToTail(int)
deleteFromHead()
deleteFromTail()
deleteNode(int)
isInList(int)
printForward()
printBackward()
clear()
anyone can help, plz ??? :!:
•
•
Join Date: Sep 2005
Posts: 14
Reputation:
Solved Threads: 0
This is what I wrote:
<< moderator edit: added
C Syntax (Toggle Plain Text)
doublyLinkedList::doublyLinkedList() { head = tail = 0; } doublyLinkedList::~doublyLinkedList() { node *temp; while (head != 0) { head->prev = 0; temp = head->next; delete head; head = temp; } } void doublyLinkedList::addToHead(int item) { node *temp; temp = new node; temp->info = item; temp->next = head; temp->prev = 0; head = temp; if (tail == 0) tail = head; } void doublyLinkedList::addToTail(int item) { node *temp; temp = new node; temp->info = item; temp->prev = tail; temp->next = 0; tail = temp; if (head == 0) head = tail; } void doublyLinkedList::deleteFromHead() { if(head != 0) { int item = head->info; node *temp = head; if(head == tail) head = tail = 0; else head = head->next; delete temp; cout << ">> " << item << " has been deleted from head"; } else cout << "** The list is empty"; } void doublyLinkedList::deleteFromTail() { if(tail != 0) { int item = tail->info; node *temp = tail; if(tail == head) tail = head = 0; else tail = tail->prev; delete temp; cout << ">> " << item << " has been deleted from tail"; } else cout << "** The list is empty"; } void doublyLinkedList::deleteNode(int item) { if(head != 0) { if(item == head->info) { cout <<"\n"; deleteFromHead(); } else { node *temp = head->next, *pred = head; while(temp != 0 && !(temp->info == item)) pred = pred->next, temp = temp->next; if(temp != 0) { pred->next = temp->next; if(temp == tail) tail = pred; delete temp; cout << "\n>> " << item << " has been deleted"; } else cout << "\n>> " << item << " is not in the list"; } } else cout << "\n** The list is empty"; } bool doublyLinkedList::isInList(int item) { node *temp; temp = head; while (temp != 0) { if(temp->info == item) return true; temp = temp->next; } return false; } void doublyLinkedList::printForward() const { node *temp = head; cout << "{HEAD}---> | "; while(temp != 0) { cout << temp->info << " | "; temp = temp->next; } cout << " <---{TAIL}"; } void doublyLinkedList::printBackward() const { node *temp = tail; cout << "{HEAD}---> | "; while(temp != 0) { cout << temp->info << " | "; temp = temp->prev; } cout << " <---{TAIL}"; } void doublyLinkedList::clear() { while(head != 0) { cout << "\n"; deleteFromHead(); } cout << "\n"; }
[code][/code] tags >> ![]() |
Similar Threads
- Doubly Linked List Problem (C++)
- doubly linked list--- help needed1 (C++)
- To create contact manager using doubly linked list(c++) (C++)
- How Can I Sort a Doubly Linked List Queue? (C)
- Need Help to Print Doubly Linked List(DLL) (Java)
- doubly linked list implementation (Java)
Other Threads in the C Forum
- Previous Thread: make it work
- Next Thread: Bluetooth Socket Api Help
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





