Beispiel #1
0
void NODE :: delete_val(int val)
{
	if(val == -1){
		cout << endl<<MSG_INVAL_VAL<<endl;
		return;
	}
	NODE *ptr,*preptr;
	for(ptr=First;ptr->data!=val;ptr=ptr->next);	//Search for the node with the value val
		preptr = ptr;		//Set a second pointer to the previous node after each iteration
	if(ptr==NULL)
	{
		cout << endl<<"Value not found."<<endl;
		return;
	}
	else
		cout << "The element "<<ptr->data<<" is successfully deleted.";
	if(ptr==First)
		delete_beg();
	else if(ptr==Last)
		delete_end();
	else{
		preptr->next = ptr->next;
		delete ptr;
		No_of_nodes--;
	}
}
Beispiel #2
0
void main()
{
	int opt;
	clrscr();
	do
	{
		clrscr();
		printf("\npress 1 for insert beg:");
		printf("\npress 2 for insert end:");
		printf("\npress 3 for insert after:");
		printf("\npress 4 for insert before:");

		printf("\npress 5 for delete beg:");
		printf("\npress 6 for delete end:");
		printf("\nlpress 7 for delete after :");
		printf("\npress 8 for delete before:");
		printf("\npress 9 for deleter that:");

		printf("\npress 10 for delete odd:");
		printf("\npress 11 for delete even:");

		printf("\npress 12 for sorting:");
		printf("\npress 13 for reverse:");

		printf("\npress 14 for insert in sorted order ;");
		printf("\npress 15 for display");
		printf("\nenter the option:");
		scanf("%d",&opt);
		switch(opt)
		{
			case 1:insert_beg(&ptr); break;
			case 2:insert_end(&ptr);break;
			case 3:insert_after(&ptr);break;
			case 4:insert_before(&ptr);break;

			case 5:delete_beg(&ptr); break;
			case 6:delete_end(&ptr);break;
			case 7:delete_after(&ptr);break;
			case 8:delete_before(&ptr);break;
		 /**/	case 9:delete_that(&ptr);break;

			case 10:delete_alter_odd(&ptr);break;
			case 11:delete_alter_even(&ptr);break;

			case 12:sort(&ptr);break;
			case 13:reverse(&ptr);break;
		/**/	case 14:insert_sort(&ptr);break;

			case 15:display(&ptr);break;
		}
		getch();
	}while(opt!=99);
	getch();
}
Beispiel #3
0
int main()
{
	create(5);
	insert_end(8);
	insert_begining(1);
	display();
	insert_end(9);
	display();
	delete_end();
	display();
}
void main ( )

   {
	 void create ();
	 void traverse ();
	 void delete_end ( );
	 clrscr ( );
	 create ( );
	 traverse( );
	 delete_end ( );
	  printf ("\n After deletion LAST node");
	 traverse( );
      getch ( );
    }
Beispiel #5
0
void test_delete(list_t *list)
{
	result_t delete_result;

	delete_result = delete_begin(list);
	assert(delete_result == SUCCESS && is_at_begining(list, 5) == FALSE);

	delete_result = delete_data(list, 50);
	assert(delete_result == SUCCESS && find(list, 50) == FALSE);

	delete_result = delete_data(list, 8);
	assert(delete_result == SUCCESS && find(list, 8) == FALSE);

	delete_result = delete_data(list, 80);
	assert(delete_result == ERROR);
	
	delete_result = delete_end(list);
	assert(delete_result == SUCCESS && is_at_end(list, 40) == FALSE);
}
void delete_pos(int pos)
{
    int i;
    struct node *ptr,*prev;
    prev=header;
    if(header==NULL)
        {
            printf("\nEmpty List");
            return;
        }
    if(pos==1)
        delete_beg();
    else
    {
        ptr=prev->link;
        for(i=1;i<pos-1;i++)
        {
            if(ptr!=NULL)
            {
                ptr=ptr->link;
                prev=prev->link;
            }
            else
            {
                printf("\nInvalid Position");
                getch();
                return;
            }
        }
        if(ptr->link==NULL)
            delete_end();
        else
        {
            prev->link=ptr->link;
            free(ptr);
        }
        printf("\nNode Deleted");
        getch();
    }
}
void main ( )
{
void create ( ), traverse ( ), insert_beg ( ), insert_end ( ),
delete_beg ( ), delete_end ( );
clrscr ( );
create ( );
printf ("\n Circular linklist is :\n");
traverse ( );
insert_beg ( );
printf ("\n Circular linklist after inserting in beginning is :\n");
traverse ( );
insert_end ( );
printf ("\n Circular linklist after inserting at end is :\n");
traverse ( );
delete_beg ( );
printf ("\n Circular linklist after deleting from beginning is :\n");
traverse ( );
delete_end ( );
printf ("\n Circular linklist after deleting from end is :\n");
traverse ( );
getch ( );
}
int main()
{
    int ch ;
    char choice;
    do
    {
        printf("Main menu:\n");
        printf("1 Create a list\n");
        printf("2 Delete the first node\n");
        printf("3 Delete an intermediate node\n");
        printf("4 Delete the last node\n");
        printf("Enter your choice\n");
        scanf("%d" , &ch);
        switch(ch)
        {
            case 1 : create();
                     break;
            case 2 : delete_beg();
                     break;
            case 3 : printf("Enter the value you want to delete : ");
                     scanf("%d" , &X);
                     delete_middle();
                     break;
            case 4 : delete_end();
                     break;
           default : printf("Wrong choice\n");
                     break;
        }
        printf("\nDo u want to continue..??\n");
        fflush(stdin);
        scanf("%c" , &choice);
        printf("\n");

    }while((choice=='y')||(choice=='Y'));

    display();
    return 0;
}
void main() {

    start =NULL;
    int choice,index,count,element,position,num_choice;
    do {

        printf("Enter your choice\n1.Insert at beginning\n2.Insert at end\n3.Delete from beginning\n");
        printf("\n4.Delete end\n5.Delete a given element\n6.Display");
        scanf("%d",&choice);

        switch(choice) {

            case 1:printf("Enter the element to be inserted: ");
                   scanf("%d",&element);
                   insert_begin(element);
                   break;
            case 2:printf("Enter the element to be inserted: ");
                   scanf("%d",&element);
                   insert_end(element);
                   break;
            case 3:delete_first();
                   break;
            case 4:delete_end();
                   break;
            case 5:printf("Enter the element to be deleted");
                   scanf("%d",&element);
                   delete_element(element);
                   break;
            case 6:display();
                   break;

        }
        printf("Do you want to continue 1.yes,2.no");
        scanf("%d",&num_choice);
    }while(num_choice==1);
}
Beispiel #10
0
int main(){
        List *list = (List*)0;
        int opt;
        int val;
        int k1,k2;
        do{     //loop
                printf("Select an operation:\n");
                printf("1) Create a list\t2) Insert at front\n3) Insert at end\t4) Delete first");
                printf("\n5) Delete last\t\t6) Display list\n7) Delete list\t\t8) Swap nodes\n9) Exit\n> ");
                scanf("%d",&opt);
                FLUSH_STDIN();
                switch (opt){
                        case 1: //Create list
                                if((create_list(list) == -1))
                                        fprintf(stderr,"\nError: List already created\n");
                                else    fprintf(stdout,"\nList successfully created\n");
                                break;

                        case 2: //Insert front
                                printf("Enter a value(integer) to insert: ");
                                scanf("%d",&val);
                                list = insert_front(list,val);
                                err_check();
                                break;

                        case 3: //Insert end
                                printf("Enter a value(integer) to insert: ");
                                scanf("%d",&val);
                                list = insert_end(list,val);
                                err_check();
                                break;

                        case 4: //Delete first
                                list = delete_front(list);
                                err_check();
                                break;

                        case 5: //Delete last
                                list = delete_end(list);
                                err_check();
                                break;

                        case 6: //Display list
                                if(display_list(list) <= 0)
                                        fprintf(stderr,"\nError: List is empty or not created. Cannot display.\n");
                                break;

                        case 7: //Delete the whole list
                                list = delete_list(list);
                                if(list_err == LIST_DELETED)
                                        fprintf(stderr,"\nThe list has already been deleted or not even created.\n");
                                else
                                        printf("\nList delete successfully.\n");
                                break;

                        case 8: //Swap
                                printf("Enter key values of two nodes to be swapped: ");
                                scanf("%d %d",&k1,&k2);
                                list = swap_nodes(list,k1,k2);
                                err_check();
                                break;

                        case 9: //Exit
                                break;

                        default:        //Invalid option
                                fprintf(stderr,"\nError: Invalid option\n");

                }//switch
        }while(opt != 9);

        return 0;
}
int main(void)
{
	node * head;
	head = NULL;
	printf("1.insert at begginng\n2.insert at end\n3.insert after kth node\n4.insert before kth node\n5.print\n6.delete_beg\n7.delete_end\n8.delete after kth\n9.delete before kth \n10.count_using_loop\n11.count_using_recusion\n12.end\n");
	int test;
	int end = 1;
	int num;
	int k;
	while(end) {
		scanf("%d", &test);
		switch(test) {
			case 1:
				scanf("%d", &num);
				head = insert_beg(head, num);
				break;
			case 2:
				scanf("%d", &num);
				head = insert_end(head, num);
				break;
			case 3:
				scanf("%d", &k);
				scanf("%d", &num);
				head = insert_aft_k(head, k, num);
				break;
			case 4:
				scanf("%d", &k);
				scanf("%d", &num);
				head = insert_bef_k(head, k, num);
				break;
			case 5:
				print(head);
				break;
			case 6:
				head = delete_beg(head);
				break;
			case 7:
				head = delete_end(head);
				break;
			case 8:
				scanf("%d", &k);
				head = delete_aft_k(head, k);
				break;
			case 9:
				scanf("%d", &k);
				head = delete_bef_k(head, k);
				break;
			case 10:
				num = count_loop(head);
				printf("%d\n", num);
				break;
			case 11:
				num = count_recur(head);
				printf("%d\n", num);
				break;
			case 12:
				end = 0;
				break;
		
		}
	}
	return 0;
}
int main()
{
    int choice,data,pos;
    header=NULL;

    while(1)
    {
        system("cls");
        printf("\nSelect your choice: ");
        printf("\n1. Create List ");
        printf("\n2. Insert Node ");
        printf("\n3. Display List ");
        printf("\n4. Delete Node ");
        printf("\n5. Arrange in Ascending order");
        printf("\n6. Exit\n");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1: create();
                    break;

            case 2: system("cls");
                    printf("\nSelect your choice: ");
                    printf("\n1. Insert at the End ");
                    printf("\n2. Insert at the Beginning ");
                    printf("\n3. Insert at a Specified Position ");
                    printf("\n4. Back to main menu\n");
                    scanf("%d",&choice);

                    switch(choice)
                    {
                    case 1: system("cls");
                            printf("\nEnter the data of the node: ");
                            scanf("%d",&data);
                            insert_end(data);
                            printf("\nNode inserted at the end of the list");
                            break;

                    case 2: system("cls");
                            printf("\nEnter the data of the node: ");
                            scanf("%d",&data);
                            insert_beg(data);
                            printf("\nNode inserted at the beginning of the list");
                            break;

                    case 3: system("cls");
                            printf("\nEnter the position at which you want to insert the node: ");
                            scanf("%d",&pos);
                            printf("\nEnter the data of the node: ");
                            scanf("%d",&data);
                            insert_pos(data,pos);
                            break;

                    case 4: break;

                    default: printf("Invalid choice!!");

                    }
                    break;

            case 3: display();
                    break;

            case 4: system("cls");
                    printf("\nSelect your choice: ");
                    printf("\n1. Delete from the End ");
                    printf("\n2. Delete from the Beginning ");
                    printf("\n3. Delete from a Specified Position ");
                    printf("\n4. Delete by the data of the node");
                    printf("\n5. Back to main menu\n");
                    scanf("%d",&choice);

                    switch(choice)
                    {
                    case 1: system("cls");
                            delete_end();
                            printf("\nNode deleted from the end of the list");
                            break;

                    case 2: system("cls");
                            delete_beg();
                            printf("\nNode deleted from the beginning of the list");
                            break;

                    case 3: system("cls");
                            printf("\nEnter the position at which you want to delete the node: ");
                            scanf("%d",&pos);
                            delete_pos(pos);
                            break;

                    case 4: system("cls");
                            printf("\nEnter the data which you want to delete: ");
                            scanf("%d",&data);
                            delete_val(data);
                            break;

                    case 5: break;

                    default: printf("Invalid choice!!");

                    }
                    break;

            case 5: ascend();
                    system("cls");
                    printf("\nList rearranged in ascending order");
                    getch();
                    break;

            case 6: exit(0);
                    break;

            default: printf("Invalid choice!!");
                     getch();

        }
    }
    return 0;
}