首页 > 其他 > 详细

链表的增删(未考虑末尾)

时间:2014-05-23 23:00:32      阅读:401      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include<iostream>
using namespace std;

class node{
public:
    node():value(0),next(NULL){}
    ~node(){}
    int value;
    node* next;
};///be careful this ;

node* createlist(int a[],int n)
{
 node* startnode = new node[n];
 node* ret = startnode;
 for(int i = 0;i<n;i++)
 {
     startnode[i].value = a[i];
     if(i<n-1) 
         startnode[i].next = startnode + i + 1;
 }
 while(startnode)
 {
     cout<<" "<<startnode->value;
     startnode = startnode->next;
 }
 cout<<endl;
 return ret;
}

node* insertnode(node* nd,int n)///在第n个元素后面加数
{
 node* temp = nd;
 node* startnode = nd;

 while(--n) ////注意这个符号的顺序
 temp = temp->next; ///这样就知道了第n个数

 node* nnode = new node;
 nnode->value = 123;
 nnode->next = temp->next;
 temp->next = nnode;

  while(startnode)
 {
     cout<<" "<<startnode->value;
     startnode = startnode->next;
 }
  cout<<endl;
 return nd;///暂时不考虑插入头这种情况
}

node* deletenode(node* nd,int n)
{
 node* temp = nd;
 node* t;
 node* startnode = nd;
 n = n -1;
 while(--n)
     temp = temp->next;
 t = temp->next ;
 temp->next = temp->next->next;
 delete t;
   while(startnode)
 {
     cout<<" "<<startnode->value;
     startnode = startnode->next;
 }
   cout<<endl;
 return nd;
}
int main()
{
    int a[] = {1,2,3,4,5,6,7,8,9};
    node * t = createlist(a,9);
    insertnode(t,3);
    deletenode(t,4);
}
bubuko.com,布布扣

 

链表的增删(未考虑末尾),布布扣,bubuko.com

链表的增删(未考虑末尾)

原文:http://www.cnblogs.com/berkeleysong/p/3737073.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!