zoukankan      html  css  js  c++  java
  • 重新认识线性表的链式存储(单链表)

    #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>
  • 相关阅读:
    第十一周课程总结
    第十周课程总结
    第九周课程总结&实验报告(七)
    第八周课程总结&实验报告(六)
    第七周课程总结&实验报告(五)
    第六周课程总结&试验报告(四)
    课程总结
    第十四周课程总结&实验报告(简单记事本的实现)
    第十三周
    第十二周学习总结
  • 原文地址:https://www.cnblogs.com/lianggaoblogyuan/p/9786168.html
Copyright © 2011-2022 走看看