int main(void)
{
    initializeList();
    while(1)
    {
        printf("1. Insert new item. 2. Delete item. 3. Search item. \n");
        printf("4. Insert Last. 5. Delete Last. 6. Print forward. \n");
        printf("7. Print backward. 8. exit.\n");

        int ch;
        scanf("%d",&ch);
        if(ch==1)
        {
            int item;
            scanf("%d", &item);
            insertFirst(item);
        }
        else if(ch==2)
        {
            int item = deleteLast();
            if(item!=NULL_VALUE) printf("Deleted: %d\n", item);
        }
        else if(ch==3)
        {
            int item;
            scanf("%d", &item);
            struct listNode * res = searchItem(item);
            if(res!=0) printf("Found.\n");
            else printf("Not found.\n");
        }
        else if(ch==4)
        {
            int item;
            scanf("%d",&item);
            insertLast(item);
        }
        else if(ch==5)
        {
            deleteLast();
        }
        else if(ch==6)
        {
            printListForward();
        }
        else if(ch==7)
        {
            printListBackward();
        }
        else if(ch==8)
        {
            freeall(list);
            break;
        }
    }

}
Ejemplo n.º 2
0
	void noExplictparam()
	{
		printf("no file supplied\n");
		DlList_T lst=dll_create();
		int hasChanged=0;
		int looping=1;
		char buff[MAX_LINE];
		while(looping)
 		{
 			fgets(buff,MAX_LINE, stdin);
 			strtok(buff,"\n");
			if(strcmp(buff,"Q")==0)
			{

				dll_destroy(lst);
				break;



			}
			else if(strcmp(buff,".")==0)
			{
				showCursor(lst);

			}

			else if(strcmp(buff,"a")==0)
			{

				int currentlooping=1;
				while(currentlooping)
				{
					fgets(buff,MAX_LINE, stdin);
					strtok(buff,"\n");
					if(strcmp(buff,".")==0)
					{
						
						break;

					}
					else
					{
					
						void* input=malloc(sizeof(char)*(strlen(buff)));
						memcpy(input,buff,strlen(buff));
						dll_append(lst, input);
						dll_move_to(lst,dll_size(lst));
						hasChanged=1;
						//printf("SIZE %d\n",dll_size(lst) );
						//showCursor(lst);
						//printList(lst);

					}



				}



			}
			
			else if(strcmp(buff, "\n")==0 || strcmp(buff,"+")==0)
			{
				if(getNext(lst)!=NULL)
				{

					dll_move_to(lst,getCursorNumber(lst) +1);



				}
				else
				{
					printf("?\n" );

				}
				

			}
			else if(strcmp(buff,"-")==0)
			{

				if(getPrevious(lst)!=NULL)
				{

					dll_move_to(lst,getCursorNumber(lst) -1);



				}



			}
			else if(strcmp(buff,"$")==0)
			{

				if(getHead(lst)==NULL)
				{

					printf("?\n");

				}
				else
				{

				dll_move_to(lst,dll_size(lst));
				showCursor(lst);
				}

			}
			//NEEDS WORKS
			else if(isdigit(buff))
			{	printf("GOT HERE\n");
				int newIndex=atoi(buff);
				if(newIndex>=1 && newIndex<=dll_size(lst))
				{

					dll_move_to(lst,newIndex);

				}



			}
			else if(strcmp(buff,".=")==0)
			{


				printf("%d\n",getCursorNumber(lst));

			}
			else if(strcmp(buff,"$=")==0)
			{

				printf("%d\n",dll_size(lst));


			}
			else if(strcmp(buff,"p")==0)
			{
				printListForward(lst);
				dll_move_to(lst,dll_size(lst));


			}
			else if(strcmp(buff,"q")==0)
			{

				if(hasChanged)
				{

					printf("? buffer dirty\n");

				}
				else
				{

					dll_destroy(lst);
					printf("\n");
					printf("Bye\n");
					break;



				}



			}
			else if(strcmp(buff,"w")==0)
			{


				printf("?\n");


			


			}
			else if(strcmp(buff,"wq")==0)
			{
					printf("?\n");





				
			}
			else if(strcmp(buff,"i")==0)
			{	
				
		
				int looping=1;
				while(looping)
				{
					fgets(buff,MAX_LINE, stdin);
					printf("%d\n",strcmp(buff,".") );
					
					if(strcmp(buff,".")==10)
					{
						break;

					}
					else
					{
						dll_insert_at(lst,getCursorNumber(lst),(void *) buff);
						dll_move_to(lst,getCursorNumber(lst));


					}
	

				}
				
				
	



			}
			else if(strcmp(buff,"d")==0)
			{
				dll_pop(lst,getCursorNumber(lst));



			}
			else 
			{
				
			}
}


	}
