C/C++培训
达内IT学院
400-996-5531
//
// main.c
// C语言单项链表
//
// Created by Pakho on 2017/5/16.
// Copyright © 2017年 Pakho. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h> // malloc 可以开辟指定大小的内存空间
#include <string.h>
// 是否需要补齐,要看开辟的总空间是不是最大成员变量所占内存的倍数
// 定义一个节点(node)的结构体
struct Student {
char name[20]; // 储存学生姓名
int age; // 储存学生年龄
struct Student * next;// 指向下一个节点
};
// 创建一个新节点并返回其地址
struct Student * newNode(){
// 开辟一个大小为 32 的地址空间,并强制转化为(struct Student *)的指针
struct Student * stu = (struct Student *)malloc(sizeof(struct Student));
printf("请输入姓名: \n");
// scanf("%s",&stu->name[0]);
scanf("%s",stu->name); // 给姓名赋值
printf("请输入年龄: \n");
// 参数表示后面对应的接受值类型,因为 age 是int 类型,所以要用 %d
scanf("%d",&stu->age); // 给年龄赋值
// stu-age 是等价于 (*stu).age
//printList("%p",&stu->age);
//printList("%p",&(*stu).age);
stu->next = NULL; // 默认尾指针 = NULL
return stu;
}
struct Student * creatList(){
struct Student * head = newNode();
struct Student * preNode = head; // 最开始的前指针为头指针
struct Student * node = newNode();
// 当新建的节点 age != 0 的时候,就串联到链表上去
while (node->age!=0) {
preNode->next = node; // 1.让前节点指向新节点
preNode = node; // 2.让最后一个节点成为心的节点
node = newNode(); // 3.继续创建新节点
}
return head;
}
void printList(struct Student * head){
printf("====================\n");
//struct Student * temp = head;
if(head != NULL){
printf("姓名:%s\n",head->name);
printf("年龄:%d\n",head->age);
printf("下一个节点地址:%p\n",head->next);
printList(head->next);
//temp = temp->next;
}
}
/*
查询
head 要查询的链表
node 是要查询的节点(只查询是否包含相同信息)
*/
int indexOfNode(struct Student * head,struct Student * node){
int index = 0;
// 创建一个中间变量,避免 head 指针随着 next 改变
struct Student * temp = head;
while (temp != NULL) {
if(strcmp(temp->name,node->name) == 0 && temp->age == node->age){
return index;
}
index++;
temp = temp->next;
}
return -1;
}
// 删除
void deleteNode(struct Student ** head,struct Student * node){
struct Student * temp = *head;
struct Student * prehead = *head ;
while (temp != NULL) {
if(strcmp(temp->name,node->name) == 0 && temp->age == node->age){
if (prehead == temp) {
*head = temp->next;
break;
}
prehead->next = temp->next;
break;
}
prehead = temp;
temp = temp->next;
}
}
// 删除2(没有二级指针)
struct Student * deleteNode2(struct Student * head,struct Student * node){
struct Student * temp = head;
struct Student * prehead = head ;
while (temp != NULL) {
if(strcmp(temp->name,node->name) == 0 && temp->age == node->age){
if (prehead == temp) {
head = temp->next;
return head;
}
prehead->next = temp->next;
return head;
}
prehead = temp;
temp = temp->next;
}
return head;
}
int changdu(struct Student * head){
int index = 0;
struct Student * temp = head;
while (temp->next!= NULL) {
temp = temp->next;
index++;
}
index++;
return index;
}
//插入
struct Student * insert(struct Student * head,struct Student * node,int a){
struct Student * temp = head;
struct Student * temp1 = head;
while (head != NULL) {
if (a<=1) {
node->next=head;
head = node;
return head;
}
else if (a>changdu(head)){
while (temp->next!=NULL) {
temp = temp->next;
}
temp->next = node;
node->next = NULL;
return head;
}else{
int i;
for (i=0; i<a; i++) {
temp1=temp;
temp = temp->next;
}
temp1->next = node;
node->next = temp;
return head;
}
}
return head;
}
struct Student * insertNodeAtIndex(struct Student * head,struct Student * node,int index){
struct Student * temp = head;
struct Student * preNode = head;
int i =1;
while (temp != NULL) {
if (index == 1) {
node->next = head;
return head;
}else if (index == i){
preNode->next = node;
node->next = temp;
return head;
}
preNode = temp;
temp = temp->next;
i++;
}
preNode->next=node;
return head;
}
int main(int argc, const char * argv[]) {
struct Student * stu = creatList();
int a = changdu(stu);
printList(stu);
printf("链表长度是%d\n",a);
while (1) {
//printf("请输入你要查询的节点信息:\n");
printf("请输入你要删除的节点信息:\n");
struct Student * node = newNode();
//int b = 0;
//scanf("请输入你要插入的位置%d",&b);
//printf("%d",b);
deleteNode(&stu, node);
//printList(stu);
//printList(insert(stu, node, 2));
//int ret = indexOfNode(stu,node);
//if(ret == -1){
// printf("没有你要查找的节点\n");
//}else{
// printf("你查找的节点在 %d 位置\n",ret);
// }
}
return 0;
}
填写下面表单即可预约申请免费试听!怕钱不够?可就业挣钱后再付学费! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有
Tedu.cn All Rights Reserved