| | |
Link List Move Method
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2005
Posts: 42
Reputation:
Solved Threads: 0
Below I have attached 4 methods from my linked list class.
1) find 2)remove 3)retrieve 4) Insert.
what I'm trying to do is write code that will let me MOVE the position of a node to another position in the linked list.
Anyone got a clue??
regards
Tim
1) find 2)remove 3)retrieve 4) Insert.
what I'm trying to do is write code that will let me MOVE the position of a node to another position in the linked list.
Anyone got a clue??
regards
Tim
C Syntax (Toggle Plain Text)
SeqItemType Sequence::retrieve(int index) { if ((index < 1) || (index > size())){ // can't delete something that isn't there } ListNode *cur = find(index); return cur->item; } void Sequence::insert(int index, SeqItemType newItem) { if ((index < 1) || (index > (size()+1))){ // if index is out of bounds don't do anything } else { // otherwise create new node and place newItem in it ListNode *newPtr = new ListNode(); // increment the size attribute length++; // store the data item in the new node newPtr->item = newItem; // if the index is 1, insert new node at beginning of list if (index == 1) { newPtr->next = head; head = newPtr; } else { ListNode *prev = find(index-1); newPtr->next = prev->next; prev->next = newPtr; } } } void Sequence::remove(int index) { ListNode *cur; if ((index < 1) || (index > size())){ } else { // remove the first node in the list if (index == 1) { cur = head; head = head->next; } else{ ListNode *prev = find(index-1); cur = prev->next; prev->next = cur->next; } // clean things up! length--; cur->next = NULL; delete cur; } } ListNode *Sequence::find(int index){ if ((index <1) || (index >size())){ return 0; } else{ ListNode *cur = head; for(int skip =1; skip<index; ++skip) cur=cur->next; return cur; } }
Last edited by Dave Sinkula; Dec 15th, 2005 at 12:09 am. Reason: Added [code][/code] tags and removed Red coloring.
What is wrong with using the functions you already got
C Syntax (Toggle Plain Text)
void Sequence::move(int from, int to) { SeqItemType item = find( from )->item ; remove(from) ; insert( to, item ) ; }
/pern.*/i
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
![]() |
Similar Threads
- Link List Insert problems (C++)
- how to reverse link list using recurtion (C)
- Using a class to add/delete/show numbers in a Link List (C++)
- circular link list (C)
Other Threads in the C Forum
- Previous Thread: initializing a hash_set ?
- Next Thread: Linked List Retrieve Method
Views: 1493 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32 win32api windows.h





