链表的简单处理

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student{
long no;
float score;
struct Student *next;
};
int n;
//动态建立链表
struct Student *creat(void){
struct Student *head;
struct Student *p1,*p2;
n=0;
p1=p2=(struct Student *)malloc(LEN);
printf("请输入链表元素(两组数据之间以空格分开,以0结束):");
scanf("%ld %f",&p1->no,&p1->score);
head = NULL;
while(p1->no!=0){
n=n+1;
if(n==1){
head = p1;
}
else p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(LEN);
printf("请输入链表元素(两组数据之间以空格分开,以0结束):");
scanf("%ld %f",&p1->no,&p1->score);
}
p2->next=NULL;
return (head);
}
//打印链表
void print(struct Student *head){
struct Student *p;
printf("\nThese %d recodes are:\n",n);
p=head;
if(head!=NULL){
do{
printf("%ld %5.1f\n",p->no,p->score);
p=p->next;
}while(p!=NULL);
}
}
//删除链表中的某项
struct Student *delete(struct Student *head){
struct Student *p, *temp;int i=0,count;
printf("请输入删除位置:");
scanf("%d",&count);
if(count<=n){
if(head!=NULL){
do{
i++;
if(count==1&&i==count){
head = head->next;
p = head;
}else{
if(i==count){
temp = p->next;
p->next= temp->next;
n--;
free(temp);
} else{
if(i==1) p = head;
else p=p->next;
}
}
}while(p!=NULL);
}
}else{
printf("你输入的位置不存在!");
}
return (head);
}
//在链表中添加
struct Student *add(struct Student *head){
struct Student *p, *temp; int i=0,count;
printf("请输入插入位置:");
scanf("%d",&count);
if(count<=n){
temp = (struct Student *)malloc(LEN);
if(head!=NULL){
do{
if(count==0&&i==count){
printf("请输入链表元素:");
scanf("%ld %f",&temp->no,&temp->score);
temp->next=head;
head=temp;
n++;
}else{
if(i==count){
printf("请输入链表元素:");
scanf("%ld %f",&temp->no,&temp->score);
temp->next=p->next;
p ->next=temp;
n++;
p=temp;
} else{
if(i==0) p = head;
else p=p->next;
}
}
i++;
}while(p!=NULL);
}
}else{
printf("你输入的位置不存在!");
}
return (head);
}
//打印指定位置链表元素
void check(struct Student *head){
struct Student *p, *temp; int i=0,count;
printf("请输入制定位置:");
scanf("%d",&count);
if(count<=n){
printf("\nThe recode is:\n");
p = head;
if(head!=NULL){
do{
i++;
if(i==count){
printf("%ld %5.1f\n",p->no,p->score);
break;
}
p=p->next;
}while(p!=NULL);
}
}
else{
printf("你输入的位置不存在!");
}

}
menu(){
printf("======菜单======\n");
printf("1.打印链表元素\n");
printf("2.打印指定位置的元素\n");
printf("3.插入元素(在输入的位置之后)\n");
printf("4.删除元素\n");
printf("5.重置链表\n");
printf("0.退出\n");
printf("================\n");
}

//主函数

int main()
{
struct Student *head;int j;
head=creat();
while(1){
menu();
scanf("%d",&j);
switch(j){
case 1: print(head);
break;
case 2: check(head);
break;
case 3: head=add(head);
break;
case 4: head=delete(head);
break;
case 5: head = creat();
break;
case 0: exit(0);
defalut :
printf("你的选择无效,请再次选择!");
break;
}
}
return 0;
}

时间: 2024-08-07 03:47:36

链表的简单处理的相关文章

数据结构概述&lt;3&gt;链表的简单应用

今天介绍两个链表的简单应用. 1.约瑟夫问题 假设有N个人决定选出一个领导人,方法如下:所有人排成一个圆圈,按顺序数数,每次数到第M个人出局,此时,他两边的人靠拢重新形成圆圈.问题是找出哪一个人将会是最后剩下的那个人.下列程序依次读入N和M,并给出最终结果. #include <stdlib.h> #include <stdio.h> typedef struct node* link; struct node { int item; link next; }; int main(

Linux 内核 链表 的简单模拟(2)

接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) for (pos = (head)->next; pos != (head);

循环双链表的简单实现

<span style="font-size:18px;"><strong>//代码为自己编写,可能有问题,欢迎大家留言指正!</strong></span> <span style="font-size:18px;"><strong> </strong></span> #include <iostream> #include <string> u

链表的简单实现

1 #include <cstdio> 2 #include <cassert> 3 4 typedef struct node { 5 int data; 6 node * next; 7 }Node, *pNode; 8 9 pNode CreateList() { 10 pNode head, p1, p2; 11 p1 = new Node; 12 printf("Please input data: \n"); 13 scanf("%d&qu

c# 单链表实现 简单示例(可复制直接运行)

最近学习数据结构,发现c# 其实和c 的链表的实现差不多的 下面是一段可直接运行的代码 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Threading; 5 6 namespace SingleLinkedList 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 13 //实例调用

C++链表-最简单

struct student { long num; float score; struct student *next; }; 注意:只是定义了一个struct student类型,并未实际分配存储空间.只有定义了变量才分配内存单元. #include<iostream> using namespace std; int main() { struct student a,b,c,*head,*p; a.num = 99101; a.score = 89.5; b.num = 99103;

数据结构和算法--链表一之单向链表的简单实现

链表在我们java中也是一种基础的数据结构,可以理解成是一种和数组同级的数组结构,正如我们所知,在我们使用这集合ArrayList和LinkedList的时候,总会学习底层数组实现的ArrayList和双向链表实现的LinkedList的区别.在这里,我们将要讲说的是单向链表的简单实现,让我们体会一下链表在实现增删改查的时候是怎么样的一个操作,在和前边涉及到的数组的增删改查进行对比,得到我们学习的结论,数组的增删效率低于链表结构,查改效率高于链表结构! 什么叫做单向链表,我们可以理解为一个一个节

C链表的简单案例

此案例只是简单的使用链表 链表的特点: 1.不需要提前知道要存入数据的长度 2.最后结点为NULL 3.头结点指向下一个结点的结构体指针 #include <stdio.h> #include <process.h> struct Student{ char cName[20]; //姓名 int iNumber; //学号 struct Student *pNext; //指向下一个结点的地址 }; int iCount; //全局变量,用来表示链表长度 //创建链表返回链表的头

侵入式单链表的简单实现

通常情况下,单链表的定义是这样子滴, typedef struct foo_s { int data; struct foo_s *next; } foo_t; 结构体里包含了链表指针next; 而侵入式单链表却不同,让结构体包含一个通用的链表.看起来是这个样儿滴, typedef struct list_s { struct list_s *next; } list_t; typedef struct foo_s { int data; list_t link; } foo_t; 所有包含了l

单链表的简单操作

单链表是一种最简单的线性表的链式存储结构,单链表又称线性链表,每个数据元素用一个结点来存储,结点分为存放数据元素的data和存放指向链表下一个结点的指针next. 链表实现:(LinkList.h) //单链表 #ifndef LINKLIST_H_ #define LINKLIST_H_ #include <iostream> //using namespace std; template <typename T> struct Node { //数据成员 T data; Nod