单链表复习

单链表复习

作者:vpoet

mails:vpoet_sir@163.com

  1 #include <iostream>
  2 #include <stack>
  3 using namespace std;
  4 
  5 typedef struct ListNode
  6 {
  7     int data;
  8     struct ListNode* next;
  9 }NODE;
 10 
 11 NODE *CreateList()
 12 {
 13     NODE * head,*p,*s;
 14 
 15     head=(NODE*)malloc(sizeof(NODE));
 16     p=head;
 17 
 18     int LinkData;
 19     int InputIndex=1;
 20     while(InputIndex)
 21     {
 22         cout<<"Please input the Node data.(if 0 is inputed,CreateLink Over!): ";
 23         cin>>LinkData;
 24         if(0!=LinkData)
 25         {
 26             s=(NODE*)malloc(sizeof(NODE));
 27             s->data=LinkData;
 28             p->next=s;
 29             p=s;
 30         }
 31         else
 32         {
 33             InputIndex=0;
 34         }
 35     }
 36 
 37     head=head->next;
 38     p->next=NULL;
 39 
 40     return head;
 41 }
 42 
 43 void Print(NODE *head)
 44 {
 45     NODE *p=head;
 46     cout<<"The LinkList is: ";
 47     while(p!=NULL)
 48     {
 49         cout<<p->data<<"  ";
 50         p=p->next;
 51     }
 52 }
 53 
 54 void ListLength(NODE*head)
 55 {
 56     
 57     NODE* p=head;
 58     int count =0;
 59     while(p!=NULL)
 60     {
 61         count++;
 62         p=p->next;
 63     }
 64     cout<<"The length is: "<<count<<endl;
 65 }
 66 
 67 void InsertNode(NODE* head)
 68 {
 69     NODE *p=head;
 70     
 71     while(p->next!=NULL)
 72     {
 73         p=p->next;
 74     }
 75 
 76     NODE *New;
 77     New=(NODE*)malloc(sizeof(NODE));
 78     cout<<"Please input the new data: ";
 79     cin>>New->data;
 80     p->next=New;
 81     New->next=NULL;
 82     
 83     head=p;
 84 }
 85 
 86 void ReversePrint(NODE* head)
 87 {
 88     stack <NODE*> StackNode;
 89     NODE *p=head;
 90     while(p!=NULL)
 91     {
 92         StackNode.push(p);
 93         p=p->next;
 94     }
 95     cout<<"Reverse to Print: ";
 96     while(!StackNode.empty())
 97     {
 98         NODE* temp;
 99         temp=StackNode.top();
100         StackNode.pop();
101         cout<<temp->data<<" ";
102     }
103 }
104 
105 void DeleteNode(NODE *head,int DelValue)
106 {
107     NODE *p=head;
108     
109     while(p!=NULL)
110     {
111         if(p->next->data==DelValue)
112         {
113             p->next=p->next->next;
114             break;
115         }
116         else
117         {
118             p=p->next;
119         }
120     }
121     head=p;
122 }
123 
124 
125 int main()
126 {
127     cout<<"Create New LinkList....\n"<<endl;
128     NODE *p=CreateList();
129     cout<<"Print the LinkList.....\n"<<endl;
130     Print(p);
131     cout<<"Print the length of LinkList...\n"<<endl;
132     ListLength(p);
133 
134     cout<<"Insert a New LinkNode....\n"<<endl;
135     InsertNode(p);
136     cout<<"Cout The New LinkNode....\n"<<endl;
137     Print(p);
138 
139     cout<<"Reverse to input the LinkList...\n"<<endl;
140     ReversePrint(p);
141 
142     cout<<"Delete a Node in LinkList....\n"<<endl;
143     int DelValue;
144     cout<<"Please input the Delete Node Value"<<endl;
145     cin>>DelValue;
146     DeleteNode(p,DelValue);
147     cout<<"Print the del Node LinkList....\n"<<endl;
148     Print(p);
149     cout<<endl;
150     return 0;
151 }
posted @ 2015-07-07 13:33  vpoet  阅读(148)  评论(0编辑  收藏  举报