阿tree

博客园 首页 联系 订阅 管理

这两天一直在学习对单链表的操作,这是我的一些心得体会,请各位大神赐教。

typedef struct Node
{                                 这里是简单的数据结构的构建
  int data;
  struct Node *next;
}Node, *ptr_Node;

typedef enum Status
{
  SUCCESS, ERROR
}Status;

首先,我们需要建立一条链表。

ptr_Node create(int *arr, int n)
{
  struct Node *head, *tail;                定义头指针,尾指针。所谓头,是引领一个结构的开头,所谓尾,则是指示结构的最后
  int i = 0;

  head = (ptr_Node)malloc(sizeof(Node));        要存储数据必须要有存储空间,用malloc开辟,(记住有借有还才是好人)

  tail = head;                       对指针经行各种初始化
  tail->next = NULL;                   连接下一个结构的关键桥梁

  for( i ; i < 8 ; ++i)
  {
  ptr_Node pNew = (ptr_Node)malloc(sizeof(Node));   
  pNew->data = arr[i];                  因为pNew是结构类型,因此里面包含的内容都是一样的,同种数据类型才能存储
  tail->next = pNew;  
  pNew->next = NULL;
  tail = pNew;                      通过尾指针指向下一个数据结构
  }

  return head;
  return NULL;
}

void destroy(ptr_Node head)
{
  ptr_Node tail;                      定义尾指针,其实也就是擦屁股,head开辟空间,就用tail释放空间。  

  while(head)                        不要跟我说,我任性,不想这么干,下场就是蓝屏
  {
  tail = head;
  head = head->next;                    之前是用tail连接两个结构,是为了开辟空间,这个则是相反
  free(tail);
  }
}

void print(ptr_Node head)                    数据操作1:遍历         显示之前存储的数组中的元素
{
  ptr_Node p = head->next;                  桥梁的作用

  while( p != NULL)
  {
  printf("%d ", p->data);                    不断地做循环,直到地址为空
  p = p->next;
  }
  printf("\n");
}
Status insert(ptr_Node *head, ptr_Node node, int index)     链表操作2:数据插入(第一个为链表的开头,第二个为带插入的位置,第三个为插入的位置)
{
  int i = 0;                            
  ptr_Node _node = *head;
  ptr_Node pSwap = NULL;
  if(((index < 0) || (index > 10)) && (_node != NULL))     (这个地方还有bug,因为10太死板了,非空太机械化)
    return ERROR;
  while( i < index )                        通过循环,查找到目标位置
  {
    _node = _node->next;
    ++i;
  }

  ptr_Node pNew = (ptr_Node)malloc(sizeof(Node));      开辟空间用于存储插入的数据

  pNew = node;                        这里的原理跟创建链表相同,pSwap仅仅是起到桥梁的作用
  pSwap = _node->next;
  _node->next = pNew;
  pNew->next = pSwap;
  return SUCCESS;
  }
Status delete(ptr_Node *head, int index, int *data)        链表操作3:数据删除(第二个数是位置,第三个数是返回删除的数据)
{
  int i = 0;
  ptr_Node _node = *head;
  ptr_Node pSwap;                        仅仅是用于交换
  if(((index < 0) || (index > 10)) && (_node->next != NULL))
    return ERROR;
  while( i < index)
  {
  _node = _node->next;
  ++i;
  }
  pSwap = _node->next;                      越过目标位置,即达到删除的目的。
  *data = pSwap->data;        
  _node->next = _node->next->next;
  free(pSwap);                            有借有还的优良习惯

  return SUCCESS;
}
int search(ptr_Node head, int data)                  链表操作4:数据查找(第二个目标数据)
{
  ptr_Node _node = head->next;                  
  int i = 0;
  while((_node != NULL) && (data != _node->data))        进行判断
  {
    _node = _node->next;  
    ++i;
  }
  if(_node == NULL)
    return -1;
  else if(data == _node->data)
    return i;                            返回目标数据的位置
}
Status edit(ptr_Node head, int index, int *data)            数据操作5:替换数字
{
  ptr_Node _node = head;
  int i = 0;
  int swap;
  if((index < 0) && (_node->next == NULL) && (index > 10))
  return ERROR;
  while( i < index)
  {
  _node = _node->next;
  ++i;
  }

swap = _node->next->data;
_node->next->data = data;
data = swap;

return SUCCESS;
}

