链表插入,删除,排序,反转

  • const常量有数据类型,宏常量没有。C++只使用const常量而不使用宏常量。
  • const数据成员只在某个对象生存期内是常量,而对于整个类而言却是可变的,因为类可以创建多个对象,不同的对象其const数据成员的值可以不同。
  • 不能在类声明中初始化const数据成员。只能在类构造函数的初始化表中进行。
  • 字符串拷贝:
char *strcpy(char *strDest,const char *strSrc);
{
    assert((strDest!=NULL)&& (strSrc !=NULL));   
    char*address = strDest;                     
    while((*strDest++ =* strSrc++)!= ‘\0’ )      
       NULL;
    returnaddress;                          
}

链表

#include<cstdlib>
#include<iostream>
using namespace std;
struct node 
{
      node *next;
      int data;
};
node *create()
{
     node*head;
     node*p;
     node*q;
     int x;
     head =(node*)malloc(sizeof(node));
     p =head;
     while(cin>> x)
     {
             if(x == 0)
                 break;
             q =(node*)malloc(sizeof(node));
             q->data =x;
             p->next =q;
             p = q;
     }
     head =head->next;
     p->next = NULL;
     return head;
}
int length(node*head)         
{
    int n = 0;
    node *p = head;
    while(p != NULL)
    {
           p =p->next;
           n++;
    }
    return n;
}
void print(node*head)
{
     node *p =head;
     while(p !=NULL)
     {
           cout <<p->data << " ";
           p = p->next;
     }
     cout << endl;
}
node *del(node*head, int num)
{
     node *p1 =head;
     node*p2;
     while(num!= p1->data &&p1->next != NULL)
     {
             p2 = p1;
             p1 =p1->next;
     }
     if(num ==p1->data)
     {
           if(p1 ==head)
           {
               head->next = p2;
               free(p1);
           }
           else
           {
              p2->next =p1->next;
              free(p1);
           }
     }
     else
        cout<< "Don't find"<< endl;
     return head;
}
node *insert(node*head, int num)
{
     node*p0;
     node*p1;
     node*p2;
     p0 =(node*)malloc(sizeof(node));
     p1 =head;
    p0->data = num;
    while(p0->data >p1->data &&p1->next != NULL)
     {
             p2 = p1;
             p1 =p1->next;
     }
     if(num<= p1->data)
     {
           if(p1 ==head)
           {
               p0->next = p1;
                head =p0;
           }
           else
           {
              p2->next =p0;
              p0->next =p1;
           }
     }
     else
     {
        p1->next =p0;
        p0->next =NULL;
     }
     return head;
}
node *sort(node*head)
{
     int n =length(head);
     inttemp;
     if(head ==NULL || head->next == NULL)
           return head;
     node *p =head;
     for(int i =1; i < n; i++)
     {
           p = head;
           for(int j = 0; j < n-i; j++)
           {
                  if(p->data> p->next->data)
                  {
                           temp = p->data;
                           p->data =p->next->data;
                           p->next->data =temp;
                  }
                  p =p->next;
           }
     }
     return head;
}          
node*reverse(node *head)
{
     node *p1 =head;
     node*p3;
    if(p1->next == NULL || p1 ==NULL)
               returnhead;
     node *p2 =p1->next;
    while(p2)
     {
                 p3 = p2->next;
                 p2->next = p1;
                 p1 = p2;
                 p2 = p3;                
     }
    head->next = NULL;
     head =p1;
     return head;
}
     
     
            
int main()
{
    int num1, num2;
    node *root = create();
    //cout<< length(root)<< endl;
    //print(root);
    //cin>> num1;
    //del(root, num1);
    //print(root);
    //cin>> num2;
    //insert(root, num2);
    //print(root);
    //sort(root);
    //print(root);
    reverse(root);
    print(root);
    system("pause");
    return 0;
}
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值