0% found this document useful (0 votes)
15 views18 pages

Struct Node

eqw

Uploaded by

phuthai033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views18 pages

Struct Node

eqw

Uploaded by

phuthai033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

struct Node sll.

pHead = p; Node* pHead;


{ sll.pTail = p; Node* pTail;
int data; }else{ };
Node* pNext; p->pNext =
sll.pHead;
}; void Init(List &sll){
sll.pHead = p;
sll.pHead = sll.pTail
} = NULL;
struct List
} }
{
void InsertFirst(List
Node* pHead;
&sll, int x){
Node *GetNode(int x)
Node* pTail;
Node *NewNode = {
}; GetNode(x);
Node *p = new
if(NewNode == Node;
NULL) return;
void Init(List &sll){ if(p == NULL)
addfirst(sll, return NULL;
sll.pHead = sll.pTail NewNode);
= NULL; p->data = x;
}
} p->pNext = NULL;
return p;
void ShowFirst(List sll)
Node *GetNode(int x) }
{
{
Node *p =
Node *p = new sll.pHead; void addlast(List &sll,
Node;
Node *p){
printf("%d\n", p-
if(p == NULL) >data); if(sll.pHead ==
return NULL;
NULL){
}
p->data = x;
sll.pHead = p;
struct Node
p->pNext = NULL;
sll.pTail = p;
{
return p;
}else{
int data;
}
sll.pTail->pNext
Node* pNext;
= p;
};
void addfirst(List &sll, sll.pTail = p;
Node *p){
}
if(sll.pHead == struct List
}
NULL){
{
Node *GetNode(int x) Node *NewNode =
{ GetNode(x);
void InsertLast(List
&sll, int x){ Node *p = new if (NewNode ==
Node; nullptr) return;
Node *NewNode =
GetNode(x); if(p == 0) return
NULL;
if(NewNode == if(sll.pTail ==
NULL) return; p->data = x; NULL){
addlast(sll, p->pNext = NULL; sll.pHead =
NewNode); NewNode;
return p;
} sll.pTail =
}
NewNode;
}else{
void ShowLast(List sll)
void addfirst(List &sll,
{ sll.pTail->pNext
int x){
= NewNode;
Node *p = sll.pTail;
Node *NewNode =
sll.pTail =
printf("%d", p- GetNode(x);
NewNode;
>data);
if (NewNode ==
}
} nullptr) return;
}
#include<bits/stdc+
+.h>
if(sll.pHead ==
NULL){ List
CreateLinkedListFrom
struct Node sll.pHead =
File(const std::string&
NewNode;
{ fname) {
sll.pTail =
int data; List l;
NewNode;
Node* pNext; std::ifstream
}else{
file(fname);
};
NewNode-
std::string line;
>pNext = sll.pHead;

struct List sll.pHead =


NewNode;
{ while(std::getline(file,
}
Node* pHead; line)){
}
Node* pTail;
std::istringstream
}; iss(line);
void addlast(List &sll,
int x){ int value, option;
char coma; #include<bits/stdc+
+.h>
if(!(iss >> value if(sll.pHead ==
>> coma >> option)) NULL){
{
struct Node sll.pHead =
break; NewNode;
{
} sll.pTail =
int data;
NewNode;
Node* pNext;
}else{
if(option == 0){
};
NewNode-
addfirst(l,
>pNext = sll.pHead;
value);
struct List sll.pHead =
}else if (option
NewNode;
== -1) {
}
{ Node* pHead;
}
addlast(l, Node* pTail;
value);
};
} void addlast(List &sll,
int x){
}
Node *GetNode(int x) Node *NewNode =
return l; { GetNode(x);
} Node *p = new if (NewNode ==
Node; nullptr) return;
if(p == 0) return
void ShowHead(List
NULL;
sll, int n){
if(sll.pTail ==
p->data = x; NULL){
Node *p =
sll.pHead; p->pNext = NULL; sll.pHead =
int cnt = 0; return p; NewNode;

while(p != NULL } sll.pTail =


&& cnt < n){ NewNode;

std::cout << p- }else{


>data << " "; void addfirst(List &sll,
int x){ sll.pTail->pNext
p = p->pNext; = NewNode;
Node *NewNode =
cnt++; GetNode(x); sll.pTail =
NewNode;
} if (NewNode ==
nullptr) return; }
}
} } sll.pHead = sll.pTail
= NULL;
return l;
}
List }
CreateLinkedListFrom
File(const std::string&
Node *GetNode(int x)
fname) { void ShowHead(List
{
sll, int n){
List l;
Node *p = new
Node *p =
std::ifstream Node;
sll.pHead;
file(fname);
if(p == NULL)
int cnt = 0;
std::string line; return NULL;
while(p != NULL
p->data = x;
&& cnt < n){
p->pNext = NULL;
std::cout << p-
while(std::getline(file,
>data << " "; return p;
line)){
p = p->pNext; }
std::istringstream cnt++;
iss(line);
} void InsertFirst(List
int value, option; &l, int x){
}
char coma; Node *p =
struct Node
GetNode(x);
if(!(iss >> value
>> coma >> option)) {
if(p == NULL)
{ int data; return;
break; Node* pNext;
} }; if(l.pHead ==
NULL){
l.pHead = p;
if(option == 0){ struct List
l.pTail = p;
addfirst(l, {
value); }else{
Node* pHead;
}else if (option p->pNext =
== -1) Node* pTail; l.pHead;
{ }; l.pHead = p;
addlast(l, }
value);
void Init(List &sll){ }
}
int SearchKey(List l, Node *p = new }
int x){ Node;
}
Node *p = l.pHead; if(p == NULL)
return NULL;
while(p != NULL){
void ShowList(List sll)
p->data = x;
if(p->data == x) {
{ p->pNext = NULL;
Node *p =
return 1; return p; sll.pHead;
} } while(p != NULL){
p = p->pNext; printf("%d ", p-
>data);
} void addlast(List &sll,
int x){ p = p->pNext;
return -1;
Node *p = }
}
GetNode(x);
}
struct Node
if(p == NULL)
struct Node
{ return;
{
int data;
int data;
Node* pNext; if(sll.pHead ==
NULL){ Node* pNext;
};
sll.pHead = p; };
sll.pTail = p;
struct List
}else{ struct List
{
sll.pTail->pNext {
Node* pHead; = p;
Node* pHead;
Node* pTail; sll.pTail = p;
Node* pTail;
}; }
};
}
void Init(List &sll){
void Init(List &sll){
sll.pHead = sll.pTail void AppendList(List
= NULL; &sll,int V[], int n){ sll.pHead = sll.pTail
= NULL;
}
}
for(int i=0;i<n;i++)
Node *GetNode(int x) {
{ addlast(sll, V[i]);
Node *GetNode(int x) void InsertLast(List if(p ==
{ &sll, int k){ sll.pTail){
Node *p = new Node *NewNode = sll.pTail =
Node; GetNode(k); NewNode;
if(p == NULL) if(NewNode == }
return NULL; NULL) return;
return;
}
p->data = x; if(sll.pHead ==
p = p->pNext;
NULL){
p->pNext = NULL;
}
sll.pHead =
return p;
NewNode; InsertFirst(sll, y);
}
sll.pTail = }
NewNode;

void InsertFirst(List }else{


void ShowList(List sll)
&sll, int k){
sll.pTail->pNext {
Node *NewNode = = NewNode;
Node *p =
GetNode(k);
sll.pTail = sll.pHead;
if(NewNode == NewNode;
while(p != NULL){
NULL) return;
}
printf("%d ", p-
} >data);
if(sll.pHead ==
p = p->pNext;
NULL){
void }
sll.pHead =
FindAndInsert(List
NewNode; }
&sll, int x, int y){
sll.pTail = struct Node
Node *p =
NewNode;
sll.pHead; {
}else{
while(p != NULL){ int data;
NewNode-
if(p->data == x) Node* pNext;
>pNext = sll.pHead;
{
sll.pHead = };
Node
NewNode;
*NewNode =
} GetNode(y);
struct List
} NewNode-
{
>pNext = p->pNext;
Node* pHead;
p->pNext =
NewNode;
Node* pTail; }else{ while(p != NULL){
}; sll.pTail->pNext sum += p-
= NewNode; >data;
sll.pTail = p = p->pNext;
void Init(List &sll){
NewNode;
}
sll.pHead = sll.pTail
}
= NULL; return sum;
}
} }
struct Node
void ShowList(List sll)
Node *GetNode(int x) {
{
{
int data;
Node *p =
Node *p = new
sll.pHead; Node* pNext;
Node;
while(p != NULL){ };
if(p == NULL)
return NULL; printf("%d ", p-
>data);
struct List
p = p->pNext;
p->data = x; {
}
p->pNext = NULL; Node* pHead;
}
return p; Node* pTail;
} };
void AppendList(List
&sll, int V[], int n){
void InsertLast(List void Init(List &sll){
for(int i=0;i<n;i++)
&sll, int k){
{ sll.pHead = sll.pTail
Node *NewNode = = NULL;
InsertLast(sll,
GetNode(k);
V[i]); }
if(NewNode ==
}
NULL) return;
} Node *GetNode(int x)
{
if(sll.pHead ==
NULL){ int SumLinkedList(List Node *p = new
sll){ Node;
sll.pHead =
NewNode; int sum = 0; if(p == NULL)
return NULL;
sll.pTail = Node *p =
NewNode; sll.pHead;
p->data = x; }
p->pNext = NULL; } struct List
return p; {
} void AppendList(List Node* pHead;
&sll, int V[], int n){
Node* pTail;
for(int i=0;i<n;i++)
void InsertLast(List };
{
&sll, int k){
InsertLast(sll,
Node *NewNode =
V[i]); void Init(List &sll){
GetNode(k);
} sll.pHead = sll.pTail
if(NewNode ==
= NULL;
NULL) return; }
}

if(sll.pHead == int
NULL){ CountKLinkedList(List Node *GetNode(int x)
sll, int k){ {
sll.pHead =
NewNode; Node *p = Node *p = new
sll.pHead; Node;
sll.pTail =
NewNode; int cnt = 0; if(p == NULL)
return NULL;
}else{ while(p != NULL){
sll.pTail->pNext if(p->data == k)
= NewNode; { p->data = x;
sll.pTail = cnt++; p->pNext = NULL;
NewNode;
} return p;
}
p = p->pNext; }
}
}
return cnt; void InsertLast(List
void ShowList(List sll) &sll, int k){
}
{
Node *NewNode =
struct Node
Node *p = GetNode(k);
sll.pHead; {
if(NewNode ==
while(p != NULL){ int data; NULL) return;
printf("%d ", p- Node* pNext;
>data);
}; if(sll.pHead ==
p = p->pNext; NULL){
sll.pHead = return temp->data; void InsertLast(List
NewNode; &sll, int k){
}
sll.pTail = Node *NewNode =
struct Node
NewNode; GetNode(k);
{
}else{ if(NewNode ==
int data; NULL) return;
sll.pTail->pNext
= NewNode; Node* pNext;
sll.pTail = }; if(sll.pHead ==
NewNode; NULL){
} sll.pHead =
struct List NewNode;
}
{ sll.pTail =
Node* pHead; NewNode;
void AppendList(List
Node* pTail; }else{
&sll, int V[], int n){
}; sll.pTail->pNext
for(int i=0;i<n;i++)
= NewNode;
{
sll.pTail =
InsertLast(sll, void Init(List &sll){ NewNode;
V[i]);
sll.pHead = sll.pTail }
} = NULL;
}
} }

void ShowList(List sll)


int RemoveHead(List Node *GetNode(int x) {
&sll){ {
Node *p =
if(sll.pHead == Node *p = new sll.pHead;
NULL) return 0; Node;
while(p != NULL){
if(p == NULL)
return NULL; printf("%d ", p-
Node *temp =
>data);
sll.pHead;
p = p->pNext;
sll.pHead = p->data = x;
sll.pHead->pNext; }
p->pNext = NULL;
if(sll.pHead == }
NULL){ return p;

sll.pTail = NULL; }
void AppendList(List
} &sll, int V[], int n){
for(int i=0;i<n;i++) if(p == {
{ sll.pTail){
Student data;
InsertLast(sll, sll.pTail =
Node* link;
V[i]); pos;
};
} }
} delete p;
struct ListStudent
p = pos-
>pNext; {
int
RemoveNodesK(List } Node* first;
&sll, int k){
cnt++; Node* last;
int cnt = 0;
}else{ };
Node *p =
pos = p;
sll.pHead;
p = p->pNext; void
Node *pos = NULL;
Initial(ListStudent &l)
}
{
}
while(p != NULL){ l.first = l.last =
return cnt; NULL;
if(p->data == k)
{ } }
if(p == struct BirthDay{
sll.pHead){
int day; void
sll.pHead = InsertLast(ListStudent
p->pNext; int month;
&l, Student &st){
if(sll.pHead double year;
Node *p = new
== NULL){ }; Node;
sll.pTail = p->data = st;
NULL;
struct Student{ p->link = NULL;
}
string ID;
delete p;
string Name; if(l.first == NULL){
p=
sll.pHead; string Gender; l.first = l.last =
p;
}else{ BirthDay Born;
}else{
pos->pNext };
= p->pNext; l.last->link = p;
l.last = p;
struct Node
} p->data = st;
} struct Student{ p->link = NULL;
string ID;
void string Name; if(l.first == NULL){
ShowListStudent(cons
string Gender; l.first = l.last =
t ListStudent &list) {
p;
BirthDay Born;
Node* current =
}else{
list.first; };
p->link = l.first;
while (current !=
nullptr) { l.first = p;
struct Node
cout << current- }
>data.ID << '\t' {
}
<< current- Student data;
>data.Name << '\t' Node* link;
<< current- void
};
>data.Gender << '\t' ShowListStudent(ListS
tudent list) {
<< current-
>data.Born.month struct ListStudent Node* current =
<< '/' list.first;
{
<< current- while (current !=
Node* first; nullptr) {
>data.Born.day << '/'
Node* last; cout << current-
<< current-
>data.Born.year << }; >data.ID << '\t'
endl; << current-
>data.Name << '\t'
void
current = Initial(ListStudent &l) << current-
current->link; { >data.Gender << '\t'

} l.first = l.last = << current-


NULL; >data.Born.month
} << '/'
}
struct BirthDay{ << current-
>data.Born.day << '/'
int day;
void << current-
int month;
InsertFirst(ListStudent >data.Born.year <<
double year; &l, Student &st){ endl;
}; Node *p = new
Node;
current = void << current-
current->link; Initial(ListStudent &l) >data.Name << '\t'
{
} << current-
l.first = l.last = >data.Gender << '\t'
}
NULL;
<< current-
struct BirthDay{
} >data.Born.month
int day; << '/'

int month; << current-


void
>data.Born.day << '/'
double year; InsertLast(ListStudent
&l, Student &st){ << current-
}; >data.Born.year <<
Node *p = new
endl;
Node;
struct Student{ p->data = st;
string ID; current =
p->link = NULL;
current->link;
string Name;
}
string Gender; if(l.first == NULL){
}
BirthDay Born; l.first = l.last =
}; p;
void swap(Student
}else{
&s1, Student &s2){
struct Node l.last->link = p;
Student temp = s1;
{ l.last = p;
s1 = s2;
Student data; }
s2 = temp;
Node* link; }
}
}; //xap sep noi bot
void
void sort(Student
ShowListStudent(ListS
Sts[], int n){
struct ListStudent tudent list) {
for(int i=0;i<n-1;i+
{ Node* current =
+){
list.first;
Node* first;
for(int j=0;j<n-i-
while (current !=
Node* last; 1;j++){
nullptr) {
}; if(Sts[j].ID
cout << current-
>Sts[j+1].ID){
>data.ID << '\t'
swap(Sts[j],
Sts[j+1]);
} { }
} Student data; }
} Node* link;
} }; void
AppdendStudents(List
Student &l, Student
void struct ListStudent Sts[], int n){
InsertOrderStudents(L
{ for(int i=0;i<n;i++)
istStudent &l, Student
{
Sts[], int n){ Node* first;
InsertLast(l,
sort(Sts, n); Node* last; Sts[i]);
}; }
for(int i=0;i<n;i++)
}
{
void
InsertLast(l, Initial(ListStudent &l)
Sts[i]); { int
RemoveIDStudent(List
} l.first = l.last = Student &L, string k){
NULL;
}
int cnt = 0;
}
struct BirthDay{
Node *p = L.first;
int day;
Node *pos = NULL;
void
int month;
InsertLast(ListStudent while(p != NULL){
double year; &l, Student &st){
if(p->data.ID ==
}; Node *p = new k){
Node;
if(p == L.first)
p->data = st; {
struct Student{
p->link = NULL; L.first =
string ID; L.first->link;
string Name; if(L.first ==
if(l.first == NULL){ NULL){
string Gender;
l.first = l.last = L.last =
BirthDay Born;
p; NULL;
};
}else{ }
l.last->link = p; delete p;
struct Node
l.last = p; p = L.first;
}else{ cout << current-
>data.ID << '\t'
pos->link = struct ListStudent
p->link; << current-
{
>data.Name << '\t'
if(p ==
Node* first;
L.last){ << current-
>data.Gender << '\t' Node* last;
L.last =
pos; << current- };
>data.Born.month
}
<< '/'
delete p; void
<< current-
>data.Born.day << '/' Initial(ListStudent &l)
p = pos-
{
>link;
<< current-
>data.Born.year << l.first = l.last =
}
endl; NULL;
cnt++;
}
}else{
current =
pos = p; current->link; void
p = p->link; } InsertLast(ListStudent
&l, Student &st){
} }
Node *p = new
} #include <sstream> Node;
if(cnt == 0){
p->data = st;
return -1; struct Student{ p->link = NULL;
}else{ string ID;
return cnt; string Name; if(l.first == NULL){
} string Gender; l.first = l.last =
} p;
double Grade;
}else{
};
void l.last->link = p;
ShowListStudent(ListS
l.last = p;
tudent list) { struct Node
}
Node* current = {
list.first; }
Student data;
while (current !=
Node* link;
nullptr) {
};
void void while (current !=
ReadStudent(ListStud SLListSort(ListStudent nullptr && count < n)
ent &l, string fn){ &l){ {
ifstream file(fn); if (l.first == nullptr) cout << current-
return; >data.ID << '\t'
if(!file.is_open()){
<< current-
return;
>data.Name << '\t'
bool swapped;
}
<< current-
do {
string line; >data.Gender << '\t'
swapped = false;
<< current-
Node *current = >data.Grade <<
while(getline(file, endl;
l.first;
line)){
while (current- current =
istringstream current->link;
>link != nullptr) {
iss(line);
if (current- count++;
Student st;
>data.Grade >
}
string grade; current->link-
>data.Grade) { }
void ReadInt(char*
getline(iss, st.ID, swap(current->data, fileName) {
','); current->link->data);
FILE* fp = fopen
getline(iss, swapped = (fileName, "r");
st.Name, ','); true;
if (fp == NULL)
getline(iss, }
st.Gender, ','); {
current =
getline(iss, printf("Error!
current->link;
grade, ','); Could not open file\
} n");
st.Grade =
stod(grade); } while (swapped); exit(1);

InsertLast(l, st); } }

} // CODE HERE
void int so;
ShowStudentHeads(c
fscanf(fp, "%d",
onst ListStudent &list,
&so);
file.close(); int n) {
printf("%d\n", so);
} Node* current =
list.first;
int count = 0;
fclose(fp); if (file == NULL) {
} fclose(fp); printf("Không
thể mở file!\n");
void ReadChar(char }
*filename){ return;
#include <stdio.h>
FILE *fp = }
void FileSum(char
fopen(filename, "r");
*fileName) { char
if(fp == NULL){ line[MAX_LINE_LENGT
FILE *file =
H];
printf("Error! fopen(fileName, "r");
Could not open file"); // Đọc từng dòng và
if (file == NULL) {
xuất ra màn hình
exit(1);
printf("Error!
while (fgets(line,
} Could not open file\
sizeof(line), file) !=
n");
NULL) {
return;
char kytu; printf("%s", line);
}
fscanf(fp, "%c", }
&kytu); int number;
fclose(file);
printf("%c", kytu); int sum = 0;
}
while (fscanf(file,
void
"%d", &number) ==
fclose(fp); ReadEleArrFile(const
1) {
char* filename) {
}
sum +=
FILE* file =
void ReadChar(char number;
fopen(filename, "rb");
*filename){
}
if (file == NULL) {
FILE *fp =
fclose(file);
fopen(filename, "r"); printf("Không
printf("%d\n", thể mở file %s\n",
if(fp == NULL){ filename);
sum);
printf("Error! return;
}
Could not open file");
#define }
exit(1);
MAX_LINE_LENGTH
int number;
} 256
while
void
(fread(&number,
ShowContent(const
char kytu; sizeof(int), 1, file)) {
char *fileName) {
fscanf(fp, "%c", printf("%d ",
FILE *file =
&kytu); number);
fopen(fileName, "r");
printf("%c", kytu); }
fclose(file); FILE* file = void
fopen(filename, "rb"); ShowBalance(const
}
char* filename) {
if (file == NULL) {
int
FILE* file =
SumNegEleArrFile(con printf("Không
fopen(filename, "r");
st char* filename) { thể mở file %s\n",
filename); if (file == NULL) {
FILE* file =
fopen(filename, "rb"); return -8080; printf("Không
thể mở file %s\n",
if (file == NULL) { }
filename);
printf("Không fseek(file, 0,
return;
thể mở file %s\n", SEEK_END);
filename); }
long fileSize =
return 0; ftell(file); char line[256];
} int numElements = while (fgets(line,
fileSize / sizeof(int); sizeof(line), file)) {
printf("%s",
int number;
line);
if (index < 0 ||
int sum = 0;
index >=
numElements) {
}
while fclose(file);
(fread(&number,
return -8080;
sizeof(int), 1, file)) { fclose(file);
}
if (number < 0) }
{
void
sum += fseek(file, index * ShowAccountNumber(
number; sizeof(int), const char* filename)
SEEK_SET); {
}
FILE* file =
}
fopen(filename, "r");
int value;
if (file == NULL) {
fread(&value,
fclose(file);
sizeof(int), 1, file); printf("Không
return sum; thể mở file %s\n",
filename);
}
fclose(file);
return;
int
FileIndexArrBi(const }
char* filename, int return value;
index) {
}
char line[256]; printf("Error!
Could not open file\
int
n");
accountNumber; //
Biến lưu số tài khoản exit(1);
char }
fullName[100];
// CODE HERE
float balance;
int so;
while(fscanf(fp,
// Đọc từng dòng "%d", &so) == 1){
dữ liệu
printf("%d ", so);
while (fgets(line,
}
sizeof(line), file)) {
fclose(fp);
// Tách thông tin
từ dòng printf("\n");
sscanf(line, "%d
%99[^\n] %f",
&accountNumber, }
fullName, &balance);
printf("%d\n",
accountNumber);

// Đóng file sau khi


đọc xong
fclose(file);
}
void
ShowVaulues(char*
fileName) {
FILE* fp = fopen
(fileName, "r");
if (fp == NULL)
{

You might also like