int main()
{
  ptr_Node head, p;
  int i, arr[8] = {23, 32, 12, 21, 33, 54, 61, 11};
  printf("测试数组为:");
  for(i = 0;i < 8;i++)
    printf("%d ", arr[i]);
  printf("\n\ncreate函数测试\n");
  head = create(arr, 8);
  if(NULL != head)
    print(head);
  else
    return 0;
  p = (ptr_Node)malloc(sizeof(Node));

  printf("\n\n插入函数测试");
  p->data = 100;
  if(SUCCESS == insert(&head, p, 0))
  {
    printf("\n---------**********---------\n");
    print(head);
  }
  p = (ptr_Node)malloc(sizeof(Node));
  p->data = 100;
  if(SUCCESS == insert(&head, p, 3))
  {
    printf("\n---------**********---------\n");
    print(head);
  }
  if(SUCCESS == insert(&head, p, 100))
  {
    printf("\n---------**********---------\n");
    printf("ERROR\n");
  }
  if(SUCCESS == insert(&head, p, -1))
  {
    printf("\n---------**********---------\n");
    printf("ERROR\n");
  }

  printf("\ndelete函数测试");
  if(SUCCESS == delete(&head, 0, &i))
  {
    printf("\n---------**********---------\n");
    printf("被删除的结点数据 : %d\n", i);
    print(head);
  }
  if(SUCCESS == delete(&head, 4, &i))
  {
    printf("\n---------**********---------\n");
    printf("被删除的结点数据 : %d\n", i);
    print(head);
  }
  if(SUCCESS == delete(&head, 100, &i))
  {
    printf("\n---------**********---------\n");
    printf("ERROR\n");
  }
  if(SUCCESS == delete(&head, -1, &i))
  {
    printf("\n---------**********---------\n");
    printf("ERROR\n");
  }

  printf("\n\nsearch函数测试");
  i = search(head, 23);
  {
    printf("\n---------**********---------\n");
    if(i < 0)
      printf("没找到\n");
    else
      printf("相等节点的前一个位置为 : %d\n", i);
  }
  i = search(head, 12);
  {
    printf("\n---------**********---------\n");
  if(i < 0)
    printf("没找到\n");
  else
    printf("相等节点的前一个位置为 : %d\n", i);
  }
  i = search(head, 11);
  {
    printf("\n---------**********---------\n");
  if(i < 0)
    printf("没找到\n");
  else
    printf("相等节点的前一个位置为 : %d\n", i);
  }
  i = search(head, 111);
  {
    printf("\n---------**********---------\n");
  if(i < 0)
    printf("没找到\n");
  else
    printf("相等节点的前一个位置为 : %d\n", i);
  }

  printf("\nedit函数测试");
   i= 10;
   if(SUCCESS == edit(head, 0, &i))
  {
    printf("\n---------**********---------\n");
    printf("index : %d\t 输入数据 : %d\t 输出数据 : %d\n", 5, 10, i);  
    print(head);
  }
  i = 100;
  if(SUCCESS == edit(head, 5, &i))
  {
    printf("\n---------**********---------\n");
    printf("index : %d\t 输入数据 : %d\t 输出数据 : %d\n", 5, 100, i);
    print(head);
  }
  if(SUCCESS == edit(head, 100, &i))
  {
    printf("\n---------**********---------\n");
    printf("ERROR\n");
  }  
  if(SUCCESS == edit(head, -1, &i))
  {
    printf("\n---------**********---------\n");
    printf("ERROR\n");
  }
  destroy(head);
  return 0;
}

posted on 2015-05-26 23:27  阿tree  阅读(212)  评论(0)    收藏  举报