Exemple #1
0
int main(int argc, char* argv[]){

   // varible declarations
  int i=0,j=0,length=0,*array=NULL, temp=0,key=0,found=0;
  double arrayTime, nodeTime;
  Node* head=NULL;
  clock_t start,end;
  
  key = atoi(argv[1]);// change string to integer
  printf("%-15s%-15s\n","Linear search","Node Search");
 
  //loop for all the argumens
  for(i=2; i<argc; i++){
    FILE* input = fopen(argv[i],"r");
    if(input == NULL){
      printf("Unable to open %s\n",argv[i]);
    }

    else{
      fscanf(input,"%d",&length);
      array = (int *)malloc(length*sizeof(int));
      for(j=0;j<length;j++){
        fscanf(input,"%d",&temp);
        array[j] = temp;
        head = insert_end(head, temp);
      }
      //timing the algorith
      start = clock();
      found = linearSearch(array,length,key);
      end = clock();
      arrayTime = (double)(end-start)/CLOCKS_PER_SEC;
      //check if the search was found 
      if(found == 0){
        printf("number not found in linear search\n");
      }
      start = clock();
      found = nodeSearch(head, key);
      end = clock();
      nodeTime = (double)(end-start)/CLOCKS_PER_SEC;
      if(found == 0){
        printf("number not found in node search\n");
      }
      
    
      printf("%-15lf%-15lf\n",arrayTime,nodeTime);
      //free the malloced memory 
      free(array);
      free_node(head);
      head = NULL;
    }
    close(input);//close the input file
  }
 

}
mn* start(mn *m)
{ 
    stack *top;
    node *trie;
    ll *temp;
    char dump,c;
	int ch;
    top = NULL;
    trie = NULL;
    temp = NULL;
    printw("\n\t\t\t\t\t\tprint 1 to all the populate data\n");
    scanw("%d",&ch);
    refresh();
    printw("\t\t\t\t\t\t\t%d thanks",ch);
    if(ch == 1)
        trie = pTrain(trie);
    while(ch != 27)
    {
        
        ch = getch();
		c = ch;

        switch(ch)
		{
			case 127:
        	{	   
				m = delete_node(m);
				clear();
                refresh();
                traverse(m);
				break;
			}
			
			case 9:
			{        
			    get_key(m,top,trie);
				break;
			}		
			default:
			{    
				m = insert_end(m,c);
				clear();
				refresh();
				traverse(m);    
			}
		}
	}
		
		clear();
		refresh();
        printw("press any key to exit");
        refresh();
	
}
Exemple #3
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();
}
void get_key(mn *m, stack *top, node *trie)
{
	ll *temp;
    top = NULL;
	char c,ch,key[20],buffer[20];
	int i=0;
    temp = m->end;
	while((temp != NULL)&&(temp->a != ' ')) 
	{
			
			key[i] = temp->a;
			temp = temp->prev;
			i++;
		
	}
	for(i=0;i<strlen(key);i++)
		{
			if(isalpha(key[i]))
				{
					buffer[i] = key[i];
				}
		}
	strcpy(buffer,key);
	strrev(buffer);
	for(i=0;i<20;i++)
      key[i]=0;   	
	strcpy(key,buffer);
    top = Complete(trie,key,top); //stack is build till this point
    if(top)
        //printw("ok1");
        top = pop(top,m,temp); //pop
        
        c = getch();
    if(c==9)
    {
        while(c==9)
        {
            top = pop(top,m,temp);//pop
            c = getch();
            
        }
    }

    else
    {
        ch = c;
        m = insert_end(m,ch);
        clear();
        refresh();
        traverse(m);
    }
}	
Exemple #5
0
//------------------------------------------------------------------
// Insert many nodes consecutively on either side. 1 front, 0 end
// Returns 0 if successful; -1 otherwise.
//------------------------------------------------------------------
int insert_many(struct node **head, char **str, int cnt, bool side){
 int i;

 if(side){
   for( i = 0; i < cnt; i++ )
     if((insert_front(head, str[i])) == -1)
       return -1;
 }
 else{
   for( i = 0; i < cnt; i++ )
     if((insert_end(head, str[i])) == -1)
       return -1;
 }
 return 0;
}
int main(void)
{
	int ch,item,pos;	
	while(1)
	{
		printf("enter ur choice;\n");
		printf("\n1:insert the number in the front\n2:insert the element in the end\n3:append in the mid\n4:display the elements\n");
		printf("5:delet the node w.r.t pos\n6:delet the node with respect to data\n7:exit\n");
		scanf("%d",&ch);
		switch(ch)
		{
		case 1:
			printf("enter the number\n");
			scanf("%d",&item);
			insert_front(item);
			break;
		case 2:
			printf("enter the number:\n");
			scanf("%d",&item);
			insert_end(item);
			break;
		case 3:
			printf("enter the number and the postision: \n");
			scanf("%d %d",&item, &pos);
			insert_mid(item, pos);
			break;

		case 4:
			display();
			break;
		case 5:
			printf("enter the pos:\n");
			scanf("%d",&pos);
			delet_pos(pos);
			break;
		case 6:
			printf("enter the data\n");
			scanf("%d",&item);
			delet_data(item);
			break;
		case 7:
			exit(0);
		}
	}
	return 0;
}
Exemple #7
0
node* insert_end(node* head, int data)//Insert new node to the link list
{

	//Checking the  head
	if (head==NULL)
	{
	        node *newPtr=malloc(sizeof(node));//Allocating memory to the node
        	newPtr->value=data;
		newPtr->next=NULL;
		head=newPtr;
		return head;
	}
	else//Recursively go to the other node
		head->next=insert_end(head->next,data);

	return head;
}	
Exemple #8
0
int main (int argc, char** argv)
{
	//Error Checking//	
	if (argc != 2){
        printf("Incorrect Number of Command Line Arguments, Please try again!\n");
        return 0;
        }
        else if (strcmp(argv[1], "input.txt")!=0)
        {
                printf("Unable to open file!\n");
                return 0;
        }
	//Open file
	FILE *input=fopen(argv[1],"r");
	//Declaration of variable and initialization of head of link list
	int number,look;
	node *head=NULL;
	//Scanning and inputting value to the link list
	while (1)
        {
		fscanf(input, "%d", &number); 
			if (feof(input)) 
			break;
		head=insert_end(head, number);
	}
	//Print the link list
	print_list(head);
	//Inputing value to be searched in the link list
	printf("Please enter a number to search for:");
	scanf("%d",&look);
	//Search the data value in the link list
	if((number=search(head,look))==1)
	printf("%d was found!\n",look);
	else
	printf("%d was not found!\n",look);
	//Changing the value in the link list into factorial value
	factorial_list(head);
	//Print the link list
	print_list(head);
	//Delete the memory allocated
	delete_list(head);
	//Closing the file
	fclose(input);
	return 0;
}
Exemple #9
0
/*------------------------------------------------------------------------------------------------------------
   get_special_list()
        Funcao que cria um lista contendo apenas os usuarios especiais de uma dada lista usando create_list()
	e salva os valores de posicao antiga utilizando get_old_position(). Retorna NULL quando ha erro na criacao
        - Parametros
          LIST* : lista contendo todos os usuarios
        - Retorno
          LIST* : lista contendo os usuarios especiais
*/
LIST *get_special_list(LIST *full_list){
	LIST *special_list = create_list();

	if(special_list != NULL){
		int i;
		NODE *aux;

		// Mecanismo de busca semelhante a print_list()
		for(i = 1; (aux = get_node(full_list, i)) != NULL; i++){
			// Coloca os usuarios especiais no final da nova lista. Caso n haja usuarios especiais a nova lista contera apenas a sentinela
			if(is_special_user(aux)) insert_end(special_list, aux->ID);
		}

		get_old_positions(full_list, special_list);
	}

	return special_list;
}
Exemple #10
0
/*------------------------------------------------------------------------------------------------------------
   get_list()
        Funcao que cria uma lista usando create_list() e le inteiros da stream de dados ate o fim do arquivo,
	inserindo nos no final da lista com ids iguais aos inteiros lidos. Caso haja erro na criacao, retorna NULL
        - Parametros
          FILE* : stream de dados a ser lida
        - Retorno
          LIST* : lista criada
*/
LIST *get_list(FILE *stream){
	LIST *list = create_list();

	if(list != NULL){
		int aux;

		while(fscanf(stream, "%d", &aux) != EOF){
			insert_end(list, aux);
		}
		// Precaucao
		if(list->last == NULL){
			remove_end(list);
		}

	}

	return list;
}
int main()
{
    int ch ;
    char choice;
    do
    {
        printf("Main menu:\n");
        printf("1 Create a list\n");
        printf("2 Insert in the beginning\n");
        printf("3 Insert in the middle\n");
        printf("4 Insert at the end\n");
        printf("Enter your choice\n");
        scanf("%d" , &ch);
        switch(ch)
        {
            case 1 : create();
                     break;
            case 2 : printf("Enter the value you want to insert : ");
                     scanf("%d" , &a);
                     insert_beg();
                     break;
            case 3 : printf("Enter the value you want to insert : ");
                     scanf("%d" , &a);
                     printf("Eter the value after which you want to insert : ");
                     scanf("%d" , &X);
                     insert_middle();
                     break;
            case 4 : printf("Enter the value you want to insert : ");
                     scanf("%d" , &a);
                     insert_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()
{
    int ch;
    struct node* head;
    struct node* addresses[100];
    scanf("%d", &ch);
    do
    {
         head = insert_end(head, ch);
         scanf("%d", &ch);
    } while (ch != -1);
    printf("\n");
    print_ll(head);
    int cnt = hash_table_approach(head, addresses);
    printf("\nCount of nodes is : %d \n", cnt);
    struct node* mid_ptr = addresses[cnt/2];
    printf("\nThe middle node is %d\n", mid_ptr->data);
    return 0;
}
Exemple #13
0
void main()
{
	int op;
	do
		{
			printf("enter option\n1: insert\n2:display\n3:delete min\n 4: exit\n");
			scanf("%d",&op);
			switch (op)
				{
				case 1:  insert_end();
					break;
				case 2:  display();
					break;
				case 3:  delete_min();
					break;
				case 4: break;
				}
		}
	while (op!=4);
}
Exemple #14
0
void NODE :: insert_loc(int val,int loc)
{
	if(val == -1){         //-1 is an invalid or cancel value
		cout << endl<<MSG_INVAL_VAL<<endl;
		return;
	}
	if(loc<1 || loc>(No_of_nodes+1)){       //If location is out of range
		cout << endl<<MSG_INVAL_LOC<<endl;
		return;
	}
	//Create a newnode to be inserted in the list
	NODE *newnode;
	newnode = new NODE;
	newnode->data = val;  //Set data
	//Set the pointers
	if(First == NULL){          //First ever node of the list
		First = newnode;
		Last = First;
		cout << "\nNew list created.\n";
	}
	else{   	//Search for correct location and insert the new node
		NODE *ptr, *preptr=NULL;
		int i=1;
		for(ptr=First ; ptr!=NULL ; ptr=ptr->next,++i)
		{
			if(i==loc)
				break;
			preptr = ptr;
		}
		if(ptr == First)
			insert_beg(val);
		else if(ptr == NULL)
			insert_end(val);
		else{
			preptr->next = newnode;
			newnode->next = ptr;
			No_of_nodes++;
			cout << "\nInsertion successful.\n";
		}
	}
}
void main ( )
{
clrscr();
void create ( );
void ftraverse ( );
void insert_beg ( );
void insert_end ( );
void insert_given_node ( );
create ( );
ftraverse ( );
insert_beg ( );
printf(" \n Doubly linklist after insertion at beginning: \n");
ftraverse ( );
insert_end ( );
printf(" \n Doubly linklist after insertion at end: \n");
ftraverse ( );
insert_given_node ( );
printf(" \n Doubly linklist after insertion at given node: \n");
ftraverse ( );
getch ( );
}
//Main Execution
void main()
{
	int a,i;
	clrscr();
	while(1)
	{
		printf("\n1.insert_beg\n");
		printf("2.insert_end\n");
		printf("3.display\n");
		printf("4.del_beg\n");
		printf("5.del_end\n");
		printf("6.del_after\n");
		printf("7.insert_befroe\n");
		printf("8.exit\n");
		printf("\n");
		printf("enter a choice:");
		scanf("%d",&a);
		switch(a)
		{
			case 1:insert_beg();
						break;
			case 2:insert_end();
						break;
			case 3:display();
						break;
			case 4:del_beg();
						break;
			case 5:del_end();
						break;
			case 6:del_after();
						break;
			case 7:insert_before();
						break;
			case 8:
						exit(1);
			default:
						printf("enter a valid choice");
		}
	}
}
stack* pop(stack *top,mn *m,ll *temp)
{
    stack *n;
    char a[20];
    int i=0;
    if(top == NULL)
    {
        printw("-");
        return top;

    }
    n = top;
    if(n->key)
        strcpy(a,n->key);
    else
        printw("sorry");

    while((temp != NULL)&&(temp->a != ' ')) 
    {
            temp = temp->prev;        
    }

    m->end = temp;

    //printw("%s",a);

    while(a[i] !='\0')
    {
        m = insert_end(m,a[i]);
        i++;
    }
    
    clear();
    refresh();
    traverse(m);
    top = top->next;
    free(n);

return top;
}
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 ( );
}
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);
}
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;
}
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;
}
Exemple #22
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()
{
        int choice;//For storing user choice
        node *start = NULL; //pointer to the first node, which initially points nowhere
	
        do
        {
		printf("***************************************************************************\n");
		printf("\t\tMENUE\n");
		printf("****************************************************************************\n");
                printf("\t1.Create List\n");
                printf("\t2.Display\n");
                printf("\t3.Count\n");
                printf("\t4.Add to empty list\n");
		printf("\t5.Add at beginning\n");
                printf("\t6.Add at end\n");
                printf("\t7.Add after node\n");
                printf("\t8.Add before node\n");
                printf("\t9.Delete\n");
                printf("\t10.Quit\n\n");
		printf("********************************************************************************\n\n");
                printf("Enter your choice : ");
                scanf("%d",&choice);

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

                 case 2:
                        display(&start);
                        break;

                 case 3:
                        count(&start);
                        break;

                 case 4:
                        insert_empty(&start);
                        break;

                 case 5:
                        insert_beginning(&start);
                        break;

                 case 6:
                        insert_end(&start);
                        break;

                 case 7:
                        insert_after(&start);
                        break;

                 case 8:
                        insert_before(&start);
                        break;

                 case 9:
                        delete_node(&start);
                        break;

                 case 10:
                         break;

                 default:
                         printf("Wrong choice\n");

                }/*End of switch */

        }while(10!=choice); //End of while

        free_list(&start);
return 0;
}//end of main