#include <iostream>
#include <string>
using namespace std;
class linklist
{
private:
class node
{
public:
node(){}
string data;
node * next;
};
node *first;
int size;
public:
linklist()
{
first = new node;
size = 0;
}
/***整表创建***/
void Create(string *s,int size)
{
if (s==NULL || size == 0)
{
return;
} node *p = new node;
p->data = s[0];
first->next = p; for (int i = 1; i<size; i++)
{
node *t = new node;
t->data = s[i];
p->next = t;
p=p->next;
}
p->next = NULL;
this->size = size;
}
/***整表删除***/
void Clear()
{
for (int i = 1;i<size+1; i++)
{
Delete(i);
}
first = NULL;
size = 0;
} /***第i[i为1,2....size,下同]个元素前插入string型e***/
void Insert(int i, string e)
{
if (i<0 || i>size)
{
return;
}
int t = i - 1;
node *p = first;
node *pn = p->next;
while(t!=0)
{
p = p->next;
pn = pn->next;
t--;
} node *ee = new node;
ee->data = e;
ee->next = pn;
p->next = ee; size ++; } /****获取第i个元素的值,并返回该值**/
string Get(int i)
{
if (i<=0 || i>size)
{
return "Error: You input wrong num.";
}
int t = i;
node *p =first;
while(t!=0)
{
p = p->next;
t--;
}
return p->data;
} /***删除第i个元素***/
void Delete(int i)
{
if (i<0 || i>size)
{
return;
}
int t = i - 1;
node *p = first;
node *pn = p->next;
while(t!=0)
{
p = p->next;
pn = pn->next;
t--;
}
p->next = pn->next;
delete pn;
size --;
} void Display()
{
if (first==NULL)
{
return;
}
node * p;
p=first->next;
/*for (int i =0; i<size; i++)
{
cout<<p->data<<" ";
p=p->next;
}*/ while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
} ~linklist()
{ }
}; int main()
{
string str[3]={"sos","OMG","fof"};
linklist s; s.Create(str,3);
s.Display(); s.Insert(2,"bingo");
s.Display(); s.Delete(3);
s.Display(); cout<<s.Get(1)<<endl; s.Clear();
s.Display(); return 0;
}

Cracking The Coding Interview 2.0 单链表的更多相关文章

  1. Cracking the Coding Interview:: 寻找有环链表的环路起始节点

    给定一个有环链表,实现一个算法返回环路的开头节点. 这个问题是由经典面试题-检测链表是否存在环路演变而来.这个问题也是编程之美的判断两个链表是否相交的扩展问题. 首先回顾一下编程之美的问题. 由于如果 ...

  2. Cracking The Coding Interview 9.0

    #include <iostream> #include <vector> using namespace std; void mswap(int &a, int &a ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  7. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  8. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  9. 《Cracking the Coding Interview》——第2章:链表——题目6

    2014-03-18 02:41 题目:给定一个带有环的单链表,找出环的入口节点. 解法1:用hash来检测重复节点肯定是容易想而且效率也高的好办法. 代码: // 2.6 You have a ci ...

随机推荐

  1. win10+cpu+tensorflow+pycharm

    1.安装64位的python3.5 选择windowsx86-64 executable installer安装 2.安装tensorflow cmd->进入到安装python的Scripts文 ...

  2. Linux中安装Mysql授权远程访问

    一.直接授权 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OP ...

  3. php 中输入输出提交

    </head> <body> 输出的两个位置 <? echo $_POST['sub']; ?> <form action="" meth ...

  4. 【JS】【4】字符串数字比较大小

    两个转换函数: parseInt():把值转换成整数 parseFloat():把值转换成浮点数 也有其他方法,详情请看参考博客,但个人认为转换函数是最好的方法 参考文档: 1,js.jquery字符 ...

  5. Docker Nginx 配置多个子域名

    参考:nginx server_name实用:配置多个子域名 在腾讯购置了域名服务,想直接配置二级域名映射到指定端口,发现腾讯不支持端口映射的方式. 想了一下,域名默认解析80端口,只能通过nginx ...

  6. vijos1448校门外的树

    描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:K=1,K=1,读入l.r表 ...

  7. Charles破解网站收藏(持续更新)

    1. 在这个网站(http://charles.iiilab.com/)下载破解文件 charles.jar 2. 替换掉原文件夹里的charles.jar Mac: /Applications/Ch ...

  8. PHP如何自定义PHP内置函数

    其实对于PHP程序员,有个纯PHP的解决方案.在php.ini里有个配置项 auto_prepend_file,可以设置一个PHP文件作为每次执行前自动加载的文件. 在这个文件里写函数,你就可以当成定 ...

  9. 使用机器学习检测TLS 恶意加密流——业界调研***有开源的数据集,包括恶意证书的,以及恶意tls pcap报文***

    2018 年的文章, Using deep neural networks to hunt malicious TLS certificates from:https://techxplore.com ...

  10. IIS隐藏版本号教程(Windows Server 2003)

    1.下载Urlscan https://www.microsoft.com/en-us/search/DownloadResults.aspx?q=URLScan(总下载页面) https://dow ...