#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. python中装饰器

    在介绍装饰器之前,要先了解装饰器的相关基础知识. 嵌套函数: 最后引入一个基本的装饰器的例子: __author__ = "YanFeixu" import time def ti ...

  2. Go语言学习之3 流程控制、函数

    主要内容: 1. strings和strconv使用2. Go中的时间和日期类型3. 指针类型4. 流程控制5. 函数详解 1. strings和strconv使用 //strings . strin ...

  3. WPF经典编程模式-MVVM示例讲解

    https://www.cnblogs.com/lvdongjie/p/5515962.html

  4. Lab 1-2

    Analyze the file Lab01-02.exe. Questions and Short Answers Upload the Lab01-02.exe file to http://ww ...

  5. Creating a Hadoop-2.x project in Eclipse

    Creating a Hadoop-2.x project in Eclipse hortonworks:MapReduce Ports http://docs.hortonworks.com/HDP ...

  6. Elasticsearch SQL

    es sql是一个X-pack组件 ,允许对es执行类似sql的查询,可以将Elasticsearch SQL理解为一个编译器,既能理解es,又能理解sql.可以通过利用es,实施大规模实时读取和处理 ...

  7. 用vivado实现4比特加法器

    `timescale 1ns / 1ps module add_4_beha( a, b, cin, sum ); :] a; :] b; input cin; output sum; :]a; :] ...

  8. 牛客练习赛30-A/C

    链接:https://ac.nowcoder.com/acm/contest/216/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K ...

  9. 二十一、MVC的WEB框架(Spring MVC)

    一.基于注解方式配置 1.首先是修改IndexContoller控制器类 1.1.在类前面加上@Controller:表示这个类是一个控制器 1.2.在方法handleRequest前面加上@Requ ...

  10. IDEA上传一个项目到github

    IDEA上传一个项目到github 只要3步 1. 2. 3. 4. 5.查看页面 上传成功... 详情:    https://blog.csdn.net/qq_27093465/article/d ...