c语言单项链表学习随笔

2018-06-18 04:03:26来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

//

//  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; }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:分块之区间修改与单点查询

下一篇:Project Euler——13.Larger Sum总结