数据结构《1》---单链表

单链表虽然简单,但是会容易发生指针操作的一些错误。。本文的实现参照了《C与指针》上单链表的实现,没有采用头节点,


// copyright @ L.J.SHOU Dec.10, 2013
#include "linked-list.h"
#include <iostream>
using namespace std;

/*
struct ListNode
{
  int val;
  ListNode *next;
  ListNode(int x=0, ListNode* p=NULL)
    : val(x), next(p){}
};
*/

ListNode* Insert(ListNode *list, int x)
{ //keep list in order after inserting x
  ListNode *cur(list), *pre(NULL), *node(NULL);

  //找到插入位置
  while(cur && cur->val < x){
    pre = cur;
	cur = cur->next;
  }

  if((node = new ListNode(x))==NULL){
    cerr << "Out of space!!!" << endl;
	return list;
  }

  node->next = cur;
  //插入在首部
  if(pre == NULL)
    list = node;
  else
    pre->next = node;

  return list;
}

ListNode* InsertFront(ListNode *list, int x)
{//insert into list front O(1)
  ListNode *node = new ListNode(x, list);

  return node;
}

ListNode* InsertRear(ListNode *list, int x)
{ 
  ListNode *rear(list), *node(NULL);

  while(rear && rear->next)
    rear = rear->next;

  node = new ListNode(x, NULL);

  // insert as the first node
  if(rear == NULL)
    list = node;
  else
    rear->next = node;

  return list;
}

ListNode* Find(ListNode *list, int x)
{
  while(list && list->val != x)
  {
    list = list->next;
  }

  return list;
}

// return pointer to last node
// return NULL if list is empty
ListNode* Last(ListNode *list)
{//return pointer to the last node
  while(list && list->next)
    list = list->next;

  return list;
}

ListNode* Delete(ListNode *list, int x)
{
  if(list == NULL) return NULL;
  ListNode *cur(list), *pre(NULL);
  //locate at the previous node
  while(cur && cur->val != x)
  {
    pre = cur;
	cur = cur->next;
  }
  //x not found
  if(cur == NULL)
	return list;

  //delete node containing x
  if(pre == NULL)
    list = cur->next;
  else
    pre->next = cur->next;
  delete cur;

  return list;
}

ListNode* Destroy(ListNode *list)
{
  ListNode *node(NULL), *cur(list); 

  while(cur)
  {
    node = cur;
	cur = cur->next;
	delete node;
  }
  return NULL;
}

int Length(ListNode const *list)
{
  if(list == NULL) return 0;
  else return 1+Length(list->next);
}

void PrintList(ListNode const *list)
{
  for(; list!=NULL; list=list->next)
    cout << list->val << " ";
  cout << endl;
}

ListNode* Reverse(ListNode *list)
{
  if(list == NULL || list->next == NULL) return list;
  ListNode *reverse_list(NULL), *next(NULL);

  while(list)
  {
    next = list->next;
    list->next = reverse_list;
	reverse_list = list;
	list = next;
  }
  return reverse_list;
}

int main(void)
{
  ListNode *list(NULL);
  for(size_t i=0; i<10; ++i)
    list = Insert(list, i);
  list = Reverse(list);
  PrintList(list);
  cout << "Last node: " << Last(list)->val << endl;
  cout << Find(list, 8)->val << endl;
  cout << "length: " << Length(list) << endl;
  list = Destroy(list);
  return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值