예제 #1
0
void poly_add(ListHeader *plist1, ListHeader *plist2, ListHeader *plist3 )
{
 ListNode *a = plist1->head;
 ListNode *b = plist2->head;
 int sum;
 while(a && b){
   if( a->expon == b->expon ){
	sum = a->coef + b-> coef;
    if( sum != 0 ) insert_node_last(plist3, sum, a->expon);
	a=a->link; b=b->link; 
   }
   else if( a->expon > b->expon ){
    insert_node_last(plist3, a->coef, a->expon);
	a=a->link;
   }
   else {
	insert_node_last(plist3, b->coef, b->expon);
	b=b->link; 
   }
 }
  // a나 b중의 하나가 먼저 끝나게 되면 남아있는 항들을 모두 
  // 결과 다항식으로 복사
 for( ; a != NULL; a=a->link) 
   insert_node_last(plist3, a->coef, a->expon);
 for( ; b != NULL; b=b->link) 
   insert_node_last(plist3, b->coef, b->expon);
}
예제 #2
0
main()
{
	ListHeader list1, list2, list3;
		
	init(&list1);
	init(&list2);
	init(&list3);


	// 다항식 1을 생성 
	insert_node_last(&list1, 3,12);
	insert_node_last(&list1, 2,8);
	insert_node_last(&list1, 1,0);

	// 다항식 2를 생성 
	insert_node_last(&list2, 8,12);
	insert_node_last(&list2, -3,10);
	insert_node_last(&list2, 10,6);

	// 다항식 3 = 다항식 1 + 다항식 2
	poly_add(&list1, &list2, &list3);
	poly_print(&list3);
}
예제 #3
0
int main()
{
	printf("\nEnter the nodes in the linked list\n");
	int data, nos, period, lambda = 1, mu = 1; char op = 'Y';
	while( ( op == 'Y' || op == 'y' ) && flag != 1 )
	{
		printf("\nEnter the data for the new node\n");
		scanf("%d",&data);
		insert_node_last(&head,&sentinel,data);
		if ( flag != 1 ) 
		{	printf("\nDo you want to insert more data ( Y or N )\n");
			scanf("%c",&op);
			scanf("%c",&op);
		}
	}

	struct node *hare, *tortoise;
	hare = ( tortoise = head ) ;
	
	//#TODO In case of No loops or 0 nodes => This shouldn't run .Error check for NULL pointer . May cause segmentation fault.
	tortoise = tortoise ->next;
	hare = hare->next->next;
	//#TODO In case of No loops or 0 nodes => This shouldn't run .Error check for NULL pointer . May cause segmentation fault.
	nos = 1;

	while ( hare != tortoise && hare != NULL )
	{
		/* They should meet at a point somewhere inside the loop 
		 * When they meet the nos shhould be a multiple of the length
		 * of the loop i.e nos = k*l where l is the length of the loop. */
		
		//#TODO In case of No loops or 0 nodes => This shouldn't run. Error check for NULL pointer . May cause segmentation fault.
		tortoise = tortoise->next;
		hare = hare->next->next;
		//#TODO In case of No loops or 0 nodes => This shouldn't run .Error check for NULL pointer . May cause segmentation fault.
		nos++;
	}

	if ( hare == NULL ) /* No loop exists in the linked list */
	{
		printf("\n :: No loop exists in the list :: \n");
		return 0;
	}
	
	if ( hare == tortoise ) /* A loop exists */
	{
		period = nos;
	}
	/* Now starting from the head of the list, we need to find the first element in the list which repeats itself
	 * i.e precisely the start node of the loop */
	//#TODO In case of No loops or 0 nodes => This shouldn't run. Error check for NULL pointer . May cause segmentation fault.
	tortoise = head;
	//#TODO In case of No loops or 0 nodes => This shouldn't run. Error check for NULL pointer . May cause segmentation fault.
	while ( tortoise != inc_steps(tortoise,period) )
	{
		tortoise = tortoise->next;
		mu++;
	}
        if ( tortoise == inc_steps(tortoise,period) )
		printf("\n :: The start node of the loop is at node no. %d :: \n",mu);
	/* Now we will find the length of the loop by keeping the tortoise fixed and moving the hair
	 * one step each until it circles around and meets the tortoise at the start node of the loop */
	
	//#TODO In case of No loops or 0 nodes => This shouldn't run. Error check for NULL pointer . May cause segmentation fault.
	hare = tortoise->next;
	//#TODO In case of No loops or 0 nodes => This shouldn't run. Error check for NULL pointer . May cause segmentation fault.
	
	while ( hare != tortoise )
	{
		hare = hare->next;
		lambda++;
	}
	if ( hare == tortoise )
	{
		printf("\n :: The length of the loop is %d :: \n ",lambda);
	}
	return 0;
}
예제 #4
0
파일: Linked list.cpp 프로젝트: joyjitc/lab
 int main()
 {
    int ch;
    char ans = 'Y';

    while (ans == 'Y'||ans == 'y')
    {
        printf("\n---------------------------------\n");
        printf("\nOperations on singly linked list\n");
        printf("\n---------------------------------\n");
        printf("\n1.Insert node at first");
        printf("\n2.Insert node at last");
        printf("\n3.Insert node at position");
        printf("\n4.Sorted Linked List in Ascending Order");
        printf("\n5.Delete Node from any Position");
        printf("\n6.Update Node Value");
        printf("\n7.Search Element in the linked list");
        printf("\n8.Display List from Beginning to end");
        printf("\n9.Display List from end using Recursion");
        printf("\n10.Exit\n");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\nEnter your choice");
        scanf("%d", &ch);

        switch (ch)
        {
        case 1:
            printf("\n...Inserting node at first...\n");
            insert_node_first();
            break;
        case 2:
            printf("\n...Inserting node at last...\n");
            insert_node_last();
            break;
        case 3:
            printf("\n...Inserting node at position...\n");
            insert_node_pos();
            break;
        case 4:
            printf("\n...Sorted Linked List in Ascending Order...\n");
            sorted_ascend();
            break;
        case 5:
            printf("\n...Deleting Node from any Position...\n");
            delete_pos();
            break;
        case 6:
            printf("\n...Updating Node Value...\n");
            update_val();
            break;
        case 7:
            printf("\n...Searching Element in the List...\n");
            search();
            break;
        case 8:
            printf("\n...Displaying List From Beginning to End...\n");
            display();
            break;
        case 9:
            printf("\n...Displaying List From End using Recursion...\n");
            rev_display(first);
            break;
        case 10:
            printf("\n...Exiting...\n");
            return 0;
            break;
        default:
            printf("\n...Invalid Choice...\n");
            break;
        }
        printf("\nYOU WANT TO CONTINUE (Y/N)");
        scanf(" %c", &ans);
    }
    return 0;
 }