充电第二天

链表

  1. 从键盘接收10个整数,构成从小到大的有序链表
   1:  #include <iostream>
   2:  using namespace std;
   3:   
   4:  typedef struct node
   5:  {
   6:      int data;
   7:      struct node *next;
   8:  }*Link;
   9:   
  10:  void insert(Link head, Link p)
  11:  {
  12:      Link q = head;
  13:   
  14:      while(q->next!=NULL && q->next->data<p->data) 
  15:          q = q->next;
  16:      p->next = q->next;
  17:      q->next = p;
  18:  }
  19:  int main()
  20:  {
  21:      int data;
  22:      
  23:      Link head = (Link)malloc(sizeof(struct node));
  24:      if (head = NULL)
  25:      {
  26:          cout << "memory allocation error\n";
  27:          exit(0);
  28:      }
  29:      head->next = NULL;
  30:      
  31:      for (int i=1; i<11; i++)
  32:      {
  33:          cout << "please input the " << i << " data:" << endl;
  34:          cin >> data;
  35:          Link p = (Link)malloc(sizeof(struct node));
  36:          if (p = NULL)
  37:          {
  38:              cout << "memory allocation error\n";
  39:              exit(0);
  40:          }
  41:          p->data = data;
  42:          insert(head, p);
  43:      }
  44:      cout << endl;
  45:      for (int i=1; i<11; i++)
  46:      {
  47:          cout << head->next->data << endl;
  48:          head = head->next;
  49:      }
  50:   
  51:      cout << system("pause");
  52:      return 0;
  53:  }
2.链表的删除
题目:有15个人围成一圈,顺序从1到15编号。从第一个人开始报数,凡报到n的人退出圈子。从键盘输入n的值,输出最后留在圈子里的人的编号
   1:  #define MAXN 15
   2:  #include <iostream>
   3:  using namespace std;
   4:   
   5:  typedef struct node
   6:  {
   7:      int data;
   8:      struct node *next;
   9:  }*Link;
  10:   
  11:  Link  createlist()
  12:  {
  13:      Link head = (Link)malloc(sizeof(struct node));
  14:      if (!head)
  15:      {
  16:          cout << "memory allocation error!" << endl;
  17:          exit(0);
  18:      }
  19:      head->data = 1;
  20:      head->next = head;   //头结点指向自身,形成环形
  21:      for (int i=MAXN; i>1; i--)
  22:      {
  23:          Link p = (Link)malloc(sizeof(struct node));
  24:          if (!p)
  25:          {
  26:              cout << "memory allocation error!" << endl;
  27:              exit(0);
  28:          }
  29:          p->data = i;   //头插,形成环形链表
  30:          p->next = head->next;
  31:          head->next = p;
  32:      }
  33:      return head;
  34:  }
  35:  int main()
  36:  {    
  37:      Link p = createlist();
  38:      cout << "please input n: " ;
  39:   
  40:      int n, k=MAXN;
  41:      cin >> n;
  42:      int c = 1;
  43:      Link q;
  44:      while(k>1)
  45:      {
  46:          if (n-1==c)
  47:          {
  48:              q = p->next;
  49:              p->next = q->next;
  50:              free(q);
  51:              c = 0;
  52:              k--;
  53:          } 
  54:          else
  55:          {
  56:              c++;
  57:              p = p->next;
  58:          }
  59:      }
  60:      cout << "The last number is: " << p->data << endl;    
  61:   
  62:  cout << system("pause");
  63:  return 0;
  64:  }
/*  链表的删除
*   题目:递归删除链表中的重复节点,返回链表头指针
*/

#include <iostream>
#define MAXN 10
using namespace std;

typedef struct node
{
    int data;
    struct node *next;
}*Link;

Link  createlist()
{
    Link head = (Link)malloc(sizeof(struct node));
    if (!head)
    {
        cout << "memory allocation error!" << endl;
        exit(0);
    }
    head->data = 2;
    Link q = head;
    for (int i=0; i<MAXN; i++)
    {
        Link p = (Link)malloc(sizeof(struct node));
        if (!p)
        {
            cout << "memory allocation error!" << endl;
            exit(0);
        }
        p->data = i;  
        q->next = p;
        q = p;
    }
    q->next = NULL;   //尾节点切记不要出现野指针
    return head;
}

Link delrep(Link head)
{
    Link p = head, q;
    while(p->next != NULL)
    {
        while(p->next != NULL && head->data != p->next->data)
            p = p->next;
        if (p->next == NULL)
        {
            head = head->next;
            delrep(head);
        }
        else
        {
            q = p->next;
            p->next = q->next;
            free(q);
        }
    }
    return head;
}
void printlist(Link head)
{
    Link p = head;
    do 
    {
        cout << p->data;
        p = p->next;
    } while (p->next!=NULL);
    cout << p->data << endl;
}
int main()
{    
    Link head = createlist();
    delrep(head);
    printlist(head);

    cout << system("pause");
    return 0;
}

你可能感兴趣的:(充电第二天)