close
#include <iostream>
#include <cstdlib>
/*寫一隻程式計算戀節串列長度,將A改成Z,將串列反轉 */

using namespace std;

struct node
{
char data;
struct node *next;
};


int printlinkedlist(struct node *head)
{
while((head)!=NULL)
    {
    cout<<"head->data:"<<head->data<<endl;
head=head->next;
}
  
}

int countList(struct node *head)
{
   int count=1;
   
    while((head->next)!=NULL)
    {
    head=head->next;
    count++;
}
return count;
}



void updateList(struct node *head)
{
    while((head->next)!=NULL)
{
if((char)head->data=='A')
{
head->data='Z';
}
head=head->next;
}
}


struct node *reverseList(struct node *h)
{
 node *p; 
 node *q;
    if(h->next!=NULL)
 p=h->next ;
 h->next=NULL;
 
 while(p->next!=NULL)
 {
  q=p;
  p=p->next;
  q->next=h;
  h=q; 
 }
return h; 
}

int main( ) 
{
struct node *head=NULL;
struct node *end=NULL; 
struct node Apple={'A',NULL};
struct node Banana={'B',NULL};
struct node Cake={'C',NULL};
struct node Dog={'D',NULL};
struct node *p=NULL;
head=&Apple;
Apple.next=&Banana;
Banana.next=&Cake;
Cake.next=&Dog;

    
cout<<"head="<<head<<endl;
cout<<"head指向的data值是:"<<(char)head->data<<endl;
cout<<"head的next是"<<(char)head->next->data<<endl; 
head=head->next;
cout<<"head指向下一個"<<(char)head->data<<endl;
head=head->next;
cout<<"head指向下一個"<<(char)head->data<<endl;
head=head->next;
cout<<"head指向下一個"<<(char)head->data<<endl;
    

    
    //計算LinkedList的長度
     cout<<"LinkedList長度為:"<<countList(head)<<endl; 
//修改linked list裡面的值 把'A'改成'Z' 
head=&Apple;
updateList(head); 
cout<<"LinkedList內容為:"<<endl; 
printlinkedlist(head); 
     
 
 
    //reverselist
    cout<<"origin:"<<endl; 
printlinkedlist(head);
head=reverseList(head);
cout<<"reverse:"<<endl; 
printlinkedlist(head);
       


    system("PAUSE");
return 0;
}
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 布拉怡 的頭像
    布拉怡

    nini的部落格

    布拉怡 發表在 痞客邦 留言(0) 人氣()