Ejemplo n.º 3
0
	void startLookingforInput(DlList_T lst,const char * filename)
	{
		int hasChanged=0;
		int looping=1;
		char buff[MAX_LINE];
		while(looping)
 		{
 			fgets(buff,MAX_LINE, stdin);
 			strtok(buff,"\n");
			if(strcmp(buff,"Q")==0)
			{

				dll_destroy(lst);
				break;



			}
			else if(strcmp(buff,".")==0)
			{
				showCursor(lst);

			}

			else if(strcmp(buff,"a")==0)
			{

				int currentlooping=1;
				while(currentlooping)
				{
					fgets(buff,MAX_LINE, stdin);
					strtok(buff,"\n");
					if(strcmp(buff,".")==0)
					{
						
						break;

					}
					else
					{
					
						void* input=malloc(sizeof(char)*(strlen(buff)));
						memcpy(input,buff,strlen(buff));
						dll_append(lst, input);
						dll_move_to(lst,dll_size(lst));
						hasChanged=1;
						//printf("SIZE %d\n",dll_size(lst) );
						//showCursor(lst);
						//printList(lst);

					}



				}



			}
			
			else if(strcmp(buff, "\n")==0 || strcmp(buff,"+")==0)
			{
				if(getNext(lst)!=NULL)
				{

					dll_move_to(lst,getCursorNumber(lst) +1);



				}
				else
				{
					printf("?\n" );

				}
				

			}
			else if(strcmp(buff,"-")==0)
			{

				if(getPrevious(lst)!=NULL)
				{

					dll_move_to(lst,getCursorNumber(lst) -1);



				}



			}
			else if(strcmp(buff,"$")==0)
			{

				if(getHead(lst)==NULL)
				{

					printf("?\n");

				}
				else
				{

				dll_move_to(lst,dll_size(lst));
				showCursor(lst);
				}

			}
			//NEEDS WORKS
			else if(isdigit(buff))
			{	printf("GOT HERE\n");
				int newIndex=atoi(buff);
				if(newIndex>=1 && newIndex<=dll_size(lst))
				{

					dll_move_to(lst,newIndex);

				}



			}
			else if(strcmp(buff,".=")==0)
			{


				printf("%d\n",getCursorNumber(lst));

			}
			else if(strcmp(buff,"$=")==0)
			{

				printf("%d\n",dll_size(lst));


			}
			else if(strcmp(buff,"p")==0)
			{
				printListForward(lst);
				dll_move_to(lst,dll_size(lst));


			}
			else if(strcmp(buff,"q")==0)
			{

				if(hasChanged)
				{

					printf("? buffer dirty\n");

				}
				else
				{

					dll_destroy(lst);
					printf("\n");
					printf("Bye\n");
					break;



				}



			}
			else if(strcmp(buff,"w")==0)
			{


				FILE* pFile = fopen(filename, "w");
				if (!pFile) {
					perror("The following error occurred:");
				} else {
					struct node* headNode=getHead(lst);

					while(headNode!=NULL)
					{
						
						fprintf(pFile, strcat((char *) (getData(headNode)),"\n"));
						headNode=nextNode(headNode);
					
					
					}
					printf("%s:file\n",filename );
					hasChanged=0;
					fclose(pFile);


			}


			}
			else if(strcmp(buff,"wq")==0)
			{
				FILE* pFile = fopen(filename, "w");
				if (!pFile) {
					perror("The following error occurred:");
				} 
				else {
					struct node* headNode=getHead(lst);

					while(headNode!=NULL)
					{
						
						printf("%s\n", (char *) (getData(headNode)) );
						fprintf(pFile, strcat((char *) (getData(headNode)),"\n"));
						headNode=nextNode(headNode);
					
					
					}
					printf("%s:file\n",filename );
					hasChanged=0;
					fclose(pFile);
					dll_destroy(lst);
					printf("\n");
					printf("Bye\n");
					break;





				}
			}
			else if(strcmp(buff,"i")==0)
			{	
				
		
				int looping=1;
				while(looping)
				{
					fgets(buff,MAX_LINE, stdin);
					printf("%d\n",strcmp(buff,".") );
					
					if(strcmp(buff,".")==10)
					{
						break;

					}
					else
					{
						dll_insert_at(lst,getCursorNumber(lst),(void *) buff);
						dll_move_to(lst,getCursorNumber(lst));


					}
	

				}
				
				
	



			}
			else if(strcmp(buff,"d")==0)
			{
				printf("HITIN\n");
				dll_pop(lst,getCursorNumber(lst));



			}
			else 
			{
				
			}
}
}