[ create a new paste ] login | about

Link: http://codepad.org/CEr5SYyh    [ raw code | output | fork ]

C, pasted on Oct 12:
#include <stdio.h>
#include <stdlib.h>

struct item { int data; struct item *next; };

struct item * mklist(int lo, int hi)
{
 if (lo > hi)
  return NULL;
 struct item * ls = (struct item *)malloc(sizeof (struct item));
 ls->data = lo;
 ls->next = mklist(lo+1, hi);
 return ls;
}

void showlist(struct item * ls)
{
 while (ls)
 {
  printf("%d ", ls->data);
  ls = ls->next;
 }
 printf("%s", "\n");
}

void delsevens(struct item * ls)
{
  struct item **e = &ls;
  while(*e) {
    if((*e)->data == 7) {
      *e = (*e)->next;
      continue;
    }

    e = &((*e)->next);
  }
}

int main (void)
{
 struct item * list = mklist(1,10);
 showlist(list);
 delsevens(list);
 showlist(list);
 return 0;
}


Output:
1
2
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 8 9 10 


Create a new paste based on this one


Comments: