① c语言中关于链表的删除
所谓链表,就是用指针将内存中动态分配的结点空间,链接起来成一个表。
所以,建表的过程即是每次为新结点分配内存;因此,释放空间的话,也要从头到尾,一个一个结点的释放,这样才能全部释放掉。
这段代码释放了整个链表空间内存;while循环的作用是从头到尾释放后续结点,如果直接free(pHead)则后面的结点将无法找到,那么造成内存空间泄露。
另外,你的while循环存在一个错误,假设释放了倒数第一个结点后,pHead指向最后一个结点,而最后一个结点的next为NULL,那么这样最后一个结点也没有释放掉,while就退出了。
while循环应该更正为:
while(pHead!=NULL)
{
pNext=pHead->next;
free(pHead);
pHead=pNext;
}
② C语言中如何用free清除一串链表
#include<stdio.h>
#include<stdlib.h>
structNode{
intcon;
Node*next;
};
Node*insert(Node*h,inta,intpos)//这里,增加返回类型
{
Node*p=(Node*)malloc(sizeof(Node));
Node*pt=h;
if(pos==0)
{
p->con=a;
p->next=h;
h=p;
}
else
{
for(inti=1;i<pos;i++)//这里,如果pos值输入大于节点个数,会导致越界,自己修改
{
pt=pt->next;
}
p->con=a;
p->next=pt->next;
pt->next=p;
}
returnh;//这里,返回地址
}
intmain()
{
inti;
intcount=0;
puts("Enternum");
scanf("%d",&i);
Node*p=(Node*)malloc(sizeof(Node));
Node*head=p;
p->next=NULL;
p->con=i;
Node*c=head;
puts("Nextone,nonnumtostop");
while(scanf("%d",&i)==1)
{
fflush(stdin);
p=(structNode*)malloc(sizeof(structNode));
p->con=i;
c->next=p;
p->next=NULL;
c=p;
puts("Nextone,nonnumtostop");
}
fflush(stdin);
inta,pos;
puts("insertaatpos");
scanf("%d%d",&a,&pos);
head=insert(head,a,pos);//这里,地址传出来
c=head;
while(c!=NULL)
{
printf("%d%d ",count,c->con);
count++;
c=c->next;
}
c=head;
Node*tmp;
while(c!=NULL)//here,多了个分号,内存释放需要改
{
tmp=c;
c=c->next;
free(tmp);
}
return0;
}
③ C语言链表删除的问题 不知道怎么回事链表删不掉 求教!!!!
#include<stdio.h>
#include<stdlib.h>
struct
node
{
int
no;
struct
node
*next;
};
void
display(struct
node
*head)
{
while(head)
{
printf("%d
",head->no);
head=head->next;
}
}
void
del(struct
node
*&head)//这里
参数要使用引用否则当删除链表头的节点时会造成错误
{
int
t;
struct
node
*q,*p;
scanf("%d",&t);
if(head->no==t)
{
p=head;
head=head->next;
free(p);
}
else
{
q=head;
p=head->next;
while(p&&(p->no!=t))
{
q=p;
p=p->next;
}
if(!p)return;
q->next=p->next;
free(p);
}
}
//该函数用于释放整个链表
void
ReleaseAll(struct
node
*&head)
{
struct
node
*pNode;
while
(head
!=
NULL)
{
pNode
=
head;
head
=
head->next;
free(pNode);
}
}
int
main()
{
struct
node
*p,*head=NULL;
int
t;
scanf("%d",&t);
while(t>0)
{
p=(struct
node
*)malloc(sizeof(struct
node));
p->no=t;
p->next=head;
head=p;
scanf("%d",&t);
}
display(head);
del(head);
display(head);
ReleaseAll(head);
display(head);
printf("\n");
system("pause");
}
④ C语言 删除链表
那里面head和tail都是全局变量。你看仔细点。
⑤ c语言结构体链表的节点删除问题 求助大佬
这个出错点在行 while() 循环之后的那一句: free(l->tail); 第一段程序,其实在 while() 循环里面已经把全部节点都释放了,包括尾节点,所以第一段程序不需要再释放一次 l->tail,只需要保留 l->tail = NULL; 即可。 第二段程序,因为最后一个...
2019-10-14回答者:隐世高手0074个回答
c语言中删除链表指定节点的一些问题?
问:struct student *Del (struct student *head, int num) { struct studen...
答:你仔细看看 while (p1->num != num && p1->next != NULL) 这一句之前的代码 p2什么时候赋值过? 而且if (p2 == head) //如果要删除的节点是第一个节点 既然是第一个节点,那么 while (p1->num != num && p1->next != NULL) 这个循环一次都没有执...
2015-10-20回答者:知道网友1个回答2
C语言数据结构的链表删除与查找问题~~,实在是被弄...
问:#include "stdlib.h" #include "stdio.h" typedef struct node{ //链表...
答:#include #include typedef struct LNode {char character; struct LNode*next; }LNode,*PLNode; PLNode CreateList()/*创建单链表*/ {PLNode P,head,q; int i; head=(PLNode)malloc(sizeof(LNode)); p=head; p->next=NULL; for(i=0;icharacter=...
2010-11-25回答者:qjbrh532个回答
C语言删除链表结点 结点结构如下
问:#define ELEMTYPE char typdef struct node { ELEMTYPE date; struct no...
答:我提供思路哈,你自己写一下,这个不难的。 分为两种情况: 1、删除的是头结点,这又可以分为两种情况:a)若是链表只有一个头结点,那么删除后头结点为NULL;b)若是链表不止一个节点,那么head指针指向头结点下一个节点。两种情况都要free 之...
⑥ C语言链表删除
对于链表的访问遍历,最好是判断表指针是否为NULL来决定是否继续,如:
p=head ;
while( p != NULL ) //当结点指针不为空时,遍历表,这要求建表时,尾结点的next=NULL!
{
printf("num=%d\n", p->num );
p=p->next ;
}
你自己调整一下自己的代码吧,估计是删除结点时,按个数访问时,访问到了NULL时,你还在操作结点指针。
⑦ C语言链表删除问题
所谓链表,就是用指针将内存中动态分配的结点空间,链接起来成一个表。
所以,建表的过程即是每次为新结点分配内存;因此,释放空间的话,也要从头到尾,一个一个结点的释放,这样才能全部释放掉。
这段代码释放了整个链表空间内存;while循环的作用是从头到尾释放后续结点,如果直接free(pHead)则后面的结点将无法找到,那么造成内存空间泄露。
另外,你的while循环存在一个错误,假设释放了倒数第一个结点后,pHead指向最后一个结点,而最后一个结点的next为NULL,那么这样最后一个结点也没有释放掉,while就退出了。
⑧ C语言删除链表问题
用这个程序到处回答问题 发财了我 你看下原码了 ,不懂可以问我的 ,必须要采纳哦
理解二级指针就都好办了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student //定义节点类型
{
char name[20];
int age;
struct student *next;
};
typedef struct student * list; //定义节点的指针类型
void insert(list *ll,int age,char name[]) //插入元素
{
list p=(list)malloc(sizeof(struct student));
p->age=age;
strcpy(p->name,name);
p->next=NULL;
while(*ll!=NULL)
{
ll=&((*ll)->next);
}
*ll=p;
}
void remove(list *ll,char name[]) //ll为指向指针的指针,name[]为你要删除的姓名
{
list p=*ll; //定义p指向链表的头指针
while((p=*ll)!=NULL)
{
if(strcmp(p->name,name)==0) // 等于进入,删除节点后不往下跳
{
*ll=(*ll)->next; //删除链表中一个节点
delete p;//释放内存
}
else
ll=&((*ll)->next); //不等于往下跳一个节点
}
}
int main()
{
list ll=NULL; //定义头指针 赋值空
insert(&ll,20,"lona"); //插入元素
insert(&ll,20,"lona");
insert(&ll,20,"lona");
insert(&ll,20,"lona");
insert(&ll,20,"hello");
remove(&ll,"lona"); //删除相同的元素
while(ll!=NULL) //打印结构 仅仅调试而已,这样做会破坏链表 建议用临时指针来打印
{
printf("%d,%s\n",ll->age,ll->name);
ll=ll->next;
}
}
⑨ C语言链表 删除
structLNode*delete(LNode*head)
{
LNode*node=head;
LNode**parent=&head;
doublemin,max;
printf("请输入min和max:");
scanf("%lf%lf",&min,&max);
while(node)
{
//大于min和小于max,则删除节点
if(node->data>min&&node->data<max)
{
*parent=node->next;
//如果你的LNode是malloc出来的,需要free(node);
node=*parent;
}
else
{
node=node->next;
parent=&node->next;
}
}
returnhead;
//这个逻辑不需要给你太多注释,你只需要拿一支笔,手动画一个3个节点的链表,
//然后从函数执行开始,一步一步去推怎么执行的就懂了。
//至于你的程序的错误,没帮你看
}
⑩ 如何删除链表(C语言).
把头节点拆下,把剩下的结点当两个,第一个作为第一个,剩下的作为第二个,先删除第一个,在删除第二个,直到第二个为空