Stack danh sách liên kết đơn


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/*
* File: khoa.cpp
* Author: KHOA
*
* Created on August 26, 2016, 9:16 AM
*/

#include
#include
#include
using namespace std;

struct Node {
int data;
Node *next;
};

struct List {
Node *head;
Node *tail;
};

// khoi tao stack

void Init[List &s] {
s.head = s.tail = NULL;
}

// kiem tra stack co rong khong

bool isEmpty[List &s] {
if [s.head == NULL] return true;
else return false;
}

// tao Node

Node *creat_Node[int x] {
Node *p = new Node;
if [p != NULL] {
p->data = x;
p->next = NULL;
} else return NULL;
}

// Push vao stack

void push[List &s, int x] {
Node *p = creat_Node[x];
if [isEmpty[s]] {
s.head = s.tail = p; // neu stack rong thi ta gan p la phan tu dau va cuoi
} else {
p->next = s.head; // cho phan tu ke tiep bang phan tu dau
s.head = p; // cap nhat phan tu dau la phan tu moi them vao
}
}

// Pop phan tu ra khoi stack

void pop[List &s] {
Node *p = s.head;
if [!isEmpty[s]] {
s.head = p->next;
delete p;
} else {
cout data == x] {
return q;
} else {
q = q->next;
}
}
return NULL;
}
// Nhap du lieu cho Stack

void nhap[List &s] {
int n;
cout >n;
for [int i = 1; i

Chủ Đề