c - change the nodes of a linked list -


the code list nodes is:

struct list{                                             int value;                                           struct list *next;                                }; 

i want make swap function this:

void swap(struct list *head , int v) 

user gives number v , program search in list , changes next node. example if user gives 3 , list contains :2 -1 7 3 -5 4 swap function make list :2 -1 7 -5 3 4 ideas ?

i made following code swap :

void swap(struct list *head, int v){        struct list *before=null;        struct list *found=null;        struct list *after=null;   if(head==null){    printf("case of empty list !\n); }  before=head; found=head; while(found->next !=null){        if (found->value==v){                 after = before->next;                 before = found->next;         }         before = found;         found = found->next;         after = found->next; } return; } 

try way :

  1. search int v in linked list till last node

  2. if found , if node not last node swap data of node.

  3. if last node can't swap. have find case

  4. if node node have consider condition if last node

if want swap node try code

void swap(node *head, int v) {  node * prev,*curr,*next,*temp  curr=head;  prev=curr;  next=curr->next;  while(curr!=null){  if(curr->data==v){    if(curr->next!=null){    prev->next=next;    temp=next->next;    next->next=curr;    curr->next=temp;     break;     }    else{      printf("\nthere no further node swap ");     }  }    prev = curr;  curr = curr->next;  next = curr->next;  }   }     

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -