演算法導論第十章資料結構--雙向鏈表,導論第十章

來源:互聯網
上載者:User

演算法導論第十章資料結構--雙向鏈表,導論第十章

看的概念挺朦朧的,沒有明確好雙鏈表到底需要哪些方法,其實針對這種結構應該可以寫很多方法,並沒有什麼特定標準。

不過真是只看不練不行啊,一下手各種錯誤,各種溢出

#include<iostream>


using namespace std;
template<class T> struct Node
{
T value;
Node<T>* pre;
Node<T>* next;


};
template<class T> class Flist
{
private:
Node<T>* front;
Node<T>* end; //每寫一個函數,遍曆類中變數哪個要維護 
int count;

public:
Flist();   //defaut constructor
~Flist();
Flist(const Flist& C_list);//支援了這個,沒有寫支援 = 可以寫個空的內嵌函式來放空這種行為,否則有相關操作時,編譯器會自動產生 
inline void Deeply_Copy(const Flist& Copylist);
bool Isempty() const;
int Listsize() const;
T& Listfront()const;
T& Listend() const;  //返回引用是不是可以改首尾node的key值了 
void push_front(T N);
void push_back(T N);
void del_front();
void del_back();
//Node<T>* Listfind(T x); //搜尋有key值x的節點 
T ShowKey(int n);   //輸出第n個值 
 
};
template<class T> Flist<T>::Flist():count(0),front(0),end(0) //初始化順序與類中聲明有關與這無關 
{

}
template<class T> Flist<T>::~Flist()
{
Node<T>* temp;
while (front != 0)
{
temp = front;
delete temp;
front = front->next;
}
temp = NULL;
}
template<class T> bool Flist<T>::Isempty() const
{
return 0 == count;
}
template<class T> int Flist<T>::Listsize() const{
return count;
}
template<class T> Flist<T>::Flist(const Flist& C_list)
{
Deeply_Copy(C_list);

}
template<class T> T& Flist<T>::Listfront() const
{
return front->value;
}
template<class T> T& Flist<T>::Listend() const
{
return end->value;
}
template<class T> void Flist<T>::push_front(T N)
{
Node<T>* p = new Node<T>;
p->value = N;
p->next = front;
if (front != 0)
front->pre = p;
front = p;
if(end == 0)
end = p ;
p->pre = 0;
count++;
}
template<class T> void Flist<T>::push_back(T N)
{
Node<T>* p = new Node<T>;
p->value = N;
p->pre = end;
if (end != 0)   //一開始這裡崩潰是因為end沒有初始化0就用 
  end->next = p; 
end = p;
end->next = 0;
count++;
}
template<class T> void Flist<T>::del_front()
{
Node<T>* temp = front;
front = front->next;
count--;
front->pre = 0;
delete temp;
}
template<class T> void Flist<T>::del_back()
{
Node<T>* temp = end;
end = end->pre;
end->next = 0;
count--;
delete temp;
}

template<class T> T Flist<T>::ShowKey(int n)
{

/*
if (front == 0)
{
cout << "there is no element is the list.." << endl;
return ;                                          //未解決,size為0時,如何提示 
}*/
    if ((front !=0)  && (n <=count) && (n >= 1))
    {
    Node<T>* temp = front;
while(--n)                    //這裡如果while(n--)就溢出崩潰,n用後再減,這裡的用是判斷,不要以為只是括弧就可以前後隨便用 
{
temp = temp->next;   //n大於size也會溢出 
}
return temp->value;
//這裡temp指向了一個也有其他指標指向節點,可不要delete 
    }

}
template<class T> void Flist<T>::Deeply_Copy(const Flist& Copylist)
{
front = end = 0;
if (Copylist.front == 0)
return ;          //這裡起到了結束函數的作用 
front = new Node<T>;
Node<T>* cp = Copylist.front;
Node<T>* np = front;
np->value = cp->value;
count = 1;
np->pre = 0;
cp = cp->next;
while (cp != 0)
{
np->next = new Node<T>;
count++;
(np->next)->pre = np;
np = np->next;
np->value = cp->value;
cp = cp->next;
}
end = np; 
}
int main()
{
Flist<int> listf;
int a[5] = {1,2,3,4,5};
for (int i = 0 ; i < 5 ; i++)
listf.push_front(a[i]);
cout << "lisrfsize: " << listf.Listsize() << endl;
for (int i = 0 ; i < 5 ; i++)
listf.push_back(a[i]); 
cout << "lisrfsize: " << listf.Listsize() << endl; 
cout << "listf is empty? : " << listf.Isempty() << endl; 
Flist<int> listf2(listf);
cout << "Listf2size : " << listf2.Listsize() << endl;
listf2.del_front();
listf2.del_back();
cout << "Listf2 front :" << listf2.Listfront() << endl;
cout << "Listf2 end :" << listf2.Listend() << endl;
cout << "Listf2 size :" << listf2.Listsize() << endl;
for (int i = 1; i <= listf2.Listsize(); i++) 
  cout << listf2.ShowKey(i) << endl;


return 0;
}


資料結構 雙向鏈表

這是我以前做的作業,你相應的做些修改應該就沒有多大問題了,不會的話再找我,我線上。
include"stdio.h"
#include<malloc.h>

typedef char ElemType;

typedef struct LNode
{ElemType data;
struct LNode *next;
}LinkList;

void CreatListF(LinkList *&L,ElemType a[],int n) //頭插法建表
{
LinkList *s;int i;
L=(LinkList *)malloc(sizeof(LinkList));
L->next=L;
for(i=0;i<n;i++)
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=a[i];
s->next=L->next;
L->next=s;
}
}

void CreateListR(LinkList *&L,ElemType a[],int n) //尾插法建表
{
LinkList *s,*r;int i;
L=(LinkList *)malloc(sizeof(LinkList));
r=L;
for(i=0;i<n;i++)
{
s=(LinkList *)malloc(sizeof(LinkList));
s->data=a[i];
r->next=s;
r=s;
}
r->next=L;
}

void InitList(LinkList *&L) //初始化線性表
{
L=(LinkList *)malloc(sizeof(LinkList));
L->next=L;
}

void DestroyList(LinkList *&L) //銷毀線性表
{
LinkList *p=L->next,*q=p->next;
while(q!=L)
{
free(p);
p=q;
q=p->next;
}
free(p);
}

int ListEmpty(LinkList *L) //判斷線性表是否為空白
{
return(L->next==L);
}

int ListLength(LinkList *L) //求線性表的長度
{
LinkList *p=L;int n=0;
while(p->next!=L)
{
n++;p=p->next;
}
return(n);
}

void DispList(LinkList *L) //輸出線性表
{
LinkList *p=L->next;
while(p!=L)
{
printf("%c",p->data);
p=p->next;
}
}

int GetElem(LinkList *L,int i,ElemType &e) //求線性表中某個資料元素值
{
int j=1;
LinkList *p=L->next;
while(j<i&&p!=L)
{
j++;......餘下全文>>
 
c++資料結構雙向鏈表的問題

你錯了兩個地方。。我幫你改了,你再仔細看看吧。。記得多加分啊。呵呵
#include "stdio.h"
#include<iostream.h>
#define elemtype int
class dulink
{
public:
elemtype data;
dulink *next,*prior;
};
class dulinklist
{
protected:
dulink *head;
public:
//頭插法建立帶頭結點的雙向鏈表
dulink *hcreat()
{
dulink *p,*s;
elemtype i;
cout<<"輸入多個節點數值(用空格分隔),為0時演算法結束";
cin>>i;
p=new dulink;
p->prior=NULL;
p->next=NULL;
while(i) //當輸入資料不為0時,迴圈建雙向鏈表
{
s=new dulink;
s->data=i;
s->next=p->next;
if(s->next)
s->next->prior=s;
s->prior=p;
p->next=s;
cin>>i;
}
return p;
}
//輸出雙向列表
void print(dulink *head)
{
dulink *p;
p=head->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}

}
};
void main()
{ dulink *p;
dulinklist h;
p=h.hcreat();
h.print(p);
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.