重新认识线性表的链式存储(单链表)
#include <iostream>
using namespace std;
#define Status int
#define ElemType int
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
Status CreateList_L(LinkList </span>&head,<span style="color: rgb(0, 0, 255);">int</span> n)<span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">头插法逆序输出</span>
{
LinkList p;
head=(LinkList)malloc(sizeof(LNode));
head->next=NULL;
for(int i=1;i<=n;i++)
{
p=(LinkList)malloc(sizeof(LNode));
//scanf(&p->data);
cin>>p->data;
p</span>->next=head->next;<span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">头插法</span>
head->next=<span style="color: rgb(0, 0, 0);">p;
}
</span><span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
Status BehindCreateList_L(LinkList </span>&head,<span style="color: rgb(0, 0, 255);">int</span> n)<span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">尾插法可得正序输出</span>
{
LinkList p;
head</span>=(LinkList)<span style="color: rgb(0, 0, 255);">malloc</span>(<span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(LNode));
head</span>->next=<span style="color: rgb(0, 0, 0);">NULL;
LinkList q</span>=<span style="color: rgb(0, 0, 0);">head;
</span><span style="color: rgb(0, 0, 255);">for</span>(<span style="color: rgb(0, 0, 255);">int</span> i=<span style="color: rgb(128, 0, 128);">1</span>;i<=n;i++<span style="color: rgb(0, 0, 0);">)
{
p</span>=(LinkList)<span style="color: rgb(0, 0, 255);">malloc</span>(<span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(LNode));
cin</span>>>p-><span style="color: rgb(0, 0, 0);">data;
p</span>->next=q-><span style="color: rgb(0, 0, 0);">next;
q</span>->next=<span style="color: rgb(0, 0, 0);">p;
q</span>=q-><span style="color: rgb(0, 0, 0);">next;
}
</span><span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
Status GetElem_L(LinkList head,</span><span style="color: rgb(0, 0, 255);">int</span> i,ElemType &<span style="color: rgb(0, 0, 0);">e)
{
LinkList p;
p</span>=head-><span style="color: rgb(0, 0, 0);">next;
</span><span style="color: rgb(0, 0, 255);">int</span> j=<span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
</span><span style="color: rgb(0, 0, 255);">while</span> (p&&j<<span style="color: rgb(0, 0, 0);">i)
{
p</span>=p-><span style="color: rgb(0, 0, 0);">next;
j</span>++<span style="color: rgb(0, 0, 0);">;
}
</span><span style="color: rgb(0, 0, 255);">if</span> (!p||j><span style="color: rgb(0, 0, 0);">i)
{
</span><span style="color: rgb(0, 0, 255);">return</span> -<span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
e</span>=p-><span style="color: rgb(0, 0, 0);">data;
cout</span><<<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">data</span><span style="color: rgb(128, 0, 0);">"</span><<e<<<span style="color: rgb(0, 0, 0);">endl;
</span><span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
Status ListInsert_L(LinkList </span>&head,<span style="color: rgb(0, 0, 255);">int</span> i,ElemType e,<span style="color: rgb(0, 0, 255);">int</span> n)<span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">第i个位置之前插入数据</span>
{
if(i<0||i>n)
{
cout<<"n erroe"<<endl;
}
LinkList p;
LinkList q=head;
p=(LinkList)malloc(sizeof(LNode));
int j=0;
while (q&&j<i-1)
{
q</span>=q-><span style="color: rgb(0, 0, 0);">next;
j</span>++<span style="color: rgb(0, 0, 0);">;
}
</span><span style="color: rgb(0, 0, 255);">if</span> (!p||j>i-<span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">)
{
</span><span style="color: rgb(0, 0, 255);">return</span> -<span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
p</span>->data=<span style="color: rgb(0, 0, 0);">e;
p</span>->next=q-><span style="color: rgb(0, 0, 0);">next;
q</span>->next=<span style="color: rgb(0, 0, 0);">p;
</span><span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
Status ListDelete_L(LinkList </span>&head,<span style="color: rgb(0, 0, 255);">int</span> i,ElemType &e)<span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">把第i个节点删除</span>
{
LinkList q=head;
int j=0;
while (q->next&&j<i-1)//i-1 node
{
q=q->next;
j++;
}
</span><span style="color: rgb(0, 0, 255);">if</span> (!(q->next)||j>i-<span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">)
{
</span><span style="color: rgb(0, 0, 255);">return</span> -<span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
e</span>=q->next-><span style="color: rgb(0, 0, 0);">data;
cout</span><<<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">data</span><span style="color: rgb(128, 0, 0);">"</span><<e<<<span style="color: rgb(0, 0, 0);">endl;
q</span>->next=q->next-><span style="color: rgb(0, 0, 0);">next;
</span><span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
Status ShowList_L(LinkList head)
{
LinkList p</span>=head-><span style="color: rgb(0, 0, 0);">next;
</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">(p)
{
cout</span><<p->data<<<span style="color: rgb(0, 0, 0);">endl;
p</span>=p-><span style="color: rgb(0, 0, 0);">next;
}
</span><span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">;
}
</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main ()
{
LinkList a;
</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">CreateList_L(a,4);</span>
BehindCreateList_L(a,<span style="color: rgb(128, 0, 128);">4</span><span style="color: rgb(0, 0, 0);">);
ListInsert_L(a,</span><span style="color: rgb(128, 0, 128);">2</span>,<span style="color: rgb(128, 0, 128);">5</span>,<span style="color: rgb(128, 0, 128);">4</span><span style="color: rgb(0, 0, 0);">);
ShowList_L(a);
</span><span style="color: rgb(0, 0, 255);">int</span> e=<span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;
ListDelete_L(a,</span><span style="color: rgb(128, 0, 128);">2</span><span style="color: rgb(0, 0, 0);">,e);
ShowList_L(a);
GetElem_L(a,</span><span style="color: rgb(128, 0, 128);">1</span><span style="color: rgb(0, 0, 0);">,e);
system(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">pause</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
}</span></pre></div>

浙公网安备 33010602011771号