简单链表

C++单链表基本操作(代码) 
#include <iostream> 
#include <stdlib.h> 
#include <assert.h> 
using namespace std; 
 
// list node structure 
typedef struct _NODE{ 
      int data; 
      struct _NODE *next; 
}NODE, *PNODE; 
 
// create a list, return the pointer to the head node 
PNODE create() 
{ 
      PNODE head = NULL; 
      PNODE current = NULL; 
      PNODE temp = NULL; 
      int data = 0; 
      int cnt = 1; 
 
      temp = new NODE; 
      assert(temp != NULL); 
      cout << "Please input numbers, end with 0" << endl; 
      cin >> data; 
      temp->data = data; 
 
      while(temp->data != 0) 
      { 
            if(cnt == 1) 
            { 
                  // head node 
                  current = temp; 
                  head = current; 
            }else{ 
                  current->next = temp; 
            } 
 
            current = temp; 
            temp = new NODE; 
            assert(temp != NULL);       } 
 
      current->next = NULL; // last node 
      return head; 
} 
 
// print data of each node in the list, from head to tail 
void print(PNODE list) 
{ 
      if(!list) { cout << "empty list" << endl; return; } 
 
      PNODE current = list; 
      while(current) 
      { 
            cout << current->data << " "; 
            current = current->next; 
      } 
      cout << endl; 
} 
 
// free each node in the list 
void destroy(PNODE &list) // pointer reference 
{ 
      if(!list) { cout << "destroy ok" << endl; return; } 
 
      PNODE current = list; 
      PNODE temp = NULL; 
      while(current) 
      { 
            cout << "destroy " << current->data << endl; 
            temp = current->next; 
            delete current; 
            current = temp; 
      } 
      list = current; 
      cout << "destroy ok" << endl; 
} 
 
// count the nodes in the list 
int count(PNODE list) 
{ 
      if(!list) return 0; 
       
      int cnt = 0;       // traverse the list 
      PNODE current = list; 
      while(current) 
      { 
            ++cnt; 
            current = current->next; 
      } 
 
      return cnt; 
} 
 
// find value in the list, return the position of the node(start from 1...) 
int find(PNODE list, int data) 
{ 
      if(!list) { cout << "value " << data << " not found in empty list" << endl; return 0; } 
 
      int cnt = 0; 
      PNODE current = list; 
      // traverse the list 
      while(current) 
      { 
            ++cnt; 
            if(current->data == data) 
            { 
                  // found 
                  cout << "find value " << data << " in node " << cnt << endl; 
                  return cnt; 
            } 
 
            current = current->next; 
      } 
 
      // not found 
      cout << "value " << data << " not found" << endl; 
      return 0; 
} 
 
// reverse the list(loop) 
void reverse_loop(PNODE &list) 
{ 
      if(!list) return; 
 
      PNODE previous = NULL; 
      PNODE current = NULL;       PNODE next = NULL; 
 
      previous = list; 
      current = previous->next; 
      while(current) 
      { 
            next = current->next; 
            current->next = previous; 
            previous = current; 
            current = next; 
      } 
      // change head to tail 
      list->next = NULL; 
      list = previous; 
      // cout << "reverse ok" << endl; 
} 
 
// reverse the list(recursive) 
PNODE reverse_rec(PNODE list) 
{ 
      if(!list) return NULL; 
 
      PNODE tail = reverse_rec(list->next); 
      if(!tail) return list; // find the last node of list 
 
      // assume the rest of the list is reversed, we only need to reverse the first two nodes 
      list->next->next = list; // the second node pointer to the first node 
      list->next = NULL; // the first node pointer to NULL(new tail) 
 
      return tail; // unchanged(new head) 
} 
 
// insert node in list 
void insert(PNODE &list, int data) 
{ 
      PNODE node = new NODE; 
      assert(node != NULL); 
      node->data = data; 
       
      // test 
      cout << "insert value " << data << endl; 
 
      if(!list)  
      {             node->next = NULL;  
            list = node;  
            return ; 
      } 
 
      PNODE current = list; 
      while(current->data < node->data && current->next) 
      { 
            // find the right position 
            current = current->next; 
      } 
 
      if(current == list) 
      { 
            // insert before the first node 
            node->next = current; 
            list = node; 
            return ; 
      } 
       
      node->next = current->next; 
      current->next = node; 
 
      return ; 
} 
 
