void insertNode(struct Node **head,int value)
{
struct Node *previous,*new,*current;
previous=NULL;
current=*head;
new=(struct Node *)malloc(sizeof(struct Node));
if(new==NULL)
{
printf("内存分配失败!");
exit(1);
}
while(current!=NULL && current->value<value)
{
previous=current;
current=current->next;
}
if(previous==NULL)
{
*head=new;
}
else
{
previous->next=new;
}
new->value=value;
new->next=current;
}
void deleteNode(struct Node **head,int value)
{
struct Node *previous,*current;
current=*head;
previous=NULL;
while(current!=NULL && current->value!=value)
{
previous=current;
current=current->next;
}
if(current==NULL)
{
printf("找不到!\n");
return ;
}
else
{
if(previous==NULL)
{
*head=current->next;
}
else
{
previous->next=current->next;
}
}
free(current);
} 谢谢分享
清尘yt 发表于 2019-8-16 18:41
找不到主函数里面的内容啊 本帖最后由 cslu 于 2020-2-1 12:01 编辑
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int value;
struct Node *next;
};
void insertNode(struct Node **head,int value)
{
struct Node *previous,current,new;
current = *head;
previous = NULL;
while(current!= NULL && current->value < value)
{
previous=current;
current=current->next;
}
new=(struct Node *)malloc(sizeof(struct Node));
if(new==NULL)
{
printf("内存分配失败!");
exit(1);
}
new->value=value;
new->next=current;
if(previous==NULL)
{
*head=new;
}
else
{
previous->next=new;
}
}
void printNode(struct Node *head)
{
struct Node *current;
current=head;
while(current!=NULL)
{
printf("%d",current->value);
current=current->next;
}
putchar('\n');
}
int main(void)
{
struct Node *head=NULL;
int input;
while(1)
{
printf("请输入1个整数(输入-1表示结束):");
scanf("%d",&input);
if(input==-1)
{
break;
}
insertNode(&head,input);
printNode(head);
}
return 0;
}
{:10_249:}俺不知道哪里还有问题,编译不通过 cslu 发表于 2020-1-31 22:01
#include
#include
struct Node
这句:struct Node *previous,current,new;
应该改为: struct Node *previous,*current,*new;
(你少打了两个星号) 阎建伟 发表于 2020-4-17 10:39
这句:struct Node *previous,current,new;
应该改为: struct Node *previous,*current,*new;
(你少打 ...
晓得了,谢谢 void getInput(struct Person *person);
void outPut(struct Person *person);
void outPutAll(struct Person *person);
void addPerson(struct Person **person);
struct Person*find(struct Person *person, char input[]);
void findPerson(struct Person **person);
void fixPerson(struct Person *person);
void delPerson(struct Person **person);
void dispPerson(struct Person **person);
void release(struct Person *person);
int main(void)
{
struct Person *head = NULL;
int input;
printf("请按照说明,输入数字进行操作!\n\
1.新建联系人。\n\
2.查找联系人。\n\
3.更改联系人。\n\
4.删除联系人。\n\
5.显示所有联系人\n\
6.退出联系人。\n");
printf("输入数字:");
scanf("%d",&input);
while(input != 6)
{
switch (input)
{
case 1: addPerson(&head);break;
case 2: findPerson(&head);break;
case 3: fixPerson(head);break;
case 4: delPerson(&head);break;
case 5: dispPerson(&head);break;
case 6: release(head);break;
//default : printf("输入错误\n");continue;
}
putchar('\n');
printf("输入数字:");
scanf("%d",&input);
}
return 0;
}
写了两天,反复看了无数遍视频,终于写出来了!小甲鱼{:7_113:} 好好学习才能天天向上! 加油!{:10_265:} 不适用指针的指针,没写备注
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int value;
struct Node *next;
};
void insertNode(struct Node *head,int * value);
void printNode(struct Node *head);
void deleteNode(struct Node *head,int * value);
void insertNode(struct Node *head,int * value)
{
struct Node *previous;
struct Node *current;
struct Node *newnode;
current=head->next;
previous=NULL;
while(current !=NULL && current->value<(*value))
{
previous=current;
current=current->next;
}
newnode=(struct Node *)malloc(sizeof(struct Node));
if(newnode==NULL)
{
printf("内存分配失败了\n");
exit(1);
}
newnode->value=(*value);
newnode->next=current;
if(previous==NULL)
{
head->next=newnode;
}
else
{
previous->next=newnode;
}
}
void printNode(struct Node *head)
{
struct Node *temp;
temp=head;
while(temp->next !=NULL)
{
temp=temp->next;
printf("%d ",temp->value);
}
putchar('\n');
}
void deleteNode(struct Node *head,int *value)
{
struct Node *current;
struct Node *previous;
current=head->next;
previous=NULL;
while(current != NULL && current->value != (*value))
{
previous=current;
current=current->next;
}
if(current == NULL)
{
printf("抱歉,没有找到匹配项\n");
return ;
}
else
{
if(previous==NULL)
{
head->next=current->next;
}
else
{
previous->next=current->next;
}
free(current);
return ;
}
}
int main()
{
struct Node head;
head.next=NULL;
int value;
printf("开始进行插入测试\n");
while(1)
{
printf("请输入一个整数(输入-1结束)");
scanf("%d",&value);
if(value == -1)
{
break;
}
else
{
insertNode(&head,&value);
}
printNode(&head);
}
printf("开始进行删除测试\n");
while(1)
{
printf("请输入一个整数(输入-1结束)");
scanf("%d",&value);
if(value != -1)
{
deleteNode(&head,&value);
printNode(&head);
}
else
{
break;
}
}
return 0;
}
页:
[1]