数据结构----线性表的基本操作

这篇博客主要介绍了数据结构中的顺序表和链表操作,包括顺序表的删除元素(O(n)时间复杂度)、数组反转和循环左移,以及链表的头插法和尾插法创建、删除和插入操作。同时,还展示了链表的递归和非递归反转实现。通过实例演示了这些操作的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于线性表中的顺序表和链表的一些基本操作
1、顺序表在O(n)删除一个指定元素
2、顺序表反转和循环左移
3、链表的头插法和尾插法建立链表
4、链表的删除和插入
5、链表的递归和非递归反转

#include <bits/stdc++.h>
using namespace std;
int data[]={1,2,3,4,5,6,7};
typedef struct LNode{
      int data;
      struct LNode *next;
}LNode,*LinkList;

int Reverse(int arr[],int left,int right){/*left---right对调*/
    int mid=(left+right)/2;
    for(int i=0;i<mid-left;i++){
        swap(arr[i+left],arr[right-i]);
    }
}
void display(int arr[],int n){
    for(int i=0;i<n;i++){
        cout<<arr[i]<<' ';
    }
    cout<<endl;
}
/*循环左移num个数据*/
void left_num(int arr[],int num,int n){//数组里面的元素循环左移num个

    Reverse(arr,0,num-1);
    Reverse(arr,num,n-1);
    Reverse(arr,0,n-1);
}
void Delete(int arr[],int x,int n){//在O(n)的时间里就可以完成对于指定元素的删除
    int k=0;//记录不为数据不为X的个数;
    for(int i=0;i<n;i++){
        if(arr[i]!=x){
            arr[k++]=arr[i];
        }
    }

}
/*都是具有头结点的链表*/
/*头插法建立链表*/
LinkList HeadCreate(int arr[],int n){
    LNode *head=(LNode*)malloc(sizeof(LNode));
    head->next=NULL;
    for(int i=0;i<n;i++){cout<<"初始数据:";
        LNode *p=(LNode *)malloc(sizeof(LNode));
        p->data=arr[i];
        p->next=head->next;
        head->next=p;
    }
     return head;
}
/*尾插法建立链表*/
LinkList RearCreate(int arr[],int n){
    LNode *head=(LNode *)malloc(sizeof(LNode));
    LNode *r=head;
    for(int i=0;i<n;i++){
        LNode *p=(LNode *)malloc(sizeof(LNode));
        p->data=arr[i];
        r->next=p;
        r=p;
    }
    r->next;
    return head;
}
void LinkListdispaly(LinkList head){
       LNode *p=head->next;
       while(p!=NULL){
           cout<<p->data<<' ';
           p=p->next;
       }
    cout<<endl;

}
void Delete(LinkList head,int x){/*把p指针后面的值给到当前指针就可以做到删除后面指针即可*/
     LNode *p=head->next;
     while(p){
        if(p->data==x){
            LNode *s=p->next;
            p->data=s->data;
            p->next=s->next;
            free(s);
        }
        p=p->next;
     }
}
void Insert(LinkList head,int index,int x){/*在index的位置插入一个元素值为x*/
      LNode *p=head->next;
      int i=1;
      while(p){
           if(i==index-1){
                LNode *q=(LNode*)malloc(sizeof(LNode));
                q->data=x;
                q->next=p->next;
                p->next=q;
                break;
           }
           p=p->next;
           i++;
      }
}
/*链表的递归翻转 这是针对不带头结点的链表所以传入我们这里是带头结点所以是head->next*/
LinkList Reverse_rect(LinkList head){
    if(head->next==NULL||head==NULL){
           return head;
     }
     LNode *newHead=Reverse_rect(head->next);
     head->next->next=head;
     head->next=NULL;
     return newHead;
}
/*链表非递归反转*/
LinkList ReverseList(LinkList head){
     LNode *res=head;

     LNode *p=head->next;
     head->next=NULL;
     LNode *s;//s为辅助指针指向p的下一个节点,防止断链;
     while(p){
         s=p->next;
         p->next=head->next;
         head->next=p;
         p=s;
     }
   return res;
}
int main()
{
    cout<<"初始数据     :";
    display(data,7);
    cout<<"翻转整个数据 :";
    Reverse(data,0,6);
    display(data,7);
    cout<<"循环左移3个位置:";
    left_num(data,3,7);
    display(data,7);
    cout<<"删除数据值为5的元素 :";
    Delete(data,5,7);
    display(data,6);
    cout<<"后插法建立一个链表";
    LinkList head=RearCreate(data,7);
    LinkListdispaly(head);
    cout<<"在链表中删除数据为4的节点:";
    Delete(head,4);
    LinkListdispaly(head);
    cout<<"在第4个位置插入4 :";
    Insert(head,4,4);
    LinkListdispaly(head);
    cout<<"非递归翻转链表 :";
    LinkList Res=ReverseList(head);
    LinkListdispaly(Res);
    cout<<"递归翻转链表 :";
    LNode *Newhead=Reverse_rect(head->next);
    while(Newhead){
        cout<<Newhead->data<<' ';
        Newhead=Newhead->next;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坏牧羊人.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值