Skip to content

Instantly share code, notes, and snippets.

@liyu1981
Created October 29, 2013 13:29
Show Gist options
  • Save liyu1981/7214638 to your computer and use it in GitHub Desktop.
Save liyu1981/7214638 to your computer and use it in GitHub Desktop.
greenplum test
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
node * label(node *list);
node * reverse(node *list);
node * reverseN(int N, node *list);
/* for test */
void printList(node *list);
node*
label(node *list)
{
node *head = list;
node *p;
int i = 1;
if (head == NULL)
return head;
for (p = head; p != NULL; ) {
p->data = i;
i += 1;
p = p->next;
}
return head;
}
node*
reverse(node *list)
{
node *p;
node *tmp;
node *head = list;
if (head == NULL || head->next == NULL)
return head;
p = head->next;
head->next = NULL;
while(p != NULL) {
tmp = p->next;
p->next = head;
head = p;
p = tmp;
}
return head;
}
node*
reverseN(int N, node *list)
{
node *bp, *ep;
node *head = list;
node *tmp;
int i;
node *retbp, *retep;
if (head == NULL || head->next == NULL)
return head;
if (N <= 1) {
return list;
}
retbp = NULL;
retep = NULL;
while(1) {
bp = head;
if (bp->next != NULL) {
ep = bp->next;
i = 2;
while(1) {
if (i>=N || ep->next == NULL)
break;
ep = ep->next;
i += 1;
}
head = ep->next;
ep->next = NULL;
}
else {
head = NULL;
ep = bp;
}
tmp = reverse(bp);
ep = bp;
bp = tmp;
if (retbp == NULL) {
retbp = bp;
retep = ep;
}
else {
retep->next = bp;
retep = ep;
}
if (head == NULL)
break;
}
return retbp;
}
void printList(node *list)
{
node *p = list;
while (p!=NULL) {
printf("%d->", p->data);
p = p->next;
}
printf("NULL\n");
}
int main(int argc, char *argv[])
{
node n1, n2, n3, n4, n5, n6, n7, n8;
node *l;
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = &n8;
n8.next = NULL;
label(&n1);
printList(&n1);
l = reverseN(1, &n1); /* implict test reverse() */
printList(l);
l = reverseN(2, NULL);
printList(l);
n1.next = NULL;
l = reverseN(2, &n1);
printList(l);
n1.next = &n2;
n2.next = NULL;
l = reverseN(2, &n1);
printList(l);
}
@liyu1981
Copy link
Author

Use this data structure which defines an element in a singly-linked list

typedef struct node
{
int data;
struct node *next;
} node;

to solve the following coding problems in C or C++:
#1 Write a function with signature

node *label(node *list)

which assigns labels 1 through N to the nodes of the list and returns the labeled list, i.e the head of the list is assigned value 1 the last element value N, N being the number of elements in the list.
#2 Write a function with signature

node *reverse(node *list)

which reverses the list.
#3 Write a function with signature

node *reverseN(int N, node *list)

which reverses sublists of size N.

Examples:

if the list is labeled 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8, the result of
reverseN(3, list) will be 3 -> 2 ->1 -> 6 -> 5 -> 4 -> 8 -> 7;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment