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();
}
int pop()
{
	int ret=delete_beg(&head);
	if(ret==-1)
	{
		printf("Empty.\n");
		return -1;
	}
	printf("Popped element : %d\n",ret);
	return 0;
}
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;
}
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;
}