zoukankan      html  css  js  c++  java
  • [Data Structure]线性表:顺序表与链表实现

    给自己找个事做…也算是开个头,打算慢慢把大二要学的数据结构课的数据结构自己慢慢地来都搭一遍。

    先来点简单的。

    顺序表:

    struct Lnode{
        int data[maxsize];//表本体
        int last;//最后一个元素的下标
    
        void clear(){
            last = -1;
        }//重置顺序表,将表的长度置为0(即最后一个下标置为-1)
    
        int FindKth(int k){
            if(k > last || k < 0) return -1;
            return data[k];
        }
    
        int Find(int x){
            int i = 0;
            while(i <= last && x != data[i])
                i++;
            if(i > last) return -1;
            else return i;
        }
    
        void insert(int i, int x){
            if(i < 0 || i > last + 1){
                printf("The Error Position
    ");
                return ;
            }
            for(int j = last; j >= i; j--)
                data[j+1] = data[j];
            data[i] = x;
        }
    
       void del(int i){
            if(i < 0 || i > last){
                printf("The Error Position
    ");
                return ;
            }
            for(int j = i + 1; j <= last; j++){
                data[j-1] = data[j];
            }
       }
    
       inline int length(){return last + 1;}
    };
    typedef Lnode* List;

    分别对建空表、找下标为k的元素、找值为x的元素下标、向表中下标为i-1的后面插入元素x、删除下标为i的元素、返回表长进行了实现

    链表(单向):

     1 struct node{
     2     int data;
     3     node* next; 
     4 };
     5 
     6 typedef node* list;
     7 
     8 list create(int n){//建表
     9     list t = (list)malloc(sizeof(node));
    10     list head = (list)malloc(sizeof(node));;
    11     head = t;
    12     t->next = NULL;
    13     while(n--){
    14         int tmp;
    15         cin>>tmp;
    16         list s = (list)malloc(sizeof(node));
    17         s->data = tmp;
    18         t->next = s;
    19         s->next = NULL;
    20         t = s;
    21     }
    22     return head->next;
    23 }
    24 
    25 list FindKth(int k, list head){//找表中第k个元素
    26     int j = 1;
    27     list tmp = head;
    28     while(tmp && j < k){
    29         j++;
    30         tmp = tmp->next;
    31     }
    32     if(tmp != NULL) return tmp;
    33     else return NULL; 
    34 }
    35 
    36 list Find(int x, list head){//找表中数值域为x的结点
    37     list tmp = head;
    38     while(tmp->data != x && tmp){
    39         tmp = tmp->next;
    40     }
    41     if(tmp != NULL) return tmp;
    42     else return NULL;
    43 }
    44 
    45 list insert(int i, int x, list head){ //将data值为x的结点插在第i个位置,即i-1个位置的后方
    46     list tmp = head;
    47     list position = FindKth(i-1, tmp);
    48     list s = (list)malloc(sizeof(node));
    49     s->data = x;
    50     if(i == 1){
    51         s->next = head;
    52         return s;
    53     }
    54 
    55     if(position == NULL)return tmp;
    56     else{
    57         s->next = position->next;
    58         position->next = s;
    59         return tmp;
    60     }
    61 }
    62 
    63 list del(int i, list head){//删掉第i个结点
    64     list tmp = head;
    65     list p = FindKth(i-1, tmp);
    66     if(i == 1){
    67         tmp = head->next;
    68         return tmp;
    69     }
    70 
    71     if(p == NULL || p->next == NULL) return tmp;
    72     else{
    73         list t = (list)malloc(sizeof(node));
    74         t = p->next;
    75         p->next = t->next;
    76         free(t);
    77         return tmp;
    78     }
    79 }
    80 
    81 int Length(list head){
    82     int j = 0;
    83     do{
    84         head = head->next;
    85         j++;
    86     }while(head);
    87     return j;
    88 }
    89 
    90 void print(list head){
    91     while(head){
    92         cout<<head->data<<" ";
    93         head = head->next;
    94     }
    95 }
  • 相关阅读:
    Visual C# 3.0 新特性概览
    一个C#睡前故事[翻译]
    ASP.NET User Control使用技巧一则
    Visual Studio 2005 调试器的新增功能
    .NET Framework 3.0新特性
    客户端提高WEB页面显示速度的方法
    VS.Net 2005中如何:使用“编辑并继续”
    如何在 VS 2005 里调试 Javascript
    关于DotNES
    Sql语句优化汇总(CSDN上的帖子)
  • 原文地址:https://www.cnblogs.com/leafsblogowo/p/12876770.html
Copyright © 2011-2022 走看看