c - How to split a linked-list into two lists -


i'm writing code split circular linked-list 2 linked lists equal number of codes, following code:

#include <stdio.h> #include <stdlib.h>  typedef struct node *ptr; struct node {     int element;     ptr prev;     ptr next; }; typedef ptr list; typedef ptr position;  int main() {     list l=malloc(sizeof(struct node));     list first=malloc(sizeof(struct node));     list second=malloc(sizeof(struct node));     splitlist(l,first,second);     return 0; }  void splitlist(list l, list first,list second) {     position p,temp;     p=malloc(sizeof(struct node));     temp=malloc(sizeof(struct node));     p=l;     int count=0;      while ((p)->next != l) {         count++;     }      int c=count;     while (c!=(count/2)-1) {         p=(p)->next;        temp=(p)->next;     }      first=l;     (p)->next=null;     second=temp;      c=count;     while (c!=(count/2)-1) {        temp=(temp)->next;     }     (temp)->next=null; } 

when compiling code gives no errors i'm not sure if it's working properly.

in order more readable , maintainable code, first step improve code create functions manipulating lists. candidate functions are:

  • listinitialize()
  • listpushfront()
  • listpushback()
  • listpopfront()
  • listpopback()
  • listgetfirstnode()
  • listgetnextnode()
  • listgetfront()
  • listgetback()
  • listempty()
  • ...

with proper set of arguments , return values of course. can write splitlist function using basic list operation functions , code easier read , reason about.

also, in order handle empty list, should have list type not pointer node.

typedef struct node_tag { int value; struct node_tag *next; struct node_tag *prev } node, *nodeptr; typedef struct intlist_tag { nodeptr front; nodeptr back; } intlist;  // creates empty list. void listinitialize( intlist *plist ) { plist->front = null; plist->back = null; } void listpushfront( intlist *plist, int value )  { nodeptr newnode = malloc(sizeof(node));    if(null != newnode )    { newnode->next = plist->front;      newnode->prev = null; newnode->value = value;      plist->front = newnode;      if( plist->back == null ) plist->back = newnode; // first element...   } } // ... 

eventually, using functions, can write splitlist() function in concise , noise-free way:

void splitlist( intlist * source, intlist *target1, intlist *target2 ) {      intlist * currenttarget = target1;      for( nodeptr currentnode = listgetfirstnode(source); currentnode != null; currentnode = listgetnextnode(currentnode) )      {           listpushback(currenttarget, currentnode->value );           if(currenttarget == target1 ) currenttarget = target2;            else currenttarget = target1;      } } 

it might appear work create other list functions if want splitlist. in real world applications want other functions (or have them already). in homework situations, looks bit funny.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -