#include <iostream>
#include <string>
using namespace std;
class linklist
{
private:
class node
{
public:
node(){}
string data;
node * next;
};
node *head;
int size;
public:
linklist()
{
head = new node;
size = 0;
}
/***整表创建***/
void Create(string *s,int size)
{
if (s==NULL || size == 0)
{
return;
} node *p = new node;
p->data = s[0];
head->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);
}
head = 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 = head;
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 =head;
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 = head;
node *pn = p->next;
while(t!=0)
{
p = p->next;
pn = pn->next;
t--;
}
p->next = pn->next;
delete pn;
size --;
} void Display()
{
if (head==NULL)
{
return;
}
node * p;
p=head->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()
{ } /*****Implement an algorithm to find the nth to last element of a singly linked list.*****/
string GetLastN(int n)
{
if (n<0||n>size)
{
return "Error: Wrong Number";
} int i = 0;
node *p = head->next;
node *q = head->next;
while(i<n)
{
p=p->next;
i++;
}
while(p!=NULL)
{
p=p->next;
q=q->next;
}
return q->data;
}
}; int main()
{
string str[8]={"sos","OMG","sos","OMG","OMG","OMG","fof","fof"};
linklist s; s.Create(str,8);
s.Display(); /*s.Insert(2,"bingo");
s.Display(); s.Delete(3);
s.Display(); cout<<s.Get(1)<<endl; s.Clear();
s.Display();*/ //s.RemoveDupulicate();
cout<<s.GetLastN(6);
//s.Display();
return 0;
}

Cracking The Coding Interview 2.2的更多相关文章

  1. Cracking the coding interview

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

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

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

  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 coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. legend2---开发日志7(vue的使用场景有哪些,或者说使用的优缺点)

    legend2---开发日志7(vue的使用场景有哪些,或者说使用的优缺点) 一.总结 一句话总结:任何页面都可以使用vue,尤其是有交互的或者用ajax的,但是vue插件及其它各种插件加载需要时间, ...

  2. linux 下设置定时任务

    Linux Crontab 定时任务 命令详解 在工作中需要数据库在每天零点自动备份所以需要建立一个定时任务.我选择在Linux下使用Crontab来添加定时任务执行shell文件.shell文件有数 ...

  3. Ribbon

    Ribbon是一个客户端的负载均衡器,它提供对大量的HTTP和TCP客户端的访问控制.Feign也是用的Ribbon,所以在这一章你也可以用@FeignClient Ribbon的一个核心概念是命名的 ...

  4. mismatch详解

    mismatch到底只是一个碱基替换,还是也包括了插入缺失? If two sequences in an alignment share a common ancestor, mismatches ...

  5. Python自学:第二章 使用制表符或换行符来添加空白

    print("Languages:\n\tPython\n\tC\n\tJava") 输出为: Languages: Python C Java

  6. uva10780

    将m分解质因数,然后计算次数取最小. #include <iostream> #include <cstdio> #include <cmath> #include ...

  7. 分离vue组件内部css

    当我们使用vue组件的时候,使用webpack打包的时候,默认会把vue组件内部的css打包到页面上,但是打包到页面上很丑陋,所以我们希望可以把vue组件内部的css抽离到css文件中,使用vue-s ...

  8. Vue之单文件组件的数据传递,axios请求数据及路由router

    1.传递数据 例如,我们希望把父组件的数据传递给子组件. 可以通过props属性来进行传递. 传递数据三个步骤: 步骤1:在父组件中,调用子组件的组名处,使用属性值的方式往下传递数据 <Menu ...

  9. D - Power Tower欧拉降幂公式

    题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...

  10. 百度地图API 自定义坐标点及图片

    var map = new BMap.Map("allmap");var point = new BMap.Point(105.955754,36.525109);map.cent ...