Beispiel #1
0
/* Malloc Function */
void *MyMalloc(int size){
	
	//checking whether given size is lesser than the (array_size - 1000) if okay then proceed, else give an error
	if(size < (ARRAY_SIZE - 1000)){
		
		//void pointers to be used
		void *vir,*adr;
	
		//checking for the HEAD : to find out the initial starting point
		if(!memory[HEAD] && (size+13)<(ARRAY_SIZE - 4)){
			//setting initial starting point
			memory[HEAD]=4;	
			//pointing vir_pointer to the starting address
			vir = &memory[memory[HEAD]];
			//setting the previous point for the block
			insert_pos(HEAD,4);
			memory[4]=0;
			memory[8]=size;
			vir += 8;
			adr = vir;
			memory[12+size] = TRUE;
			insert_pos(17+size,13+size);
			//returning the address
			return adr;					
		}

		/* if HEAD is initialized then check for the next position which HEAD points */
		else{
			int x = memory[HEAD];
			if((isAvailable(x) == TRUE) && (giveSize(x) == size || giveSize(x) > size)){
				void *vir,*adr;
				vir = &memory[x];
				vir += 8;
				adr = vir;
				//setting the chunck is filled
				memory[x+8+size] = TRUE;
				return adr;						
			}
			else{
				int prev = x;
				int next = nextPosition(x);//taking the next position adjacent to 'x'
				//try to find a available slot from the memory
				while(((isAvailable(next) == FALSE) ||  (giveSize(next) < size )) && (next+13+giveSize(next)+size<19996) && giveSize(next) != 0){
					//if not found then go into the loop : set 'prev' to current 'next' and 'next' to next_position of 'next'
					prev = next;
					next = nextPosition(next);
					//check whether new 'next' is available : if available then allocate it, else continue the loop
					if((isAvailable(next) == TRUE) && (giveSize(next)==0)){
						void *vir,*adr;
						insert_pos(prev,next);
						insert_size(size,next+4);
						vir = &memory[next];
						vir += 8;
						adr = vir;
						//setting the chunk is filled
						memory[next+8+size] = TRUE;
						insert_pos(next+size+13,next+size+9);
						return adr;							
					}
					else{
						continue;
					}
				}
				//if available memory slot is found, then allocate it , else give an error 
				if(isAvailable(next) && (next+13+giveSize(next)+size < 19996) && (giveSize(next) == size || giveSize(next) > size || giveSize(next) == 0)){
					
						void *vir,*adr;
						insert_pos(prev,next);
						insert_size(size,next+4);
						vir = &memory[next];
						vir += 8;
						adr = vir;
						//setting the chunk is filled
						memory[next+8+size] = TRUE;
						insert_pos(next+size+13,next+size+9);
						return adr;							
				}
				printf("Cannot allocate space. Memory is filled.\n");
			}			
		}
	

	}

	else{
		printf("The asked Memory Portion cannot be allocated.\n");
	}
	


}
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;
}
Beispiel #3
0
void main()
{
    SeqList mylist;
    InitSeqList(&mylist);
    ElemType Item;
    int pos;
    int select = 1;
    while(select)
    {
        printf("**********************************************\n");
        printf("**  [1] push_back       [2] push_front      **\n");
        printf("**  [3] pop_back        [4] pop_front       **\n");
        printf("**  [5] insert_pos      [6] show_list       **\n");
        printf("**  [7] del_pos         [8] del_val         **\n");
        printf("**  [9] find_pos        [10]find_Elem       **\n");
        printf("**  [11]sort            [12]resver          **\n");
        printf("**  [13]clear           [14]merge           **\n");
        printf("**  [15]destroy         [0]quit_system      **\n");
        printf("**********************************************\n");


        printf("请选择:>");
        scanf("%d",&select);
        if(select == 0)
        {
            break;
        }
        switch(select)
        {
            case 1:
            {
                printf("请输入要插入的数据(-1结束):>");
                while(scanf("%d",&Item),Item!=-1) //此处为逗号表达式,真假值由最后一个表达式决定,
                //所以此处在扫描到Item不是-1时就会自动执行一下下面的函数,
                //所以每输入一次非-1的数就头插一次。
                {
                    push_back(&mylist,Item);
                }
                break;
            }
            case 2:
            {
                printf("请输入要插入的数据(-1结束):>");
                while(scanf("%d",&Item),Item!=-1) //此处为逗号表达式,真假值由最后一个表达式决定,
                //所以此处在扫描到Item不是-1时就会自动执行一下下面的函数,
                //所以每输入一次非-1的数就头插一次。
                {
                    push_front(&mylist,Item);
                }
                break;
            }
            case 3:
            {
                pop_back(&mylist);
                break;
            }
            case 4:
            {
                pop_front(&mylist);
                break;
            }
            case 5:
            {
                ElemType e;
                printf("请输入要插入的位置,当前有元素%d:",mylist.size);
                scanf("%d",&pos);
                printf("请输入要插入的元素值:");
                scanf("%d",&e);
                insert_pos(&mylist,pos,e);
                break;
            }
            case 6:
            {
                show_list(&mylist);
                break;
            }
            case 7:
            {
                printf("请输入要删除的位置,当前有元素%d:",mylist.size);
                scanf("%d",&pos);
                del_pos(&mylist,pos);
                break;
            }
            case 8:
            {
                printf("请输入要删除的元素:");
                scanf("%d",&Item);
                printf("start and Item=%d\n",Item);
                del_val(&mylist,Item);
                break;
            }
            case 9:
            {
                printf("请输入要查找线性表元素的位置:");
                scanf("%d",&pos);
                find_pos(&mylist,pos,&Item);
                printf("线性表的第%d个元素为:%d\n",pos,Item);
                break;
            }
            case 10:
            {
                printf("请输入要查找的元素:");
                scanf("%d",&Item);
                find_Elem(&mylist,Item,&pos);
                printf("元素%d所在位置为:%d\n",Item,pos);
                break;
            }
            case 11:
            {
                sort(&mylist);
                break;
            }
            case 12:
            {
                printf("resver start.\n");
                resver(&mylist);
                printf("resver end!\n");
                break;
            }
            case 13:
            {
                clear(&mylist);
                break;
            }
            case 14:
            {
                printf("目前无法测试,因为只有一个线性表,无法做合并操作。\n");
                //merge(SeqList *list1,SeqList *list2,SeqList *list_merge);
                break;
            }
            case 15:
            {
                destroy(&mylist);
                break;
            }
            default:
            {
                printf("输入的选择不存在,请重新输入。\n");
                select=1;
                break;
            }
        }
    }
}
//Driver function 
int main()
{
    int arr[MAX],i,size,choice;

    printf("Enter number of elements to be inserted (Maximum 20 ) : ");
    scanf("%d",&size);

    
    for(i=0;i<size;i++)
    scanf("%d",&arr[i]);

  


    traverse(arr,size);

while(1)
{

    printf("\t\t\n\t\t Select Array opertaion (Press 0 to exit ) : \n");
    printf("1.Insert new element at desired index (without replacement)\n");
    printf("2.Insert new element at desired index (with replacement)\n");

    printf("3.Search and add a new element before the search element \n");
    printf("4.Search and replace the search element with a new element\n");

    printf("5.Delete an element (Search by index)\n");
    printf("6.Delete an element (Search by value)\n");

    printf("7.Print array in reverse order \n");
	 	    
                  
    scanf("%d", &choice);


    switch(choice)
    {

      case 0 : printf("Exiting...\n");
    		return 0;	
	
      case 1 : insert_pos(arr,size);     
		size++;
                break;
      case 2: insert_and_replace(arr,size);
	        break;

      case 3: search_and_insert(arr,size);
		size++;
		break;
      case 4: search_and_replace(arr,size);
		break;
      case 5: Delete_by_index(arr,size);
		size--;
		break;
      case 6:Delete_by_value(arr,size);
		size--;
		break;

      case 7:reverse_traverse(arr,size);
		break;
	
     default : printf("Wrong choice..Please try again with a correct input(1-7)");  
  
    }

	
}

}