简单链表

转载 2012年03月26日 16:57:43
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; 
} 


单链表的实现-简单操作

  • 2016年11月18日 22:50
  • 401KB
  • 下载

c语言简单循环链表

  • 2016年04月22日 09:42
  • 2KB
  • 下载

(C#)一个最简单的链表类

转载自https://my.oschina.net/Tsybius2014/blog/277453 摘要: 一个最简单的链表类 一、关于C#链表 C#链表可用类LinkedList来存放。本文中的...

简单链表程序

  • 2014年03月16日 21:17
  • 1KB
  • 下载

c语言简单链表

  • 2013年07月15日 11:21
  • 1KB
  • 下载

动态内存分配(malloc/free)简单实现--隐式空闲链表

本文使用隐式空闲链表实现简单的动态内存分配。 动态内存分配器维护一个大块区域,也就是堆,处理动态的内存分配请求。分配器将堆视为一组不同大小的块的集合来维护,每个块要么是已分配的,要么是空闲的。 实...

简单的链表结构代码

  • 2012年12月22日 21:48
  • 4KB
  • 下载

基于JAVA的单链表简单实现

  • 2013年01月05日 23:07
  • 5KB
  • 下载

用链表做的简单c语言学生管理系统

用链表做的简单c语言学生管理系统 里面有用到简单的文件操作 #include #include struct STU {     int num;     char name[5];     ...
  • mealu00
  • mealu00
  • 2017年07月14日 15:46
  • 148
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:简单链表
举报原因:
原因补充:

(最多只允许输入30个字)