问题9:编程实现一个单链表的建立/测长/打印以及结点的删除。
2013-09-06 20:35
741 查看
此题为一基本的数据结构方面的题目,主要考查单链表。
代码如下:
程序运行结果:
加入结点删除后的代码如下:
/**
* 功能:单链表创建、测长打印
* 作者:ahnselina
*/
#include <stdio.h>
typedef struct student
{
int data;
struct student *next;
}node;
//建立单链表
node *creat()
{
node *head, *p, *s;
int x, cycle = 1;
//建立头结点
head = (node*)malloc(sizeof(node));
p = head;
while(cycle)
{
printf("\nplease enter the data: ");
scanf("%d", &x);
if(x != 0)
{
//新建一个结点
s = (node*)malloc(sizeof(node));
s->data = x;
printf("\n %d", s->data);
p->next = s;
p = s;
}
else
cycle = 0;
}
head = head -> next;
p->next = NULL;
printf("\n链表第一个结点值为: %d", head->data);
return head;
}
//单链表测长
int length(node *head)
{
int len = 0;
node *p = head;
while(p != NULL)
{
len++;
p = p->next;
}
return len;
}
//单链表打印
void print(node *head)
{
node *p = head;
int n = length(head);
printf("\nNow, These %d records are:\n", n);
while(p != NULL)
{
printf("\n uuu %d ", p->data);
p = p->next;
}
}
//删除单链表一个结点
node *del(node *head, int num)
{
node *p1, *p2;
p1 = head;
while(num != p1->data && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->data)
{
if(p1 == head)
{
head = p1->next;
free(p1);
}
else
{
p2->next = p1->next;
free(p1);
}
}
else
printf("\n %d不存在", num);
return head;
}
int main(void)
{
printf("===========NOW CREAT A LIST=========\n");
node *p = creat();
printf("\n下面计算链表长度:");
int len = length(p);
printf("\n链表长度为: %d", len);
printf("\n下面打印该单链表:");
print(p);
node *q = del(p, 4);
printf("\n删除结点后的链表为:\n");
print(q);
return 0;
}运行结果:
PS:在我最开始的程序里面,编译的时候出现错误
error: stray '\243' in program
这是因为有非标ascII的字符,字符一般都是全角符号什么的比如{
} 和{} ,和,
注意切换下输入法就好了
代码如下:
/** * 功能:单链表创建、测长打印 * 作者:ahnselina */ #include <stdio.h> typedef struct student { int data; struct student *next; }node; //建立单链表 node *creat() { node *head, *p, *s; int x, cycle = 1; //建立头结点 head = (node*)malloc(sizeof(node)); p = head; while(cycle) { printf("\nplease enter the data: "); scanf("%d", &x); if(x != 0) { //新建一个结点 s = (node*)malloc(sizeof(node)); s->data = x; printf("\n %d", s->data); p->next = s; p = s; } else cycle = 0; } head = head -> next; p->next = NULL; printf("\n链表第一个结点值为: %d", head->data); return head; } //单链表测长 int length(node *head) { int len = 0; node *p = head; while(p != NULL) { len++; p = p->next; } return len; } //单链表打印 void print(node *head) { node *p = head; int n = length(head); printf("\nNow, These %d records are:\n", n); while(p != NULL) { printf("\n uuu %d ", p->data); p = p->next; } } int main(void) { printf("===========NOW CREAT A LIST=========\n"); node *p = creat(); printf("\n下面计算链表长度:"); int len = length(p); printf("\n链表长度为: %d", len); printf("\n下面打印该单链表:"); print(p); return 0; }
程序运行结果:
加入结点删除后的代码如下:
/**
* 功能:单链表创建、测长打印
* 作者:ahnselina
*/
#include <stdio.h>
typedef struct student
{
int data;
struct student *next;
}node;
//建立单链表
node *creat()
{
node *head, *p, *s;
int x, cycle = 1;
//建立头结点
head = (node*)malloc(sizeof(node));
p = head;
while(cycle)
{
printf("\nplease enter the data: ");
scanf("%d", &x);
if(x != 0)
{
//新建一个结点
s = (node*)malloc(sizeof(node));
s->data = x;
printf("\n %d", s->data);
p->next = s;
p = s;
}
else
cycle = 0;
}
head = head -> next;
p->next = NULL;
printf("\n链表第一个结点值为: %d", head->data);
return head;
}
//单链表测长
int length(node *head)
{
int len = 0;
node *p = head;
while(p != NULL)
{
len++;
p = p->next;
}
return len;
}
//单链表打印
void print(node *head)
{
node *p = head;
int n = length(head);
printf("\nNow, These %d records are:\n", n);
while(p != NULL)
{
printf("\n uuu %d ", p->data);
p = p->next;
}
}
//删除单链表一个结点
node *del(node *head, int num)
{
node *p1, *p2;
p1 = head;
while(num != p1->data && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->data)
{
if(p1 == head)
{
head = p1->next;
free(p1);
}
else
{
p2->next = p1->next;
free(p1);
}
}
else
printf("\n %d不存在", num);
return head;
}
int main(void)
{
printf("===========NOW CREAT A LIST=========\n");
node *p = creat();
printf("\n下面计算链表长度:");
int len = length(p);
printf("\n链表长度为: %d", len);
printf("\n下面打印该单链表:");
print(p);
node *q = del(p, 4);
printf("\n删除结点后的链表为:\n");
print(q);
return 0;
}运行结果:
PS:在我最开始的程序里面,编译的时候出现错误
error: stray '\243' in program
这是因为有非标ascII的字符,字符一般都是全角符号什么的比如{
} 和{} ,和,
注意切换下输入法就好了
相关文章推荐
- 基础笔试题:编程实现一个单链表的建立、测长、打印
- 实现一个单链表的建立、测长、打印、删除、插入
- 编程实现一个单链表实现创建、测长以及打印
- (2)单链表的操作 ① 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数
- 编程实现一个单链表的建立/测长/打印
- 线性表的存储结构优缺点及编程实现一个单链表的建立,测长,打印
- 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数将其返回。 ⑤ 建立两个
- 实现一个算法,删除单链表中间的某个结点,假定你只能访问该结点
- 编程实现一个单链表的打印
- 建立一个带附加头结点的单链表.实现测长/打印/删除结点/插入结点/逆置/查找中间节点/查找倒数第k个节点/判断是否有环
- 【链表】删除一个无头单链表的非尾节点 以及从尾到头打印单链表
- 实现一个单链表的建立、测长、打印,插入,排序,逆置
- [002]链表笔记--编程实现一个单链表的创建/测长/打印
- 使用JAVA实现单链表的建立、测长、打印、删除、插入、排序、逆置
- cc150:实现一个算法来删除单链表中间的一个结点,只给出指向那个结点的指针
- 笔试题:创建一个单链表,结点包含学生的学号,姓名,性别,年龄信息.写几个程序,实现按学生学号插入,查询,删除等操作.
- 编程实现一个单链表节点的删除
- 009实现一个算法来删除单链表中的一个结点,仅仅给出指向那个结点的指针(keep it up)
- 【Java】实现一个算法,删除单链表中间的某个结点,假定你只能访问该结点
- 《剑指offer》面试题57 删除链表中重复的结点 C++ 实现 以及 错误总结 (指针问题)!!