链表操作 big bang
来源:互联网 发布:蘑菇街和淘宝一样吗 编辑:程序博客网 时间:2024/04/30 03:45
题目描述
复习考研累了的时候看看一集二十分钟左右的《生活大爆炸》也不失为一种乐趣。在剧中Sheldon可以说是一个极品,真不知Leonard是如何忍受这位极品室友成天的唠叨。
你知道么?Sheldon有一个神秘的小本本,记录了所有他从小开始讨厌的人名。Stuart这位漫画店老板就是小本本的一员哦,谁叫他常常毫不客气地挤兑Sheldon,曾多次赌赢过Sheldon呢。
Penny是一个漂亮的女孩,好奇心也很强。为了满足她的好奇心,我当回编剧让她意外知道了Sheldon的那个小本本放在了哪里。于是她几乎每天都去看,看看上面有哪些人。但是那个小本本上的人名实在太多。要知道她可是没上过大学在饭店里面当服务员啊。请聪明的你帮帮她处理处理那个小本本吧。
图1:《生活大爆炸》里的角色
Sheldon每天都会在小本本里记录些人名,当然有时也会与他们和好就会从小本本中将这个人名删除。我们假设Sheldon会在一个空的小本本上插入、删除、查询某个人。
要帮助Penny,你需要知道一个顺序表是怎么初始化、插入、删除以及查找的。下面我就将这些算法列举在下方。
图2:线性表的动态分配顺序存储结构以及初始化
图3:线性表的插入算法
图4:线性表的删除算法
图5:线性表的查找算法
输入
输入数据只有一组,有很多行。每行的格式可能是下列一种:
insert a name
delete name
show
search name
其中 a 是一个整数,代表在第a个名字前插入名字。name是一个姓名,只包含英文字母的大小写,每个名字不超过30个字符。
输入保证不会插入列表中已经存在的姓名,不会删除列表中不存在的姓名,也不会搜索列表中不存在的姓名。
输出
起始时,列表是空的。只输出show和search name 的结果。show将列表中的姓名全部输出,search只输出找到该名字的序号(从1开始)。每次输出占一行,姓名间用空格隔开。如果列表中没有名字了,show时也要输出一个空行。
样例输入
样例输出
简单的链表操作
#include<stdio.h>#include<string.h>#include<malloc.h>#define NULL 0#define len sizeof(struct stu)char s[10];struct stu{ char name[31]; struct stu *next;};struct stu *ins(struct stu *head,int num,struct stu *head1){ struct stu *p1; if(head==NULL){ head=head1; head1->next=NULL; return head; } int n; n=1; p1=head; if(head!=NULL&&num==1){ head1->next=p1; head=head1; return head; } while(p1->next!=NULL&&n!=num-1){ p1=p1->next; n++; } if(n==num-1){ head1->next=p1->next; p1->next=head1; } return head;};void print(struct stu *head){ struct stu *p; int i; if(head==NULL){ printf("\n"); } else{ p=head; while(p!=NULL){ for(i=0;i<strlen(p->name);i++){ printf("%c",p->name[i]); } p=p->next; if(p!=NULL) printf(" "); } printf("\n"); }}void se(struct stu *head){ struct stu *p; char b[31]; scanf("%s",b); int n=1; p=head; while(p!=NULL){ if(strcmp(p->name,b)==0){ printf("%d\n",n); break; } p=p->next; n++; }}struct stu *del(struct stu *head){ struct stu *p1; struct stu *p2; char b[31]; scanf("%s",b); if(head==NULL){ return head; } p1=head; while(p1->next!=NULL&&strcmp(p1->name,b)!=0){ p2=p1; p1=p1->next; } if(strcmp(p1->name,b)==0){ if(p1==head){ head=p1->next; } else{ p2->next=p1->next; } free(p1); p1==NULL; } return head;};int main(){ struct stu *head; struct stu *head1; head=NULL; int num; char b[20]; while(~scanf("%s",s)){ if(strcmp(s,"insert")==0){ head1=(struct stu *)malloc(len); scanf("%d %s",&num,&head1->name); head=ins(head,num,head1); //print(head); } else if(strcmp(s,"show")==0){ print(head); } else if(strcmp(s,"search")==0){ se(head); } else if(strcmp(s,"delete")==0){ head=del(head); } } return 0;}
- 链表操作 big bang
- HNCU1325: 算法2-3~2-6:Big Bang(静态链表)
- The big bang theory!
- The Big Bang Theory
- the big bang
- The big bang S7E1
- 仿Big-Bang实现
- HNCU1325:算法2-3~2-6:Big Bang (线性表)
- A Big Bang to Me
- 【数据结构】-线性表-顺序表-1325: 算法2-3~2-6:Big Bang【麻烦】
- Lyric of The Big Bang Theory Theme
- uva11721 Instant View of Big Bang
- UVa 11721 Instant View of Big Bang
- Some Practical Oral English come from 《The Big Bang》
- LightOJ 1108 - Instant View of Big Bang(SPFA)
- 1325:算法2-3~2-6:Big Bang
- Acm Club 1325:算法2-3~2-6:Big Bang
- LightOJ - 1108 Instant View of Big Bang(负环)
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- Window 7-32下MySQL-5.7.11的安装
- 【GOF23设计模式】_观察者模式
- 一个word文档中,多个表格的批量调整(根据窗口调整表格和添加表格水平线)
- CSS -- file上传,多个浏览器统一样式
- 链表操作 big bang
- 【杭电oj】1166 - 敌兵布阵(线段树,树状数组)
- android开源的酷炫的交互动画和视觉效果:Interactive-animation
- 在Eclipse中使用JUnit4进行单元测试(中级篇)
- 【LeetCode-59】Spiral Matrix II
- linux下的抓包工具tcpdump用法
- Alcatraz的安装和使用(出自小码哥教育)
- Android roboguice 开源框架使用
- C++ 用libcurl库进行http通讯网络编程