P330.9 实验报告 创建动态链表实现插入删除

#include<stdio.h>

#include<stdlib.h>

#define LEN sizeof(struct Student)

struct Student {  long num;  char name[20];  int age;  float score;  struct Student *next; };

int n;

struct Student *creat()

{  struct Student*head;

struct Student *p1,*p2;

n=0;

p1=p2=(struct Student*)malloc(LEN);

printf("Please enter the student num\t");

scanf("%d",&p1->num);

if (p1->num==0)

return head=NULL;

printf("Please enter the student name\t");  scanf("%s",p1->name);

printf("Please enter the student age\t");  scanf("%d",&p1->age);

printf("Please enter the student score\t");  scanf("%f",&p1->score);

head=NULL;

while(p1->num!=0)

{

n=n+1;

if(n==1)head=p1;

else p2->next=p1;

p2=p1;

p1=(struct Student*)malloc(LEN);

printf("Please enter the student num\t");

scanf("%d",&p1->num);

if (p1->num==0){    p2->next=NULL;    return head;   }

printf("Please enter the student name\t");

scanf("%s",p1->name);

printf("Please enter the student age\t");

scanf("%d",&p1->age);

printf("Please enter the student score\t");

scanf("%f",&p1->score);  }

}

void print(struct Student *head)

{

struct Student *p;

printf("\nNow,These %d records are:\n",n);

p=head;

if(head!=NULL)   do

{printf("%d\t%s\t%d\t%.1lf\n",p->num,p->name,p->age,p->score); p=p->next;   }

while(p!=NULL);

}

struct Student *insert(struct Student *head)

{

struct Student *p1,*p2,*p3;

p2=head;

p1=(struct Student*)malloc(LEN);

printf("Now you can insert new student\n");

printf("Please enter the student num\t");

scanf("%d",&p1->num);

printf("Please enter the student name\t");

scanf("%s",p1->name);

printf("Please enter the student age\t");

scanf("%d",&p1->age);

printf("Please enter the student score\t");

scanf("%f",&p1->score);

if(head==NULL)  {   head=p1;   p1->next=NULL;  }

else

{

while( (p1->num>p2->num)&&(p2->next!=NULL))   {    p3=p2;    p2=p2->next;   }

if(p1->num<p2->num)

{

if(head==p2)    {     head=p1;     p1->next=p2;    }

else    {     p3->next=p1;     p1->next=p2;    }

}

else

{    p2->next=p1;    p1->next=NULL;   }

}

n=n+1;

return head; }

struct Student *del(struct Student *head,long num)

{  struct Student *p1,*p2;

if(head==NULL)

{   printf("\nlist null\n");   return head;  }

p1=head;

while(num!=p1->num && p1->next!=NULL)

{   p2=p1;   p1=p1->next;  }

if(num==p1->num)

{   if(p1==head)    head=p1->next;

else    p2->next=p1->next;

printf("delete :%d\n",num);

n-=1;

free(p1);  }

else   printf("num not found\n");

return head;

}

void main()

{  struct Student *head;  int num;

head=creat();  print(head);

head=insert(head);  print(head);

printf("enter delete number\n");  scanf("%d",&num);

head=del(head,num);     print(head);

}

时间: 2025-01-23 11:19:21

P330.9 实验报告 创建动态链表实现插入删除的相关文章

头插法链表的基本操作:创建空链表,插入结点,遍历链表,求链表长度,查找结点,删除结点

1 #include <stdio.h> 2 #include <stdlib.h> 3 /* 4 头插法链表的基本操作:创建空链表,插入节点,遍历输出节点数据,求链表长度,查找结点,删除结点. 5 */ 6 typedef struct node 7 { 8 int data; 9 struct node * next; 10 }NODE; 11 //创建空链表 12 NODE * createList() 13 { 14 NODE * head = (NODE *)malloc

单链表的插入删除操作(c++实现)

下列代码实现的是单链表的按序插入.链表元素的删除.链表的输出 // mylink.h 代码 #ifndef MYLINK_H #define MYLINK_H #include<iostream> using namespace std; struct node { int data; node *next; }; class list { public: list() { head=NULL; }; void insert(int item); void del(int item); voi

单链表的插入 删除 及带环 问题

#include<stdio.h> #include<assert.h> #include<malloc.h> #include<stdlib.h> typedef int Datatype; typedef struct SListNode { Datatype data; struct SListNode*next; }SListNode; void Erase(SListNode*&pHead, SListNode *pos); void Pu

链表_有序链表(插入删除遍历)

插入的节点位置有两种情况,一是有previous节点,而是没有previous节点 //链结点 public class Link { public long dData; public Link next; public Link(long dd) { dData=dd; } public void displayLink() { System.out.print(dData+" "); } } public class SortedList { private Link first

c语言动态链表的创建

创建动态连链表就是将一个个节点连接起来 (1)动态生成节点 (2)输入节点数据 (3)将节点链在一起 例: typedef struct Data { char num[20]; char name[10]; char sex; float english; float chinese; float math; }; typedef struct Node { struct Data data;//结构体类型//结构体嵌套 struct Node* next;//结构体指针型 }node,*Pn

静态PAT、动态PAT实验报告

网络运维 静态PAT.动态PAT 实验报告姓名: 李军 班级: NTD1710 日期: 2017 年 1 月 5 日实验任务 配置要求:PC192.168.1.1通过防火墙动态PAT转换访问PC120.1.1.1Server通过防火墙静态PAT转换访问PC120.1.1.1思路及实验步骤 配置思路:第一步.首先配置终端设备PC机和Server服务器的IP地址第二步.配置SW3.ISP.R1设备的VLAN及IP,使其能够ping 通终端PC机IPSW3:port link-type accessp

操作系统第2次实验报告:创建进程

个人信息 姓名:郑楚杭 学号:201821121009 班级:计算1811 一.实验目的 熟练Linux创建进程fork操作. 二.实验内容 在服务器上用VIM编写一个程序:一个进程创建两个子进程. 查看进程树 查看进程相关信息 三.实验报告 1. 编写程序 2. 打印进程树 编译1的程序并运行: 打印进程树: 3. 解读进程相关信息 (1) ps -ef UID PID PPID C STIME TTY TIME CMD zhengch+ 31795 31794 0 23:29 pts/4 0

数据结构实验报告-实验一 顺序表、单链表基本操作的实现

实验一    顺序表.单链表基本操作的实现   l  实验目的 1.顺序表 (1)掌握线性表的基本运算. (2)掌握顺序存储的概念,学会对顺序存储数据结构进行操作. (3)加深对顺序存储数据结构的理解,逐步培养解决实际问题的编程能力. l  实验内容 1. 顺序表 1.编写线性表基本操作函数: (1)InitList(LIST *L,int ms)初始化线性表: (2)InsertList(LIST *L,int item,int rc)向线性表的指定位置插入元素: (3)DeleteList1

C语言:创建动态单向链表,创建完成后,输出每一个节点的数据信息。

// //  main.c //  dynamic_link_list // //  Created by ma c on 15/8/5. //  Copyright (c) 2015年 bjsxt. All rights reserved. //  要求:写一个函数建立有3名学生数据的动态单向链表,并输出链表中每个结点的所有内容. /* 建立动态链表的思想: 1.开辟一个新结点,并使p1,p2指向它: 2.读入一个学生数据给p1所指的结点: 3.head = NULL,n = 0; 4.判断读