鏈表的插入刪除與鏈表的進階
來自專欄 Science雜記
void insert (Node *head,int i,int x)
{
Node*p,*s;int j=0;
p=head;
while((p!=NULL)&&(j<i-1))
{
p=p->next;
j++;
}
if(p==NULL)
cout>>"NO FOUND";
else
{
s=new Node;
s->data=x;
s->next=p->next;
p-next=s;
}
}插入
----------------------------------
void delete(Node *head,int i)
{
Node *p,*s;int j=0;
p=head;
while((p->next!=NULL)&&(j<i-1))
{
p=p->next;
j++;
}
if(p->next==NULL)
cout>>"NO ";
else
{
s=p->next;
p->next=s->next;
free(s);
}
}
刪除
-------------------------------------
struct Double node
{
int data;
node *pre,*next;
} ;雙向鏈表的定義
-----------
void insert(node *head,int i,int x)
{
node *s,*p;int j=0;
s=new node;
p=head;
while((p->next!=NULL)&&(j<i))
{
p=p->next;
j++;
}
if(p==NULL)
cout>>"NO answer";
else
{
s->pre=p->pre;
p->pre=s;
s->next=p;
p->pre->next=s;
}
}雙向鏈表的插入
------------------------------
void delete(Node *head,int i)
{
Node *p;int j=0;
p=head;
while((p->next!=NULL)&&(j<i-1))
{
p=p->next;
j++;
}
if(p==NULL)
cout>>"NO ";
else
{
p->pre->next=p->next;
p->next->pre=p->pre;
}
}
雙向鏈表的刪除
--------------------------------------
struct node
{
int d;
node *next
};
void Circular linked list(node*head,int n,m)
{
node*p,*r;
r=head;
p=new node;
p->d=1;
p->next=NULL;
for(int i=2;i<=n;i++)
{
p=new node;
p->d=i;
p->next=NULL;
r->next=p;
r=p;
}
r->next=head;r=head;
}
-----------------------
循環鏈表
推薦閱讀:
※幾種簡易速演算法
※Leetcodes Solution 2 Add Two Numbers
※民間絕學八字新演算法直斷七柱法[原創3]
※理解梯度下降法