Beispiel #1
0
main() {

	struct node* HEAD = NULL;
	createlinkedlist(&HEAD, 5);
	printlinkedlist(HEAD);
	
	f_InsertNth(&HEAD, 10, 2);
	printlinkedlist(HEAD);

	f_InsertNth(&HEAD, 11, 1);
	printlinkedlist(HEAD);

	printf("\n");
}
void walklist(node *head)
{
	node *slow = head->next;//使用快慢指针
	node *fast = head->next;
	node *preslow = slow;//slow用于处理节点个数奇偶数的问题

	if(fast->next == NULL)
	{
		return;
	}
	//fast = fast->next->next;
	while(fast->next != NULL)
	{
		fast = fast->next->next;
		preslow = slow;
		if(fast == NULL)
		{
			break;
		}
		slow = slow->next;
	}

	//if(fast != NULL)
	//{
		//printf("%d\n", slow->key);
		//printf("%d\n", slow->next->key);
	//}
	//else
	//{
		printf("%d\n", preslow->key);
		printf("%d\n", preslow->next->key);
	//}
	
	//node *tail = preslow;
	node *mid = NULL;
	if(fast != NULL)
	{
		mid = reverselist(slow);//奇数个节点,slow相当于头节点
	}
	else
	{
		mid = reverselist(preslow);//偶数个节点
	}
	printlinkedlist(mid);

	node *p = head->next;
	mid = mid->next;
	while(mid != NULL)
	{
		printf("%d-%d\n", p->key, mid->key);
		if(p->key != mid->key)
		{
			printf("NO\n");
			return;
		}
		p = p->next;
		mid = mid->next;
	}
	printf("YES\n");
}
main() {

	struct node* HEAD = NULL;
	
	createlinkedlist(&HEAD, 4);
	printlinkedlist(HEAD);

}
int main()
{
	int i;
	struct node *list = BuildListOfLength(4);
	printf("linked ");
	printlinkedlist(list);
	printf("has data %d at index 2.\n",getnth(list, 2));
}
Beispiel #5
0
void printlinkedlist(struct listnode *head)
{
	if(head == NULL){
		return;
	}
	printlinkedlist(head->next);
	printf(" %d", head->data);
	return;
}
int main( ) {
    
    unsigned long long int input;
    struct node *root1, *root2, *root3;
    
    input = 999;
    root1 = linkedList2numberForwards(input);
    printlinkedlist(root1);
    
    printf("\n");
    
    input = 1;
    root2 = linkedList2numberBackwards(input);
    printlinkedlist(root2);
    
    printf("\n");
    
    return 0;
}
int main()
{
	int data;
	struct node *list = BuildListOfLength(4);
	printf("linked ");
	printlinkedlist(list);
	data = Pop(&list);
	printf("poped the list and top node had data: %d and now list is ", data);
	printlinkedlist(list);
	data = Pop(&list);
        printf("poped the list and top node had data: %d and now list is ", data);
	printlinkedlist(list);
	data = Pop(&list);
        printf("poped the list and top node had data: %d and now list is ", data);
	printlinkedlist(list);
	data = Pop(&list);
        printf("poped the list and top node had data: %d and now list is ", data);
	printlinkedlist(list);
	printf("error will rise if tried to pop from NULL list\n");
	data = Pop(&list);
}
int main(int argc, char *argv[])
{
	node *head = (node *)malloc(sizeof(node));
	head->next = NULL;
	insert(head, 1);
	insert(head, 2);
	insert(head, 3);
	insert(head, 3);
	insert(head, 2);
	insert(head, 1);
	printlinkedlist(head);

	walklist(head);
	return 0;
}
Beispiel #9
0
int printPaths(struct node *binaryTree, struct listnode *listhead)
{
	if(binaryTree == NULL){
		printf("empty tree\n");
		return;
	}
	PushOnList(&listhead, binaryTree->data);
        if(binaryTree->left == NULL && binaryTree->right == NULL){
		printf("path :");
                printlinkedlist(listhead);
		printf("\n");
		return;
	}
	if(binaryTree->left != NULL){
		printPaths(binaryTree->left, listhead);
	}
	if(binaryTree->right != NULL){
		printPaths(binaryTree->right, listhead);
	}
        return;
}
int main()
{
	struct node* b =NULL;
	struct node* a = BuildListOfLength(5);
	printf("list ");
	printlinkedlist(a);
	FrontBackSplit(a, &a, &b);
	printf("splited to ");
	printlinkedlist(a);
	printf(",");
	printlinkedlist(b);
	printf("\n");

	a = BuildListOfLength(4);
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");

	a = BuildListOfLength(3);
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");

	a = BuildListOfLength(2);
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");

        a = BuildListOfLength(1);
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");

        a = NULL;
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");


}