Data structure - basic operation of linear table

Some basic operations on the sequence table and linked list in the linear list
1. The sequence table deletes a specified element in O(n)
2. The sequence table reverses and rotates to the left
3. The head insertion method and the tail insertion method of the linked list establish a linked list
4 , Deletion and insertion of linked list
5, Recursive and non-recursive inversion of linked list

#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;
}

Guess you like

Origin blog.csdn.net/qq_44741914/article/details/110367071