#include <iostream>
#include <string>
using namespace std;
class linklist
{
private:
class node
{
public:
node(){}
string data;
node * next;
}; int size;
public:
node *head; 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 delete a node in the middle of a single linked list, given only access to that node.
//
// EXAMPLE
//
//Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothing is returned, but the new linked list looks like a->b->d->e //未考虑c为尾节点的情况
void DeleteNode(node *c)
{
if (c==NULL || c->next == NULL)
{
return;
} node *cn = c->next;
string cc = cn->data;
c->next = cn->next;
delete cn;
c->data = cc;
}
}; 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.DeleteNode(s.head->next->next->next); s.Display();
return 0;
}

Cracking The Coding Interview2.3的更多相关文章

  1. Cracking The Coding Interview2.4

    删除前面的linklist,使用node来表示链表 // You have two numbers represented by a linked list, where each node cont ...

  2. Cracking the coding interview

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

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

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

  4. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  5. 《cracking the coding intreview》——链表

    前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...

  6. Cracking the Coding Interview(Trees and Graphs)

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

  7. 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 ...

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

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

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

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

随机推荐

  1. java调用dll/so文件

    大家都知道用C++编写的程序如果用于windows使用则编译为xxx.dll文件,如果是Linux使用则编译为libxxx.so文件.下面将java调用dll/so文件的方法粘出来方便下次使用.此处使 ...

  2. idea javamaven项目 连接sqlserver 数据库方法

    这里用的是c3p0连接数据库 1.pom文件写法: <!-- 数据库连接池 --> <dependency> <groupId>com.mchange</gr ...

  3. javascript ----> Immediately-Invoked Function Expression (IIFE)(翻译)

    http://benalman.com/news/2010/11/immediately-invoked-function-expression/ 如果你没有注意到,我对术语有一点点坚持. 所以,在听 ...

  4. Python Web简单加法器的实现--Python

    坚持写博客来记录学习过程,哪怕学习的东西多么简单!下面是python中cgi相关知识. Template.py:(模板引擎文件) #模板引擎def start_response(resp=" ...

  5. android -------- java虚拟机和Dalvik虚拟机

    java虚拟机 虚拟机是一种抽象化的计算机,通过在实际的计算机上仿真模拟各种计算机功能来实现的.Java虚拟机有自己完善的硬体架构,如处理器.堆栈.寄存器等,还具有相应的指令系统.Java虚拟机屏蔽了 ...

  6. dcoker 安装mysql和数据持久化

    # mysql 镜像下载 docker pull mysql ( 版本自己选择)   # run 服务 docker run --name mysql.5.5 -p 3306:3306 -e MYSQ ...

  7. yum源仓库搭建

    系统:centos7 一.安装nginx yum install -y nginx yum  install -y createrepo   安装建yum源仓库的工具,可以用来建立yum仓库yum  ...

  8. ADG配置(主备库环境)

    @font-face { font-family: "Courier New"; }@font-face { font-family: "宋体"; }@font ...

  9. C# 语法特性 - 匿名方法(C#2.0)

    概述 匿名方法的本质其实就是委托. 编译后会生成委托对象,生成方法,然后把方法装入委托对象,最后赋值给声明的委托变量. (匿名方法可以省略参数:编译的时候会自动为这个方法按照委托签名的参数添加参数) ...

  10. [CodeForces - 447A] A - DZY Loves Hash

    A - DZY Loves Hash DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert ...