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

来源:互联网 发布:linux重启后进不去系统 编辑:程序博客网 时间:2024/06/11 00:58
  • 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;}    


原创粉丝点击