c语言链表删除节点问题

问题描述

c语言链表删除节点问题
#include
#define NUll 0
#include
struct stu
{
char name[6];
int xuehao;
int grade;
struct stu next;
};
typedef struct stu STU;
main()
{
STU *p*p_start*p2*p_print*p_charu*p_charu2;
int ixuehao;
//输入
for (i=0;i {
p=(STU *)malloc(sizeof(STU));
printf(""请输入学生姓名:n"");
scanf(""%s""&p->name);
printf(""请输入学生的学号:n"");
scanf(""%d""&p->xuehao);
printf(""请输入学生的成绩:n"");
scanf(""%d""&p->grade);
if (i==0)
p2=p_start=p;
else
{
p2->next=p;
p2=p;
if (i==2)
p->next=NULL;
}
}
// printf(""%d""p_start->next->next->next);
// printf(""%d""p_start->next->next->xuehao);
//链表的插入
p_charu=p_start;
printf(""请输入要删除的学号n"");
scanf(""%d""&xuehao);
while (1)
{
p_charu2=p_charu->next;
if (p_charu->next->xuehao==xuehao)
{
// if (p_charu->next->next==NULL)
// p_charu->next=NULL;
else
p_charu->next=p_charu->next->next;
free(p_charu2);
break;
}
if (p_charu->xuehao==xuehao)
{
p_start=p_charu->next;
free(p_charu);
break;
}
p_charu=p->next;
}
//输出
p_print=p_start;
/

for (i=0;i {
printf(""学生姓名为:%sn""p_print->name);
printf(""学生的学号为:%dn""p_print->xuehao);
printf(""学生的分数为:%dn""p_print->grade);
if (p_print->next!=NULL)
p_print=p_print->next;
}
*/
}
这是一个删除结点并输出的程序,
加注释的输出部分没有问题,关键应该在删除结点的循环那里,但是找不见错误啊,
错误为:当删除最后一个链表也就是3时程序发生错误,停止运行。

求解

解决方案

要贴到源代码的两个引号里面去

解决方案二:
不要这样贴代码,应该选择工具栏上的代码,然后再贴。
你这样贴的代码丢了很多字符,没法直接调试。

解决方案三:
贴了两次没发现贴的一样,include后面、for循环里面等等都丢字符了么

解决方案四:
已经在http://ask.csdn.net/questions/159597这个帖子详细回复了

解决方案五:
```#include
#define NUll 0
#include
struct stu
{
char name[6];
int xuehao;
int grade;
struct stu next;
};
typedef struct stu STU;
main()
{
STU *p*p_start*p2*p_print*p_charu*p_charu2;
int ixuehao;
//输入
for (i=0;i {
p=(STU *)malloc(sizeof(STU));
printf(""请输入学生姓名:n"");
scanf(""%s""&p->name);
printf(""请输入学生的学号:n"");
scanf(""%d""&p->xuehao);
printf(""请输入学生的成绩:n"");
scanf(""%d""&p->grade);
if (i==0)
p2=p_start=p;
else
{
p2->next=p;
p2=p;
if (i==2)
p->next=NULL;
}
}
// printf(""%d""p_start->next->next->next);
// printf(""%d""p_start->next->next->xuehao);
//链表的插入
p_charu=p_start;
printf(""请输入要删除的学号n"");
scanf(""%d""&xuehao);
while (1)
{
p_charu2=p_charu->next;
if (p_charu->next->xuehao==xuehao)
{
// if (p_charu->next->next==NULL)
// p_charu->next=NULL;
else
p_charu->next=p_charu->next->next;
free(p_charu2);
break;
}
if (p_charu->xuehao==xuehao)
{
p_start=p_charu->next;
free(p_charu);
break;
}
p_charu=p->next;
}
//输出
p_print=p_start;
/

for (i=0;i {
printf(""学生姓名为:%sn""p_print->name);
printf(""学生的学号为:%dn""p_print->xuehao);
printf(""学生的分数为:%dn""p_print->grade);
if (p_print->next!=NULL)
p_print=p_print->next;
}
*/
}

解决方案六:
@苏小喵
请移步

http://ask.csdn.net/questions/159593

时间: 2024-08-22 14:27:07

c语言链表删除节点问题的相关文章

c-关于链表删除节点及打印的两个错误

问题描述 关于链表删除节点及打印的两个错误 //遍历一般线性表,删除位于键值为负节点之后的所有节点 #include #include #include #include struct example { int input; struct example* next; }; typedef struct example EXAMPLE; void creat (EXAMPLE* head); void remove (EXAMPLE* head); void print (EXAMPLE* h

单链表-删除链表的节点出现问题

问题描述 删除链表的节点出现问题 我创建了一个链表,链表的第一个节点不是空白的.我想用free函数删除第一个节点,发现出错.删除其他的节点没有问题.不知道什么缘故. 解决方案 你的链表怎么定义的? 是 typeof struct Node { Node next; } Node * head这样定义的么? 那么你删除首节点要这么做 prehead = head; head = head->next; free(prehead); 解决方案二: 删除节点时: Node *s,*p; s=p->n

结构体链表-c语言链表,输入输出正确但是删除操作报错

问题描述 c语言链表,输入输出正确但是删除操作报错 #include#include#define len sizeof(struct student) struct student{int num;float score;struct student *next;}; struct student *creat(void) // 建立{struct student *p1*p2*head;int n=0;p1=p2=(struct student *)malloc(len);scanf("&q

删除链表的节点,然后再查询,编号会错误

问题描述 删除链表的节点,然后再查询,编号会错误 #include #include #include #include using namespace std; typedef int datatype; typedef struct person { datatype score; char name[20]; int number; struct person *llink, *rlink; }PE; void display(PE *head) { PE *p; cout << end

创建二叉树 二叉树如何删除节点操作教程_C 语言

复制代码 代码如下: // 二叉树.cpp : 定义控制台应用程序的入口点. // /* *二叉树作业 *2012.12.1 13:55 *Made By Karld Vorn Doenitz */ #include "stdafx.h" #include<iostream> #include<string> using namespace std; class TreeNode{//建立节点类 public: char num; TreeNode *leftc

c语言-关于C语言链表的一些问题,代码怎么都运行不成功跪求大神指点

问题描述 关于C语言链表的一些问题,代码怎么都运行不成功跪求大神指点 下面代码主要实现链表的创建,插入,删除,并且能将两个年龄递增链表进行合并成递减链表 然而在插入和删除操作中gets函数无法起作用,strcmp函数也出现位置冲突报错..功力不足实在解决不了..跪求大神解答..(感觉自己写的东西除了上面两个错误应该还有,但是因为位置冲突问题就只能编译到那个地方无法进行下去..我肉眼实在找不出来.. #include<stdio.h> #include<stdlib.h> #incl

C语言 链表

原文:C语言 链表 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能.到目前我只写了一半,先传上来,大家有兴趣的可以帮忙指正,谢谢 在vs2010上面编译运行无错误. 每天都会把我写的新代码添加到这个里面.直到此链表完成. ? #include "stdafx.h" #include "stdio.h" #include <stdl

c语言链表代码求大神解释

问题描述 c语言链表代码求大神解释 insert中的 while( next_node && new_node->data > next_node->data) 是什么意思? 还有excise是什么意思,干什么用的? 最近在学链表,学不清楚... #include #include #include "list.h" /* Create a new node containing the specified data, and return a poi

c语言链表节点删除问题求解

问题描述 c语言链表节点删除问题求解 这是一个删除结点并输出的程序,总共有三个链表错误为:当删除最后一个链表也就是3时程序发生错误,停止运行.求大家帮忙解解决! #include <stdio.h> #define NUll 0#include <stdlib.h>struct stu{ char name[6]; int xuehao; int grade; struct stu *next;};typedef struct stu STU;main(){ STU *p*p_st