元素-如何实现顺序表的各个功能?用C语言实现

问题描述

如何实现顺序表的各个功能?用C语言实现

/* 创建链表 */插入元素
删除元素 添加元素 获取元素 查找元素 清空链表 判断是否为空 判读是否已满

计算顺序表中元素的个数 急用

解决方案

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(); }

Link.h

 #ifndef LINK_H_INCLUDED
#define LINK_H_INCLUDED
#include<iostream>
template <typename E> class Link
{
public:
    E element;
    Link *next;

    Link(const E& elemval,Link* nextval =NULL)
    {
        element =elemval;
        next = nextval;
    }
    Link(Link* nextval = NULL)
    {
        next = nextval;
    }
};

#endif // LINK_H_INCLUDED

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"Link.h"
#include"book.h"

template <typename E> class LList: public List<E>
{
private:
    Link<E>* head;
    Link<E>* tail;
    Link<E>* curr;
    int cnt;

    void init()
    {
        curr=tail=head=new Link<E>(0);//将head指针所指的元素的值赋值为0
        cnt=0;
    }

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

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

    ~LList(){ removeall();}

    void print() const;

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

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

    void append(const E& it)
    {
      tail=tail->next=new Link<E>(it,head);//将tail->下一个指针指向head形成循环链表
      cnt++;
    }

    E remove()
    {
             Assert(head->next!=head,"NO element");
             E it=curr->next->element;
             Link<E>* ltemp=curr->next;

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

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

           //  else return -1;

    }

    void moveToStart()
    { curr=head;}

    void moveToEnd()
    { curr=tail;}

    void prev()
    {
         Link<E>* temp=head;
         while(temp->next!=curr)temp=temp->next;
         curr=temp;
     }

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

     int length()const {return cnt;}

     int currPos() const
     {
         Link<E>* temp=head;
         int i;
         for(i=0;curr!=temp;i++)temp=temp->next;
         return i;

     }

     void moveToPos(int pos)
     {
          curr=head;
          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;
}

解决方案二:

以前写的代码,要是差哪样功能的话,你解决不了的话再联系我:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define err_exit(msg) (perror(msg),(exit(1)))
typedef struct node * link;
static link head=NULL;
int i=0;
struct node {
    unsigned char data;
    int count;
    link next;
};
link make_node(unsigned char data)
{
    link p;
    p=(link)malloc(sizeof(link));
    p->data=data;
    p->count=0;
    p->next=NULL;
    return p;
}
void free_node (link p)
{
    free(p);
}
link search(unsigned char data)
{
    link p;
    //int i=0;
    for(p=head;p;p=p->next){
        //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*/
void insert(link p)
{
    p->next=head;
    head=p;
    ++i;
    p->count=i;
}
void delete(link p)
{
    link pre;
    if(p==head){
        /*'cause' p going to be ignoe or 'delete'
         *turn the head to p->next point
         */
         head=p->next;
         return ;
    }
    for(pre=head;pre;pre=pre->next)
        if(pre->next==p){
            pre->next=p->next;
            return ;
        }
}
int main(void)
{
    link p;
    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;
}

解决方案三:

参考代码:http://yun.baidu.com/s/17yIcQ

解决方案四:

我的博客 单链表

其他容器也有, 欢迎参考

解决方案五:

提供思路
删除元素,把前一个的next指向 后一个结点,把当前结点删除
添加,把新结点next指向当前的next结点,把当前位置结点next 指向新结点
获取
查找按next找
清空 把头next断掉
看头没有有next
没有满的说法吧
遍历可以查个数

时间: 2024-09-09 16:02:07

元素-如何实现顺序表的各个功能?用C语言实现的相关文章

如何在C++中建立一个顺序表_C 语言

准备数据 复制代码 代码如下: #define MAXLEN 100 //定义顺序表的最大长度struct DATA{ char key[10]; //结点的关键字  char name[20]; int age;};struct SLType //定义顺序表结构 { DATA ListData[MAXLEN+1];//保存顺序表的结构数组 int ListLen;   //顺序表已存结点的数量 }; 定义了顺序表的最大长度MAXLEN.顺序表数据元素的类型DATA以及顺序表的数据结构SLTyp

顺序表的实现示例

线性表的顺序实现例子 .h文件 此文件为方法 #ifndef SQLIST_H_INCLUDED #define SQLIST_H_INCLUDED #include "ds.h" //for Status,OK ... #ifndef ElemType #define ElemType int /* 数据元素类型默认为 int */ #define ELEMTYPE_TAG #endif /***********************************************

【数据结构1】顺序表

顺序表的基本概念 1 静态存储 2 动态存储 顺序表的基本操作 1 插入操作 2 删除操作 3 查找操作 4 顺序表并集 5 顺序表合并 1 顺序表的基本概念 顺序存储的线性表称为顺序表.表中元素的逻辑顺序与物理顺序相同. 假设顺序表L存储的起始位置是b,每个数据元素所占用存储空间大小是l,则表L所对应的顺序存储如下图.(本文规定:顺序表元素位序从1开始,而数组元素下标从0开始) 顺序表中元素的一维数组可以是静态分配,也可以是动态分配. 1.1 静态存储 在静态分配时,由于数组的大小和空间已经固

数据结构C#版笔记--顺序表(SeqList)

线性结构(Linear Stucture)是数据结构(Data Structure)中最基本的结构,其特征用图形表示如下: 即:每个元素前面有且只有一个元素(称为"前驱"),同样后面有且只有一个元素(称为"后继")--注:起始元素的前驱认为是空,末尾元素的后继认为也是空,这样在概念上就不冲突了. 线性表(List)是线性结构的一种典型实现,它又可以分为:顺序表(SeqList)和链表(LinkList)二大类. 顺序表(SeqList)的基本特征为:元素在内部存储时

数据结构之自建算法库——顺序表

学习<数据结构>课程的过程中,同步开展实践.对每一种逻辑结构,实现其各种存储结构下的基本运算,是一项基础性的工作.学习方法建议请参考"0207将算法变程序"[视频]部分建议的方法 本文为算法库中的第一个,针对线性表中的顺序存储结构,实现各种基本运算. 算法库包括两个文件:  头文件:list.h,包含定义顺序表数据结构的代码.宏定义.要实现算法的函数的声明:  源文件:list.cpp,包含实现各种算法的函数的定义 list.h #ifndef LIST_H_INCLU

C++实现顺序表的常用操作(插入删出查找输出)_C 语言

实现顺序表的插入,删除,查找,输出操作在C语言中经常用到.下面小编给大家整理实现代码,一起看下吧 代码如下所示: #include<iostream> using namespace std; #define MAXSIZE 15 typedef int DataType; typedef struct { DataType data[MAXSIZE]; //通常用一位数组来描述顺序表的数据存储 int SeqLength; /*线性表长度*/ } SeqList; SeqList *Init

顺序表应用1:多余元素删除之移位算法

顺序表应用1:多余元素删除之移位算法 Time Limit: 1000MS Memory Limit: 650KB Problem Description 一个长度不超过10000数据的顺序表,可能存在着一些值相同的"多余"数据元素(类型为整型),编写一个程序将"多余"的数据元素从顺序表中删除,使该表由一个"非纯表"(值相同的元素在表中可能有多个)变成一个"纯表"(值相同的元素在表中只保留第一个). 要求:        1.

顺序表应用2:多余元素删除之建表算法

顺序表应用2:多余元素删除之建表算法 Time Limit: 3MS Memory Limit: 600KB Problem Description 一个长度不超过10000数据的顺序表,可能存在着一些值相同的"多余"数据元素(类型为整型),编写一个程序将"多余"的数据元素从顺序表中删除,使该表由一个"非纯表"(值相同的元素在表中可能有多个)变成一个"纯表"(值相同的元素在表中只保留第一个). 要求:        1.必须先

顺序表应用4:元素位置互换之逆置算法

顺序表应用4:元素位置互换之逆置算法 Time Limit: 10MS Memory Limit: 570KB Problem Description 一个长度为len(1<=len<=1000000)的顺序表,数据元素的类型为整型,将该表分成两半,前一半有m个元素,后一半有len-m个元素(1<=m<=len),设计一个时间复杂度为O(N).空间复杂度为O(1)的算法,改变原来的顺序表,把顺序表中原来在前的m个元素放到表的后段,后len-m个元素放到表的前段. 注意:先将顺序表元