Tạo Danh Sách Liên Kết

watch_later Thứ Ba, 18 tháng 4, 2017

#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
typedef struct tagNode{
 int info;
 struct tagNode*pNext;
}Node;
typedef struct tagList{
 Node*pHead;
 Node*pTail;
}List;
Node*CreateNode (int x){
 Node*p;
 p=new Node;
 if (p==NULL)
  return NULL;
 p->info=x;
 p->pNext=NULL;
 return p;
}
void CreateList (List &L){
 L.pHead=L.pTail=NULL;
}
void AddHead (List &L, Node*p){
 if (L.pHead==NULL)
 L.pHead=L.pTail=p;
 else {
  p->pNext=L.pHead;
  L.pHead=p;
 }
}
void PrintList (List L){
 Node*p;
 p=L.pHead;
 while (p!=NULL)
 {
  cout<<p->info<<"\t";
  p=p->pNext;
 }
}
int main(){
 int x;
 List L;
 CreateList(L);
 do {
  cout<<"Nhap x: ";
  cin>>x;
  if (x!=0){
   Node*p;
   p=CreateNode(x);
   AddHead(L, p);
  }
 }
 while (x!=0);
 cout<<"Day so vua nhap: "<<endl;
PrintList (L);
getch();
return 0;
}