单链表 链表

来源:互联网 发布:淘宝手机版新品上架 编辑:程序博客网 时间:2024/06/15 01:44

最近学了下数据结构,真心觉得自己学起来比较吃力,东西总是记不住,而且还学得不好,因此,毅然觉得把自己所学的过程中理解写出来,也算是对自己所学知识的一点总结,其中难免错漏百出,如果你发现了请您告诉我一下,欢迎指出错误,以免误导不小心点进来看的人单链表 <wbr>链表,好了,下面开始写下自己的理解:

 

struct Node
{
         int data;
         Node *next;
};
 
 
Nodecreate1()//头插法 
{
         Node *head new Node;
         head->next NULL;
         for(int 0; 10; i++)
         {
                 Node *s new Node;
                 s->data 1;
                 s->next head->next;//这次的snext区域指向上次头结点next节点,形成连接
                 head->next s;//令头结点next区域指向s结点 
         }
         return head;
}
 
Nodecreate2()//尾插法
{
         Node *rear,*head,*s;
         head new Node;
         rear head;//初始化是空表
         for (int 0; 10; i++)
         {
                new Node;
                 s->data 10;
                 rear->next s;//尾结点next区域指向新节点
                 rear s;//新节点成为尾结点
         }
         rear->next NULL;//之后不要忘记置尾结点的后继指针为空
         return head;
}
 
int getLength(Node *head)//获得表的长度
{
         Node *s head->next;
         int count 0;
         while!= NULL)
         {
                 count++;
                 s= s->next;
         }
         return count;
}
 
NodegetPointer(Node *head,int position)//获得第position个位置的指针,位置是从1开始计算
{
         if(position getLength(head)//位置大于链表的长度
         {
                 cout<<"非法获取"<<endl;
                 return NULL;
         }
         int count 1;
         Node *s head->next;
         while(count position)
         {
                s->next;
                 count++;
         }
         cout<<s->data<<endl;
         return  s;
}
 
void insert(Node *head,int data ,int position)//往第positon后个位置插入数据data
{
         if(position getLength(head|| position 0)//位置非法
         {
                 cout<<"非法插入"<<endl;
                 return;
         }
         Node *s getPointer(head,position);//得到第position个位置的指针
         Node *p new Node;
         p->data data;
         p->next s->next; //新结点的next指针指向之前的结点的next指针
         s->next p;//positioninext指针指向新结点
}
 
NodedeleteData(Node *head,int position)
{
         if(position getLength(head|| position <= 0)//位置非法
         {
                 cout<<"删除失败!"<<endl;
                 return head;
         }
         if(position == 1)//如果是删除第一个位置上的数
         {
                 //cout<<"正在尝试删除第一个点"<<endl;
                 Node *s head->next;
                 delete(head);
                 head s;
                 return head;
         }
         Node *s getPointer(head,position 1);//得到待删点的前驱
         Node s->next;//得到待删点的指针
         s->next p->next;//待删点的前驱指向待删点的后驱,实现了链表的连接
         delete(p);
         return head;
}
 
int _tmain(int argc_TCHARargv[])
{
         cout<<"dd"<<endl;
         Node *head new Node;
         //head->next NULL;
         ///*
         head create2();
         cout<<"表长度为"<<getLength(head)<<endl;
         //insert(head,20,5);
         //getPointer(head,1);
         //deleteAll(head);
         //if(head==NULL)cout<<"表头为空"<<endl;
         head=deleteData(head,5);
         Node *q head->next;
         while(q != NULL)
         {
                 cout<<q->data<< ";
                q->next;
         }
 
         
}

 

原创粉丝点击