重新认识线性表的链式存储(单链表)

#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>&amp;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>-&gt;next=head-&gt;next;<span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">头插法</span>
            head-&gt;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>&amp;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>-&gt;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&lt;=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>&gt;&gt;p-&gt;<span style="color: rgb(0, 0, 0);">data;
    p</span>-&gt;next=q-&gt;<span style="color: rgb(0, 0, 0);">next;
    q</span>-&gt;next=<span style="color: rgb(0, 0, 0);">p;
    q</span>=q-&gt;<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  &amp;<span style="color: rgb(0, 0, 0);">e)
    {
    LinkList  p;
    p</span>=head-&gt;<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&amp;&amp;j&lt;<span style="color: rgb(0, 0, 0);">i)
    {
    p</span>=p-&gt;<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&gt;<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-&gt;<span style="color: rgb(0, 0, 0);">data;
    cout</span>&lt;&lt;<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>&lt;&lt;e&lt;&lt;<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>&amp;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-&gt;<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&gt;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>-&gt;data=<span style="color: rgb(0, 0, 0);">e;    
    p</span>-&gt;next=q-&gt;<span style="color: rgb(0, 0, 0);">next;
    q</span>-&gt;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>&amp;head,<span style="color: rgb(0, 0, 255);">int</span> i,ElemType &amp;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-&gt;next)||j&gt;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-&gt;next-&gt;<span style="color: rgb(0, 0, 0);">data;
        cout</span>&lt;&lt;<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>&lt;&lt;e&lt;&lt;<span style="color: rgb(0, 0, 0);">endl;
        q</span>-&gt;next=q-&gt;next-&gt;<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-&gt;<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>&lt;&lt;p-&gt;data&lt;&lt;<span style="color: rgb(0, 0, 0);">endl;
            p</span>=p-&gt;<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>
posted @ 2018-10-14 13:50  七月的四字  阅读(123)  评论(0编辑  收藏  举报