// delete specified node(start from 0) 
void remove(PNODE &list, int pos) 
{ 
      if(!list) return ; 
 
      PNODE current = list; 
      if(pos == 0) 
      { 
            // remove first node 
            list = current->next; 
            cout << "delete first node " << current->data << endl; 
            delete current; 
            return ; 
      } 
 
      // find pos in list 
      int cnt = 0; 
      PNODE temp = NULL;       while(current->next) 
      { 
            if(++cnt == pos) 
            { 
                  // found 
                  temp = current->next; 
                  current->next = temp->next; 
                  cout << "delete node " << pos+1 << " with value " << temp->data << endl; 
                  delete temp; 
                  return ; 
            } 
 
            current = current->next; 
      } 
 
      // not found 
      cout << "node " << pos+1 << " not found in list" << endl; 
      return ; 
} 
 
// find the middle node of list(rounded-up) 
PNODE middle(PNODE list) 
{ 
      if(!list) return NULL; 
 
      PNODE first = NULL; 
      PNODE second = NULL; 
      first = second = list; 
      int pos = 1; 
       
      while(second->next && second->next->next) 
      { 
            first = first->next; // step one by one 
            second = second->next->next; // step two each time 
            pos++; 
      } 
 
      cout<< "find middle node " << pos << "(" << count(list) << ") with value " << 
first->data << endl; 
      return first; 
} 
 
// exchange two integers 
void exchange(int &i, int &j) { 
      i ^= j; 
      j ^= i; 
      i ^= j; 
} 
 
// sort order(little -> big) 
void sort(PNODE &list) 
{ 
      if(list == NULL || list->next == NULL) return ; 
 
      int temp = 0; 
      PNODE current = list; 
      int cnt = count(list); 
      for(int i = 1; i < cnt; i++) 
      { 
            current = list; 
            for(int j = 0; j < cnt - i; j++) 
            { 
                  if(current->data > current->next->data) 
                  { 
                        exchange(current->data, current->next->data); 
                  } 
                  current = current->next; 
            } 
      } 
} 
 
// merge two sorted lists into one list 
PNODE merge_loop(PNODE list1, PNODE list2) 
{ 
      if(!list1) return list2; 
      else if(!list2) return list1; 
 
      PNODE new_list = NULL; 
      if(list1->data <= list2->data) 
      { 
            new_list = list1; 
            list1 = list1->next; 
      }else{ 
            new_list = list2; 
            list2 = list2->next; 
      } 
       PNODE current = new_list; 
      while(list1 && list2) 
      { 
            if(list1->data <= list2->data) 
            { 
                  current->next = list1; 
                  list1 = list1->next; 
                  current = current->next; 
            }else{ 
                  current->next = list2; 
                  list2 = list2->next; 
                  current = current->next; 
            } 
      } 
 
      if(!list1) current->next = list2; 
      if(!list2) current->next = list1; 
 
      return new_list; 
} 
 
// merge two sorted lists into one list 
PNODE merge_rec(PNODE list1, PNODE list2) 
{ 
      if(!list1) return list2; 
      else if(!list2) return list2; 
 
      PNODE mlist = NULL; 
      if(list1->data <= list2->data) 
      { 
            mlist = list1; 
            mlist->next = merge_rec(list1->next, list2); 
      }else{ 
            mlist = list2; 
            mlist->next = merge_rec(list1, list2->next); 
      } 
 
      return mlist; 
} 
 
// start from here 
int main() 
{ 
      // create       PNODE slist = create(); 
      PNODE slist2 = create(); 
      cout << "after input: " << endl; 
      print(slist); 
      print(slist2); 
 
      // sort 
      cout << "after sorted: " << endl; 
      sort(slist); 
      sort(slist2); 
      print(slist); 
      print(slist2); 
 
      // merge 
      cout << "after merged: " << endl; 
      // PNODE mlist = merge_loop(slist, slist2); 
      PNODE mlist = merge_rec(slist, slist2); 
      print(mlist); 
 
      // find 
      cout << "nodes of mlist: " << count(mlist) << endl; 
      find(mlist, 0); 
      find(mlist, 8); 
      find(mlist, 20); 
 
      // insert 
      insert(mlist, 0); 
      insert(mlist, 8); 
      insert(mlist, 10); 
      print(mlist); 
 
      // middle 
      middle(mlist); 
 
      // remove 
      remove(mlist, 0); 
      remove(mlist, 8); 
      remove(mlist, 10); 
      print(mlist); 
 
      // middle 
      middle(mlist); 
 
      // reverse       reverse_loop(mlist); 
      // mlist = reverse_rec(mlist); 
      cout << "after reversed: " << endl; 
      print(mlist); 
 
      // destroy 
      destroy(mlist); 
 
      system("PAUSE"); 
      return 0; 
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值