u011046042 于 2014.12.11 18:11 提问

/* 创建链表 */插入元素

5个回答

okcd00   2014.12.11 19:11

book.h

``````
// A collection of various macros, constants, and small functions
// used for the software examples.

// First, include all the standard headers that we need
#include <iostream>
#include <cstdlib>
#include <time.h>  // Used by timing functions

// Now all the standard names that we use
using std::cout;
using std::endl;
using std::string;
using std::ostream;

const int defaultSize = 10; // Default size

// Return true iff "x" is even
inline bool EVEN(int x) { return (x % 2) == 0; }

// Return true iff "x" is odd
inline bool ODD(int x) { return (x % 2) != 0; }

// Assert: If "val" is false, print a message and terminate
// the program
void Assert(bool val, string s) {
if (!val) { // Assertion failed -- close the program
cout << "Assertion Failed: " << s << endl;
exit(-1);
}
}

// Swap two elements in a generic array
template<typename E>
inline void swap(E A[], int i, int j) {
E temp = A[i];
A[i] = A[j];
A[j] = temp;
}
// Random number generator functions

inline void Randomize() // Seed the generator
{ srand(1); }

// Return a random value in range 0 to n-1
inline int Random(int n)
{ return rand() % (n); }

// Swap two integers
inline void swap(int& i, int& j) {
int temp = i;
i = j;
j = temp;
}

// Swap two char*'s
inline void swap(char* i, char* j) {
char* temp = i;
i = j;
j = temp;
}

// Big enough for simple testing
#define INFINITY 9999

// Timing variables and functions
unsigned tstart = 0;  // Time at beginning of timed section

// Initialize the program timer
void Settime() { tstart = (unsigned) clock(); }

// Return the elapsed time since the last call to Settime
double Gettime() {
unsigned tcurr = (unsigned) clock();
return (double)(tcurr - tstart)/(double)CLOCKS_PER_SEC;
}

// Your basic int type as an object.
class Int {
private:
int val;
public:
Int(int input=0) { val = input; }
// The following is for those times when we actually
//   need to get a value, rather than compare objects.
int key() const { return val; }
// Overload = to support Int foo = 5 syntax
Int operator= (int input) { val = input; return val; }
};

// Let us print out Ints easily
ostream& operator<<(ostream& s, const Int& i)
{ return s << i.key(); }
ostream& operator<<(ostream& s, const Int* i)
{ return s << i->key(); }

``````

`````` #ifndef LINK_H_INCLUDED
#include<iostream>
{
public:
E element;

{
element =elemval;
next = nextval;
}
{
next = nextval;
}
};

``````

list.h

`````` #ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
template<typename E> class List
{
private:
void operator =(const List&){}
List (const List&){}
public:
List(){}
virtual ~List(){}

virtual void clear() =0;

virtual void insert(const E& item) =0;

virtual void append(const E& item) =0;

virtual E remove() =0;

virtual void moveToStart() =0;

virtual void moveToEnd() =0;

virtual void prev() =0;

virtual void next() =0;

virtual int length() const =0;

virtual int currPos() const =0;

virtual void moveToPos(int pos) =0;

virtual const E& getValue() const =0;
};
#endif // LIST_H_INCLUDED

``````

Llist_for5.h

``````#ifndef LLIST_H_INCLUDED
#define LLIST_H_INCLUDED

#include"List.h"
#include"book.h"

template <typename E> class LList: public List<E>
{
private:
int cnt;

void init()
{
cnt=0;
}

void removeall()
{
tail->next=NULL;//删除指针时先将tail->next 赋值为NULL 其他不改动
{
delete curr;
}
}

public:
LList(int size=1){init();}

~LList(){ removeall();}

void print() const;

void clear() { removeall();init();}

void insert(const E& it)
{
if(tail == curr)tail = curr->next;
cnt++;
}

void append(const E& it)
{
cnt++;
}

E remove()
{
E it=curr->next->element;

if(tail==curr->next)
tail=curr;

curr->next=curr->next->next;
delete ltemp;
cnt--;
return it;

//  else return -1;

}

void moveToStart()

void moveToEnd()
{ curr=tail;}

void prev()
{
while(temp->next!=curr)temp=temp->next;
curr=temp;
}

void next()
{ curr=curr->next;}

int length()const {return cnt;}

int currPos() const
{
int i;
for(i=0;curr!=temp;i++)temp=temp->next;
return i;

}

void moveToPos(int pos)
{
for(int i=0;i<pos;i++)curr=curr->next;
}

const E& getValue() const
{
return curr->element;   // 变成读取当前的元素的值
//return curr->next->element;
}
};

#endif // LLIST_H_INCLUDED

``````

main.cpp

``````#include"LList_for5.h"
#include<iostream>
using namespace std;
int main()
{
LList<int> cycle;
int n,m,i,cur,cnt=0;
cin>>n>>m;
for(i=1;i<=n;i++) { cycle.append(i); }
// for(int i=0;i<= n;i++){cout<<cycle.getValue()<<endl;cycle.next();}
for(i=1;;)
{
if(cnt==n)break;
if(cycle.getValue()==0){cycle.next();}
if(i==m)
{

cycle.prev();
cur=cycle.remove();
cout<<cur<<endl;
cnt++;
i=0;
}
cycle.next();
i++;
}

return 0;
}

``````
u011046042 没事的啊 一起学习嘛！ 多交流 一起进步
3 年多之前 回复
okcd00 回复u011046042: 为了安全性习惯这么写了……那就全public掉吧…… 实在不好意思(●'◡'●) 我C刚学一小会儿就转C++了……
3 年多之前 回复
u011046042 哪个private 和 public 这些怎么解决呢？
3 年多之前 回复
okcd00 回复u011046042: 哦对了还有for(int i=0 .....) 这些循环里的int i移到for外面去，C里不支持for中声明定义临时变量
3 年多之前 回复
okcd00 回复u011046042: 那把命名空间的namespace std都去掉，然后cin、cout改成scanf和printf吧
3 年多之前 回复
u011046042 有没有C语言的 ？？ 这个是C++的吧！
3 年多之前 回复
u011767611   2014.12.15 18:01

`````` #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define err_exit(msg) (perror(msg),(exit(1)))
int i=0;
struct node {
unsigned char data;
int count;
};
{
p->data=data;
p->count=0;
p->next=NULL;
return p;
}
{
free(p);
}
{
//int i=0;
//p->count=i;
if(p->data==data)
{
//p->count=i;
printf("I find it at the pos[%d]\n",p->count);
return p;
}
}
}
/*pre insert*/
{
++i;
p->count=i;
}
{
/*'cause' p going to be ignoe or 'delete'
*turn the head to p->next point
*/
return ;
}
if(pre->next==p){
pre->next=p->next;
return ;
}
}
int main(void)
{
p=make_node('a');
insert(p);
p=make_node('2');
insert(p);
p=make_node('1');
insert(p);
p=make_node('b');
insert(p);
search('b');
return 0;
}

``````
zxh707wk   2014.12.11 19:52
zilaishuichina   2014.12.18 09:53

jinfengyiye   2014.12.18 12:46