gpt4 book ai didi

c - 如何删除指针数组的第一个值?

转载 作者:行者123 更新时间:2023-11-30 16:51:51 24 4
gpt4 key购买 nike

我正在尝试编写一个函数(Remove_Item),该函数获取指针数组以及数组的长度,并删除第一个指针。这是代码:

 #include <stdio.h>
#include <stdlib.h>

void Print_Menu();
void Clear_Queue(int ***list,int size);
void Add_Item(int ***list,int size);
void Remove_Item(int ***list,int size);
void Print_Queue(int **list,int size);
void Print_MaxMin(int **list,int size,int Max_or_Min);
void Print_Index(int **list,int value,int size);


int main()
{
int size_list = 0,input = 1,new_size,value;
int **FIFO = (int**)malloc((size_list+1)*sizeof(int*));
FIFO[0] = NULL;
Print_Menu();

while(input != 0)
{
scanf("%d", &input);

switch(input)
{
case 0:
Clear_Queue(&FIFO,size_list);
return;
case 1:
size_list++;
Add_Item(&FIFO,size_list);
break;
case 2:
Remove_Item(&FIFO,size_list);
--size_list;
break;
case 3:
Print_Queue(FIFO,size_list);
break;
case 4:
Print_MaxMin(FIFO,size_list,1);
break;
case 5:
Print_MaxMin(FIFO,size_list,-1);
break;
case 6:
printf("Please enter value to find index\n");
scanf("%d",&value);
Print_Index(FIFO,value,size_list);
break;
case 7:
Clear_Queue(&FIFO,size_list);
size_list = 0;
break;
case 8:
Print_Menu();
break;
default:
printf("Error: Unrecognized choice\n");
}
if (input != 8)
printf("Please select your next choice (select 8 for complete menu)\n");
}
return;
}

void Print_Menu()
{
printf("Please select your choice:\n");
printf("0.Exit\n");
printf("1.Add item to the queue\n");
printf("2.Remove item from the queue\n");
printf("3.Print queue\n");
printf("4.Print the maximum item in the queue\n");
printf("5.Print the minimum item in the queue\n");
printf("6.Print index of given item\n");
printf("7.Clear queue\n");
printf("8.Print the menu\n");
}


void Clear_Queue(int ***list,int size)
{
int i=0;
for(i=0 ; i<size ; i++)
{
free((*list)[i]);
(*list)[i] = NULL;
}
printf("is clear\n");

}

void Add_Item(int ***list,int size)
{
int i=0,value;

*list = (int**)realloc(*list,(size)*sizeof(int*));

if((*list) ==NULL)
{
printf("Error\n");
Clear_Queue(list,size);
return;
}
(*list)[size-1] = (int*)malloc(1*sizeof(int));

if((*list)[size-1] == NULL)
{
printf("Error\n");
Clear_Queue(list,size);
}

printf("Enter item value to add\n");
scanf("%d",&value);
(**list)[size-1] = value;
printf("item %d added\n",(**list)[size-1]);

}

void Remove_Item(int ***list,int size)
{
int i=0,item,tmp;

if((*list)[0] == NULL)
{
printf("Error queue is empty\n");
return;
}

item = *((*list)[0]);

for(i=0 ; i<size-1 ; i++)
{
*((*list)[i]) = *((*list)[i+1]);
}
*list = (int**)realloc(*list,size-1);
printf("item %d was removed\n",item);
}

void Print_Queue(int **list,int size)
{
int i=0,item;
if(list[0] == NULL)
{
printf("Error queue is empty\n");
return;
}

for(i=0 ; i<size ; i++)
{
item = (*list)[i];
printf("%d ",item);
}

printf("\n");
return;
}

void Print_MaxMin(int **list,int size,int Max_or_Min)
{
int i,j,max,min;
if((list)[0] == NULL)
{
printf("Error queue is empty\n");
return;
}
max = (*list)[0];
min = (*list)[0];
for(i=1 ; i<size ; i++)
{
if(((*list)[i] > max)&&(Max_or_Min == 1))
{
max = (*list)[i];
}
if(((*list)[i] < min)&&(Max_or_Min == -1))
{
min = (*list)[i];
}
}
if(Max_or_Min == 1)
{
printf("The maximum value is %d\n",max);
}
if(Max_or_Min == -1)
{
printf("The minimum value is %d\n",min);
}
}

void Print_Index(int **list,int value, int size)
{
int i,current,index_found=0;

if(list[0] == NULL)
{
printf("Error queue is empty\n");
return;
}

for(i=0 ; i<size ; i++)
{
current = (*list)[i];
if(current == value)
{
printf("The index of %d is %d\n",value,i);
index_found++;
}
}
if(index_found == 0)
{
printf("Error value not in array\n");
}
}

问题在于,当输入为1 2 3时,输出为0 2。而不是删除第一个元素,会将第一个元素变为零并删除最后一个元素。

最佳答案

我的用于删除数组中第一个指针的代码:

#include <stdio.h>
#include <stdlib.h>
void removep(int *, int);
int main() {
int * arr = malloc(3*sizeof(int *));
arr[0] = 1;
arr[1] = 5;
arr[2] = 10;
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
removep(arr, 3);
printf("%d %d", arr[0], arr[1]);
return 0;
}

void removep(int arr[], int n) {
int i;
for(i = 0; i < n - 1; i++) {
*(arr+i) = *(arr+i+1);
}
arr = realloc(arr, (n-1)*sizeof(int *));
}

输出:

1 5 10
5 10

希望有帮助。

关于c - 如何删除指针数组的第一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41555472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com