线性表

#include"stdio.h"     //非顺序结构  链表
#include"stdlib.h"
#include"time.h"
struct node
{
    int val;
    node *next;
};

int Length(node *head) //长度
{
    int cnt;
    node*p=head->next;
    for(cnt=0;p;p=p->next)
        cnt++;
    return cnt;
}

node *init() //初始化
{
    node*head=(node*)malloc(sizeof(node));
    head->val=-1;
    head->next=NULL;
    return head;
}



void Delete(node *head,int *e,int pos)  //删除
{
    int i=-1; //设置头结点 为-1这个位置
    node *p=head;
    while(i<=pos-1&&p) //找到要删除的位置的前驱
    {
        i++;
        if(i>pos-1) break; //找到位置了
        if(p->next)
            p=p->next;
        else
            break;
    }
    if(i==pos)  //找到这个元素了
    {
        node *q=p->next;
        *e=q->val;
        p->next=q->next;
    }
    else
    {
        printf("It's so boring to delete this elem!\n");
    }
}
void Insert(node *head,int e,int pos)  //插入
{
    int i=-1;node *p=head;
    while(i<=pos-1&&p)//找到元素pos的前驱
    {
        i++;        //标志表示能否插入  如果无法到达pos+1,无法插入
        if(i>pos-1) break; //找到目标位置了

        if(p->next) //当 没有找到 并且 后继不为空 的时候继续 向后移动
            p=p->next;
        else break;  //当 后继为空 的时候会直接退出

    }
    if(i==pos)
    {
        node *q=(node*)malloc(sizeof(node));
        q->next=p->next;
        q->val=e;
        p->next=q;
    }
    else{
        printf("Why so Diao!\n");
    }

}
void ListTraverse(node *head)  //遍历
{
    int first=1;
    for(node*p=head->next;p;p=p->next)
    {
        if(first) first=0;
        else printf(" ");
        printf("%d",p->val);
    }
    printf("\n");
}
int main()
{
    node * head=init();
    Insert(head,1,0);
    Insert(head,2,0);
    Insert(head,3,0);
    Insert(head,40,0);
    Insert(head,5,0);
    ListTraverse(head);
    Insert(head,3,10);
    printf("List length:%d\n",Length(head));

    int *e=(int*)malloc(sizeof(int));
    Delete(head,e,0);
    ListTraverse(head);
    printf("%d\n",*e);
    printf("List length:%d\n",Length(head));

    printf("%.2f\n",(double)clock()/CLOCKS_PER_SEC);

}

.


#include"stdio.h"      //顺序结构 数组
#include"stdlib.h"
struct list{
    int *next;
    int len;
    int cap;
};
void init(list *s,int n)  //初始化一个容量为n的表
{
    s->next=(int*)malloc(n*sizeof(int));
    s->len=0;
    s->cap=n;
}
void append(list *s,int a)  //向末尾追加一个数字
{
    s->next;
    if(s->len<s->cap)
    s->next[s->len++]=a;
}
void insert(list *s,int a,int pos)  //向pos位置插入a
{
    if(pos>=0&&pos<=s->len) // 在[0...len]范围内可以操作
    if(s->len+1<s->cap) //小于容量才操作
    {
        for(int i=++s->len-1;i>pos;i--)
            s->next[i]=s->next[i-1];
        s->next[pos]=a;
    }
}
void Delete(list *s,int pos,int *e)
{
    if(pos>=0&&pos<s->len)  //删除的位置不能出界
    {
        *e=s->next[pos];
        for(int i=pos;i<--s->len;i++)
            s->next[i]=s->next[i]+1;
    }
}
void destroy(list *s)
{
    free((void*)s->next);
    s->next=NULL;
}
int compare(int e,int a)
{
    return a==e;
}
int location(list s,int e)
{
    for(int i=0;i<s.len;i++)
        if(compare(e,s.next[i]))
        return i;

    return -1;
}

int priorElem(list s,int e) //返回e的前驱的下标
{
    int pos=-1;
    for(int i=0;i<s.len;i++)
        if(compare(e,s.next[i]))
    {pos=i;break;}
    return pos==0?-1:pos-1;
}

void ListTraverse(list s)
{
    for(int i=0;i<s.len;i++)
        printf("%d ",s.next[i]);
    printf("\n");
}
int main()
{
    list *p=(list*)malloc(sizeof(list));
    init(p,5);
    //向末尾追加元素
    append(p,1);
    //打印表格 长度 容量 与第0个元素
    printf("%d %d %d\n",p->len,p->cap,p->next[0]);
    //向1这个位置插入2
    insert(p,2,1);
    printf("%d %d %d %d\n",p->len,p->cap,p->next[0],p->next[1]);

    printf("%d\n",location(*p,2));

    printf("%d\n",priorElem(*p,2));

    ListTraverse(*p);
    int a;
    Delete(p,0,&a);
    printf("%d %d\n",p->next[0],a);


    destroy(p);
    free((void*)p);
    p=NULL;
    return 0;
}


#include"stdio.h"  //循环链表
#include"iostream"
#include"stdlib.h"
struct Node
{
    Node *next;
    int val;
};

Node* Init()
{
    Node *head=(Node*)malloc(sizeof(Node));
    head->next=head;
    return head;
}

void Append(Node *head,int e)
{
    Node *p=head;
    while(p->next!=head)
    {
        p=p->next;
    }
    Node *q=(Node*)malloc(sizeof(Node));
    p->next=q;
    q->next=head;
    q->val=e;
}

void Insert(Node *head,int e,int pos)
{
    int i=-1;
    Node* p=head;
    while(i<=pos-1)  //while(i<=pos-1&&p->next!=head)
    {
        i++;
        if(i==pos) break;      //找到了
        if(p->next!=head)
            p=p->next;
        else break; //若下一个为head则停止
    }
    if(i==pos)
    {
        Node *q=(Node *)malloc(sizeof(Node));
        q->next=p->next;
        q->val=e;
        p->next=q;
    }
    else
        puts("It's so boring!");
}

void Delete(Node* head,int *e,int pos)
{
    Node*p=head;
    int i=-1;
    while(i<=pos-1)
    {
        i++;
        if(i==pos)break;
        if(p->next!=head) p=p->next;
        else break;
    }
    if(i==pos)
    {
        Node *q;
        q=p->next;
        *e=q->val;
        p->next=q->next;
        free(q);
        q->next=NULL;
    }
    else
    {
        *e=-1;
        puts("Why so boring!");
    }
}


void Print(Node *head)
{
    Node *p=head->next;
    while(p!=head)
    {
        printf("%d ",p->val);
        p=p->next;
    }

    printf("\n");
}

int main()
{
    Node *head=Init();
    Append(head,1);
    Append(head,2);
    Print(head);

    //Insert(head,3,1);
    //Insert(head,3,0);
    Insert(head,2,2);
    Insert(head,334,5);
    Print(head);
    int *e=(int*)malloc(sizeof(int));
    //Delete(head,e,2);
    //Delete(head,e,0);
    Delete(head,e,234);
    Print(head);
    printf("%d\n",*e);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值