struct
student
{
unsigned
int
stu_ID
char
sex
int
age
struct
student
*st
}
struct
student
*p,*h,*s
student*
crate_list(){//建立链表,返回链表的头结点
int
i=20
unsigned
short
c
p
=
new
student
h
=
p
p->stu_ID=1000
p->sex
=
'M'
p->age
=
i
do{
s=new
student
s->stu_ID=i+1000
s->sex
=
'M'
s->age
=
i
p->st
=
s
p
=
p->st
i++
}while(i<31)
p
=
NULL
return
h
}
bool
delete_node(student*
p,int
a){//传入头结点和年龄
if(p){
student
*
temp=p,*p_pre
while(temp->age!=a){
p_pre=temp
temp=temp->st
if(!temp)
return
false
}
p_pre->st=temp->st
delete
temp
temp=NULL
return
true
}
return
false
}
void
display_list(student
*
h){//打印链表中的数据
student*
temp=p
while(temp->st)
cout<<temp->stu_ID<<"
"<<temp->sex<<"
"<<temp->age<<endl
}
同学你好,我帮你运行了一下没有死掉,你的程序有问题,这是我帮你写的新代码,我运行了是可以的,你看看,希望你能采纳,给个赞,有什么问题可以问我
# include <stdio.h>
# include <stdlib.h>
# include <malloc.h>
typedef struct node
{
int data
struct node *pNext
}NODE, *PNODE
PNODE Create_List(void)
void Traveser_List(PNODE)
bool Search_List(PNODE, int, int *)
int main()
{
int Loc
PNODE pHead
pHead = Create_List()
Traveser_List(pHead)
if(Search_List(pHead, 6, &Loc))//查找链表中数值为3的结点,并输出结点的位置
{
printf("此元素在链表中的位置是第%d个结点\n", Loc)
}
else
{
printf("此链表中没有您要找的元素!\n")
}
return 0}
PNODE Create_List(void)
{
int len
int val//存放创造结点的有效数值
printf("请输入链表长度len= ")
scanf("%d", &len)
PNODE pHead = (PNODE)malloc(sizeof(NODE))//创造头结点
if(pHead == NULL)
{
printf("动态内存分配失败!\n")
exit(-1)
}
PNODE Tail = pHeadTail->pNext = NULL//创造一个指针使之始终指向为为节点
for(int i = 0i<leni++)
{
printf("请输入第%d个节点的值:", i+1)
scanf("%d", &val)
PNODE pNew = (PNODE)malloc(sizeof(NODE))
if(pNew == NULL)
{
printf("动态内存分配失败!\n")
exit(-1)
}
pNew->data = val
Tail->pNext = pNew
pNew->pNext = NULL
Tail = pNew
}
return pHead
}
void Traveser_List(PNODE pHead)
{
PNODE p = pHead->pNext
while(p != NULL)
{
printf("%d ", p->data)
p = p->pNext
}
printf("\n")
}
bool Search_List(PNODE pHead, int i, int *Loc)
{
PNODE p = pHead->pNext
int count = 1
while(NULL != p &&p->data != i)
{
p = p->pNext
count++
}
if(p == NULL)return false
*Loc = count
return true
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)