c语言怎么删除字符如何删除列表节点

void Delete(struct student *head){&& &&& //根据输入的学号来确定删除的数据;&& &struct student *p;&& &struct student *Pguard=&& &if(head==NULL)&& //一定要判断链表是否为空;&& &&& &&& &printf(&Sorry,the list is NULL!/n&);&& &else&& &{&& &&& &printf(&please input the number you want to delete:/n&);&& &&& &scanf(&%d&,&numbers);&& &&& &if(!head)&& &&& &{&& &&& &&& &printf(&The list is NULL!/n&);&& &&& &&& &&& &&& &}&& &&& &if(head-&number==numbers)& //删除头节点,一定要判断,否则就会丢失整个链表;但是要是有点头节点的链表也可以不用判断;&& &&& &{&& &&& &&& &p=&& &&& &&& &head=head-&&& &&& &&& &free(p);&& &&& &&& &printf(&The number in the head has been deleted!!/n&);&& &&& &&& &&& &&& &}&& &&& &&& &for(PPguard-&Pguard=Pguard-&next) //除头节点以外的其他节点的删除方法;&& &&& &{&& &&& &&& &if(Pguard-&next-&number ==numbers)&& &&& &&& &{&& &&& &&& &&& &p=Pguard-&&& &&& &&& &&& &Pguard-&next=p-&&& &&& &&& &&& &free(p);&& &&& &&& &}&& &&& &&& &printf(&the numbers %d has been deleted!/n&,numbers);&& &&& &&& &&& &&& &}&& &&& &&& &printf(&the numbers %d is not found!/n&,numbers);&& &}}共有 957 人关注过本帖
标题:单链表删除第一个节点出现错误,不明白为什么。
来 自:北京
等 级:论坛游民
帖 子:20
专家分:24
结帖率:33.33%
&&已结贴√
&&问题点数:2&&回复次数:12&&&
单链表删除第一个节点出现错误,不明白为什么。
新手刚学数据结构,写了一个删除链表节点的代码。其他都正常,但是删除第一个节点出现问题,不明白为什么。请大神给予解惑。感激不尽!
下面是代码:
#include&stdio.h&
#include&malloc.h&
#include&stdlib.h&
struct listnode{
&&& struct listnode *
//创建长度为n的单链表
struct listnode *create(int n)
&&& struct listnode *p,*q,*
&&& head = (struct listnode *)malloc(sizeof(struct listnode));
&&& head = NULL;
&&& for( i = 1; i &= i++ )
&&&&&&&&scanf(&%d&,&a);
&&&&&&&&p = (struct listnode *)malloc(sizeof(struct listnode));
&&&&&&&&p-&data =
&&&&&&&&if(head == NULL)
&&&&&&&&&&&&head =
&&&&&&&&&&&&q =
&&&&&&&&else
&&&&&&&&&&&&q-&next =
&&&&&&&&&&&&q =
&&& q-&next = NULL;
//打印单链表
void print_list(struct listnode *head)
&&& while(head)
&&&&&&&&printf(&%d &,head-&data);
&&&&&&&&head = head-&
//删除单链表的第i个节点
struct listnode *delete_list(struct listnode *head, int i)
&&& int j = 0;
&&& struct listnode *p;
&&& while(p && j & i-1)
&&&&&&&&p = p-&
&&&&&&&&j++;
&&& if( !p-&next || j & i-1)
&&&&&&&&exit(1);
&&& p-&next = p-&next-&
int main()
&&& struct listnode *
&&& //head = NULL;
&&& printf(&输入链表的长度: &);
&&& scanf(&%d&,&n);
&&& head = create(n);&&& //&&& 创建长度为n的单链表
&&& printf(&\n输出单链表的值:\n&);
&&& print_list(head);
&&& printf(&\n输入要删除节点的位置:&);
&&& scanf(&%d&,&del);
&&& head = delete_list(head,del);
&&& printf(&\n删除后的单链表为:\n&);
&&& print_list(head);
&&& return 0;
[此贴子已经被作者于 21:15编辑过]
搜索更多相关主题的帖子:
来 自:流年
等 级:贵宾
威 望:82
帖 子:2768
专家分:14477
首先你并没有“删除”节点,那些节点的内存还在,其次你是delete
函数最后返回的是head而head并没有变
一片落叶掉进了回忆的流年。
来 自:北京
等 级:论坛游民
帖 子:20
专家分:24
回复 2楼 诸葛欧阳
//删除单链表的第i个节点
struct listnode *delete_list(struct listnode *head, int i)
&&& int j = 0;
&&& struct listnode *p,*q;
&&& while(p && j & i-1)
&&&&&&&&p = p-&
&&&&&&&&j++;
&&& if( !p-&next || j & i-1)
&&&&&&&&exit(1);
&&& q = p-&
&&& p-&next = q-&
&&& //p-&next = p-&next-&
&&& free(q);
按照你说的我也把那个内存释放掉了,但是运行结果还是错的。是我其他地方有问题吗?还是这个改的就不对?
不要嘲笑我家樱木!
等 级:ID已被封
威 望:30
帖 子:2976
专家分:7697
程序代码:
#include&stdio.h&
#include&malloc.h&
#include&stdlib.h&
struct listnode{
&&& struct listnode *
//创建长度为n的单链表
struct listnode *create(int n)
&&& struct listnode *p,*q,*
&&& head = (struct listnode *)malloc(sizeof(struct listnode));
&&& if(head) head-&next = NULL;
&&& for(i = <font color=#; i & i++)
&&&&&&&&scanf(&%d&, &a);
&&&&&&&&p = (struct listnode *)malloc(sizeof(struct listnode));
&&&&&&&&p-&data =
&&&&&&&&p-&next = NULL;
&&&&&&&&q-&next =
&&&&&&&&q = q-&
&&& return
//打印单链表
void print_list(struct listnode *head)
&&& struct listnode *p;
&&& p = head-&
&&& while(p)
&&&&&&&&printf(&%d &,p-&data);
&&&&&&&&p = p-&
//删除单链表的第i个节点
struct listnode *delete_list(struct listnode *head, int i)
&&& int j = <font color=#;
&&& struct listnode *p,*q;
&&&while(p && j & i-<font color=#)
&&&&&&&&p = p-&
&&&&&&&&j++;
&&& if( !p-&next || j & i -<font color=#)
&&&&&&&&exit(<font color=#);
&&& q = p-&
&&& p-&next = q-&
&&& free(q);
&&& return&&
int main()
&&& struct listnode *
&&& //head = NULL;
&&& printf(&输入链表的长度: &);
&&& scanf(&%d&,&n);
&&& head = create(n);&&& //&&& 创建长度为n的单链表
&&& printf(&\n输出单链表的值:\n&);
&&& print_list(head);
&&& printf(&\n输入要删除节点的位置:&);
&&& scanf(&%d&,&del);
&&& head = delete_list(head,del);
&&& printf(&\n删除后的单链表为:\n&);
&&& print_list(head);
&&& return <font color=#;
我们都在路上。。。。。
来 自:流年
等 级:贵宾
威 望:82
帖 子:2768
专家分:14477
删除第i个节点只需要将第i-1个节点和第i+1个节点相连
一片落叶掉进了回忆的流年。
等 级:ID已被封
威 望:30
帖 子:2976
专家分:7697
将就你的程序改了一下,头节点链表的表头一般不放数据,哑节点。
当条件不满足时,用Exit(0)直接退出让人感到莫明妙。
这时链表分配的节点内存还没有释放你就退出了。
其它问题自己思考修改吧。
[此贴子已经被作者于 11:49编辑过]
我们都在路上。。。。。
来 自:北京
等 级:论坛游民
帖 子:20
专家分:24
非常感谢楼上。
另外对于头结点与头指针我还有点不懂。struct listnode *这个head是头结点呢?还是头指针呢?是不是只要分配了内存空间就代表是头结点啊?就像下面这个语句。
head = (struct listnode *)malloc(sizeof(struct listnode));
不要嘲笑我家樱木!
来 自:北京
等 级:论坛游民
帖 子:20
专家分:24
回复 5楼 诸葛欧阳
那如果没有头结点,删除第一个节点,就没办法用你说的那个了。另外怎么样才算是有头结点啊?我现在还不明白我这个程序是否有头结点?一直搞不懂......是不是就是head = (struct listnode *)malloc(sizeof(struct listnode));这样才算是有头结点啊?
不要嘲笑我家樱木!
等 级:ID已被封
威 望:30
帖 子:2976
专家分:7697
以下是引用chenxd在 18:42:05的发言:
非常感谢楼上。
另外对于头结点与头指针我还有点不懂。struct listnode *这个head是头结点呢?还是头指针呢?是不是只要分配了内存空间就代表是头结点啊?就像下面这个语句。
head = (struct listnode *)malloc(sizeof(struct listnode));
头指针啊,指针指向分配了节点大小内存。我们称第一个节点为头节点,你程序的问题是执行上面这条语句后,head=NULL,直接让刚分配的头节点内存丢失,再也无法释放。
我们都在路上。。。。。
等 级:ID已被封
威 望:30
帖 子:2976
专家分:7697
以下是引用chenxd在 18:46:16的发言:
那如果没有头结点,删除第一个节点,就没办法用你说的那个了。另外怎么样才算是有头结点啊?我现在还不明白我这个程序是否有头结点?一直搞不懂......是不是就是head = (struct listnode *)malloc(sizeof(struct listnode));这样才算是有头结点啊?
俺并木有修改你的删除代码,带头节点的链表一共有n+1个节点,比如创建3个大小长度的链表,实际有4个节点,头节点占一个,你的创建程序把第一个数据1的节点接到head上,压根就没有头节点,虽然也生成了链表,但删除第一个节点用那段代码就删除不到一节点,除非你自己修改代码处理删除第一个节点的情况。
或许你对指针和动态分配内存理解还有模糊的地方,慢慢来吧,拿笔画画链表,继续写插入,排序,释放链表的代码,总有一天会顿悟的。
[此贴子已经被作者于 19:57编辑过]
我们都在路上。。。。。
版权所有,并保留所有权利。
Powered by , Processed in 0.043670 second(s), 8 queries.
Copyright&, BCCN.NET, All Rights Reserved#include&cstdio&
#include&cstdlib&
typedef struct DoubleLinkedList
struct DoubleLinkedList *
struct DoubleLinkedList *
}DlinkedList_N
//建立链表
DlinkedList_Node* createDLink()
DlinkedList_Node *head,*p,*s;
head = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));
printf("please input the data: \n");
scanf("%d",&x);
if(x != 65535)
s = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));
s -&data =
printf("\n数据输入结束\n");
p-&next = NULL;
head = head -&
head-&pre = NULL;
//顺序、反序打印链表
void printDLink(DlinkedList_Node *head)
DlinkedList_Node *p,*s;
printf("正序输出双向链表:\n");
printf("%d ",p-&data);
printf("\n 逆序输出双向链表: \n");
printf("%d ",s-&data);
printf("\n \n");
//删除一个结点
DlinkedList_Node* deleteDlinkedList_Node(DlinkedList_Node *head,int i)
DlinkedList_Node *p;
if(p-&data == i)
head = p-&
head-&pre = NULL;
if(p-&data == i)
p-&pre-&next = p-&
p-&next-&pre = p-&
printf("没有找到想要删除的数据\n");
//插入一个结点
DlinkedList_Node* insertDlinkedList_Node(DlinkedList_Node *head,int i)
DlinkedList_Node *p,*
temp = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));
temp -&data =
if(i & p-&data)//比头结点数据小,插入到链表头部
head-&next =//此处p为原来的head
head-&pre = NULL;
p-&pre =//此处p为原来的head
while(p != NULL && i & p-&data)//寻找合适的插入位置
if(i & p-&data)//在链表中间某处找到合适插入位置
temp -&next =
temp -&pre = p-&
p -&pre-&next =
else//没有找到合适的位置,只有将数据插入到链表尾部
//遍历到链表尾部,p==NULL
temp -&pre =
temp -&next = NULL;
int main()
DlinkedList_Node *
head = createDLink();
printDLink(head);
head = insertDlinkedList_Node(head,1012);
head = deleteDlinkedList_Node(head,1991);
printDLink(head);
/*****************************
运行结果如下:
please input the data:
please input the data:
please input the data:
please input the data:
please input the data:
please input the data:
please input the data:
数据输入结束
正序输出双向链表:
逆序输出双向链表:
420 512 92 1991
正序输出双向链表:
逆序输出双向链表:
420 512 92 1012
******************************/
阅读(...) 评论()C语言实现输出链表中倒数第k个节点
投稿:shichen2014
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了C语言实现输出链表中倒数第k个节点,主要涉及链表的遍历操作,是数据结构中链表的常见操作。需要的朋友可以参考下
本文实例展示了C++实现输出链表中倒数第k个节点的方法,分享给大家供大家参考之用。
运行本文所述实例可实现输入一个单向链表,输出该链表中倒数第k个节点。
具体实现方法如下:
* Copyright (c) 2011 alexingcool. All Rights Reserved.
#include &iostream&
int array[] = {5, 7, 6, 9, 11, 10, 8};
const int size = sizeof array / sizeof *
struct Node
Node(int i = 0, Node *n = NULL) : item(i), next(n) {}
Node* construct(int (&array)[size])
Node *head = &
for(int i = 0; i & i++) {
Node *temp = new Node(array[i]);
head-&next =
return dummy.
void print(Node *head)
while(head) {
cout && head-&item && " ";
head = head-&
Node* findKnode(Node *head, int k)
Node *pKnode =
if(head == NULL) {
cout && "link is null" &&
return NULL;
while(k--) {
if(head == NULL) {
cout && "k is bigger than the length of the link" &&
return NULL;
head = head-&
while(head) {
head = head-&
pKnode = pKnode-&
void main()
Node *head = construct(array);
cout && "source link: ";
print(head);
Node *kNode = findKnode(head, 5);
if(kNode != NULL)
cout && "the knode is: " && kNode-&item &&
测试用例如下:
1. NULL Link
&&& head = NULL;
2. normal Link, with normal k
&&& k &= len(head);
3. normal Link, with invalid k
&&& k & len(head)
希望本文所述对大家C程序算法设计的学习有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具博客访问: 141363
博文数量: 43
博客积分: 451
博客等级: 下士
技术积分: 880
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: C/C++
查看xml文档:
another text
another text 2
删除value2这个节点:
if ( !xmlStrcmp( node->name, (const xmlChar*)"value2") )
xmlNodePtr node_to_del =
node = node->
xmlUnlinkNode( node_to_del );
xmlFreeNode( node_to_del );
再次查看xml文档:
another text 2
留下空行,相当别扭啊。
讲讲xml文档的结构先,其肉眼没看见的结构如下(以value1、value2、value3为例):
文本节点 ( "
value1 节点
文本节点 ( "
value2节点
文本节点 ( "
value3节点
当使用xmlUnlinkNode删除value2节点时,xml文档剩下如下内容:
文本节点 ( "
value1 节点
文本节点 ( "
文本节点 ( "
value3节点
文本节点 ("
") 注意上面的两行文本节点,这就是产生空白行的原因。我们对症下药,删除value2节点前面那个文本节点即可:
xmlUnlinkNode(cur); /* delete the value2 node */
xmlFreeNode(cur);
cur = tmpP
tmpPtr = cur->
xmlUnlinkNode(tmpPtr); /* delete text node */
xmlFreeNode(tmpPtr);
xmlSaveFile("test.xml", doc);/* 注意要保存好文档 */
参考:http://permalink.gmane.p.gnome.lib.xml.general/9920
阅读(1822) | 评论(1) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
CU新的风格太难弄了,里面的格式有误,不好修改。
请登录后评论。

我要回帖

更多关于 c语言怎么删除字符 的文章

 

随机推荐