Exemple #1
0
 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;
 }
Exemple #2
0
int main (int argc, char *argv[])
{
	char ch;
	int choice;
	int ret_val = 0;
	struct node *head = NULL;
	int *data = NULL;
	int value = 0;
	int pos = -1;

	while (1) {
		printf("\nMENU\n\n");
		printf("1. Insert at the front\n");
		printf("2. Insert at a position\n");
		printf("3. Delete from the front\n");
		printf("4. Delete from a position\n");
		printf("5. Display\n");
		printf("6. Reverse the linked list\n");
		printf("7. Pairwise Swap\n");
		printf("\nEnter your choice\n");
		scanf("%d", &choice);
		switch (choice)
		{
			case 1:
				printf("\nEnter a value\n");
				scanf(" %d", &value);
				data = get_new_node(&value);
				ret_val = insert_node(&head, (void *)data);
				if (ret_val)
					printf("\nOperation failed\n");
				break;
			case 2:
				printf("\nEnter the position at which to insert\n");
				scanf(" %d", &pos);
				printf("\nEnter a value\n");
				scanf(" %d", &value);
				data = get_new_node(&value);
				ret_val = insert_node_pos(&head, pos, (void *)data);
				if (ret_val)
					printf("\nOperation failed\n");
				break;
			case 3:
				ret_val = delete_node(&head);
				if (ret_val)
					printf("\nOperation failed\n");
				break;
			case 4:
				printf("\nEnter the position at which to delete\n");
                                scanf(" %d", &pos);
                                ret_val = delete_node_pos(&head, pos);
                                if (ret_val)
                                        printf("\nOperation failed\n");
				break;
			case 5:
				print_list(head);
				break;
			case 6:
				head = reverse(head);	
				break;
			case 7:
				pairwise_swap(&head);
				break;
			default:
				printf("\nWrong choice. Doing nothing\n");
		}
		printf("Do you want to continue (y/n) ??");
		scanf(" %c", &ch);
		if (ch == 'n' || ch == 'N') {
			printf("\nBreaking the driver loop\n");
			break;
		}
	}
	return 0;
}