C++ 单链表操作

来源:互联网 发布:风险矩阵图的横纵 编辑:程序博客网 时间:2024/05/22 00:33

include //printf() scanf()

include //srand() rand()

include //malloc() NULL

typedef struct Node
{
int data;
struct Node* next;
}Node,*PNode;

//随机建立产生n个元素的值,建立带表头结点的单链线性表,尾插法
//这个链表的头结点就是head,也可以看作这个链表的名字

PNode CreateList( int n ){    PNode head,p;    head=(PNode)malloc(sizeof(Node));    head->data=NULL;    p=head;    PNode temp;    srand((int)time(0));    int tt=rand();    while( n-- ){        temp=(PNode)malloc(sizeof(Node));        temp->data=rand();        p->next=temp;        p=temp;    }    p->next=NULL;    return head;}

// 链表的元素个数

int LengthList( PNode head ){    PNode p;    p=head;    int num=0;    while( p->next!=NULL ){        num++;        p=p->next;    }    return num;}

//打印链表

void PrintList(PNode head){    int n=LengthList( head );    PNode p;    p=head->next;    while( n-- ){        printf("%d\n",p->data);        p=p->next;    }}

//删除第i个结点,并用e返回它的值

bool DeleteList(PNode head,int i,int &e){    PNode p,q;    p=head;    int j=1;    while( p->next && j<i ){ //找第i-1个结点        p=p->next;        j++;    }    if( !(p->next) || j>i )        return false;    q=p->next;    e=q->data;    p->next=q->next;    free(q);    return true;}

//在链表第i个位置之前插入新的数据元素e

bool InsertList( PNode head,int i,int e ){    PNode p,q;    int j=1;    int len=LengthList(head);    if( i>len || i <j)        return false;    p=head;    while( p->next && j<i ){        p=p->next;        j++;    }    if( !(p->next)||j>i )        return false;    q=(PNode)malloc(sizeof(Node));    q->data=e;    q->next=p->next;    p->next=q;    return true;}

//冒泡法实现单链表的排序
//PNode SortList( PNode head)

void swap( PNode p,PNode q ){    int temp;    temp=p->data;    p->data=q->data;    q->data=temp;}Node* SortList( Node* head){    Node *p,*q;    p=head->next;    int len=LengthList(head);    int i,j;    if( len==1 )        return head;    for( i=0;i<len-1;i++ ){        p=head->next;        for( j=0;j<len-i;j++ ){            if( p->next ){                q=p->next;                if( p->data > q->data ){                    swap( p,q );                }                p=p->next;            }else                continue;        }    }    return head;}

//实现单链表的逆置

Node * ReverseList(Node * head){    Node *pre,*mid,*next;    if( head==NULL || head->next==NULL )        return head;    pre=head->next;    mid=pre->next;    pre->next=NULL;    while( mid ){        next=mid->next;        pre=mid->next;        pre=mid;        mid=next;    }    head->next=pre;    return pre;}

//逆置函数2

Node *reverse(Node *head)  {      Node *p1,*p2,*p3;      if (NULL == head || NULL == head->next)      {          return head;      }      p1 = head;      p2 = p1->next;      while (NULL != p2->next)      {          p3 = p2->next;          p2->next = p1;          p1 = p2;          p2 = p3;      }      p2->next = p1;      p1 = p2;      head->next->next = NULL;      head->next = p1;      return head;  }  

//主函数

int main(){    PNode head;    head=CreateList(10);    int len=LengthList(head);    printf("%s%d\n","The length of the List is: ",len);    PrintList(head);    int e;    bool status=DeleteList(head,2,e);    if( status )            printf( "The deleted element is %d\n",e );    printf("The length of the List is: %d\n",LengthList(head));    PrintList(head);    bool status1=InsertList(head,2,10000);    if( status1 )           printf( "The insert element is %d\n",e );    printf("The length of the List is: %d\n",LengthList(head));    PrintList(head);    printf("The sorted List is: %d\n",LengthList(head));    head=SortList(head);    PrintList(head);    printf("The reversed List is: %d\n",LengthList(head));    head=reverse(head);    PrintList(head);    head=ReverseList(head);    printf("The reversed List is: %d\n",LengthList(head));    PrintList(head);    system("pause");    return 0;}
原创粉丝点击