c - Sorting linked list -


i have slight problem sorting in linked lists. have doubly linked list needs sorting after intital input, swap function intend use, problem herein lies screws header pointers there may be. cannot sort list jusr replacing data there many variables , can change in course of development (new variables can added or deleted)

task *taskheader;  struct task{    task *pprev;    //large number of variables    int number;    task *pnext;    task(){      pprev = null;      pnext = null;    } } //swap function void swap(task *task2, task *task3)    task* task1 = task2->prev; // alias readability    task* task4 = task3->next; // alias readability    if(task1!=null) task1->next = task3;    task3->prev = task1;    task3->next = task2;    task2->prev = task3;    task2->next = task4;    if(task4!=null) task4->prev = task2; }  void sort(task *taskheader){    task *temp = taskheader;    while(temp != null){       if(temp->number < temp->pnext->number) swap(temp, temp->pnext);     } } 

how should append swap function keep headers intact , not crash when swapping start or end task nodes? adding null checks swap function keep function going out of bounds , ending pointer faults, there better way solve this?

should traverse linked list til reach point pprev == null , change header point towards node?

so have double linked list , , hard sort.if want an orderded list. maybe need write functions:

a init function , create head node , store address global variable.

ainsert function, insert node right place.

and , ordered ,but still need other functions make list useful.

a delete function.

a find function

a size function.

a empty function.

a 'get' function

...

good luck.

after command :

insert :

   node1      ||    node2     ||           node3 

if need insert new node list, behind node1.

 1. create new node node4               2. find node1                      node1  2. node1->next->pre = node4             |  3. node4->next = node1->next    -->     |node4                                          | ||                                         node2                                          ||                                         node3   4.node4->pre = node1           5.node1->next = node4           -->    node1                                          ||                                         node4                                          ||                                         node2                                          ||                                         node3 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -