Cracking The Coding Interview2.3
#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的更多相关文章
- Cracking The Coding Interview2.4
删除前面的linklist,使用node来表示链表 // You have two numbers represented by a linked list, where each node cont ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 《cracking the coding intreview》——链表
前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
随机推荐
- spring aop通过注解实现日志记录
首先是几个概念:连接点(Joinpoint).切点(Pointcut).增强(Advice).切面(Aspect) 另外也要使用到注解. 需求:通过注解定义LogEnable.然后程序运行能够识别定义 ...
- jquery如何获取checkbox的值
jquery如何获取checkbox的值 一.总结 一句话总结:就是通过jquery获取哪些对应name的checkbox,然后找出:check(被选中的),然后通过jquery的each遍历获取这些 ...
- 练习:将从表读出来的时间戳除以1000(java读时间戳会多出3个000)用jackson包 实现
练习:将从表读出来的时间戳除以1000(java读时间戳会多出3个000)jackson包 实现 entity @Entity @DynamicUpdate //自动更新日期 @Data //get/ ...
- 守护进程函数——内部的小范围try catch 增强了 while死循环执行的 可靠性
void Watch() { try { LogHelper.WriteLog("WatchServi ...
- (Gorails) activeStore模块,把一堆属性放在一个hash对象内。gem 'activerecord-typedstore'增强了store模块,更好用了
https://api.rubyonrails.org/classes/ActiveRecord/Store.html https://gorails.com/episodes/preferences ...
- android -------- Eclipse下的NDK配置环境
NDK 全称是Native Development Kit,是一个让开发人员在Android应用中嵌入使用本地代码编写的组件的工具集 原生开发工具包 (NDK) 是一组可让您在 Android 应用中 ...
- python生成随机整数
python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成 ...
- python记录_day16 类的成员
一.变量 1.实例变量(又叫字段.属性) 创建对象时给对象赋值 形式: self.xxx = xxx 访问: 对象名.xxx 只能由对象访问 class Person: def __init_ ...
- 【Java】【2】String和List相互转换
正文: 1,String转List //常见的为逗号分隔 String str = "a,b,c"; List<String> list1 = Arrays.asLis ...
- python-django rest framework框架之解析器
1.解析器 : 对请求的数据进行解析 - 请求体进行解析. 解析器在你不拿请求体数据时 不会调用. class UsersView(APIView): def get(self,request,*ar ...