Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void createList(List &L) {
*/
//-------------your code here-------------
cout<<"your code here"<<endl;

first(L) = NULL;

//----------------------------------------
}
Expand All @@ -19,7 +19,9 @@ address allocate(infotype x) {
address P = NULL;
//-------------your code here-------------
cout<<"your code here"<<endl;

P = new elmlist;
info(P) = x;
next(P) = NULL;

//----------------------------------------
return P;
Expand All @@ -32,7 +34,7 @@ void deallocate(address &P) {
//-------------your code here-------------
cout<<"your code here"<<endl;


delete(P);
//----------------------------------------
}

Expand All @@ -43,7 +45,8 @@ void insertFirst(List &L, address P) {
*/
//-------------your code here-------------
cout<<"your code here"<<endl;

next(P) = first(L);
first(L) = P;


//----------------------------------------
Expand All @@ -56,7 +59,12 @@ void insertLast(List &L, address P) {
*/
//-------------your code here-------------
cout<<"your code here"<<endl;

address Q;
Q = first(L);
while(next(Q)!=NULL){
Q = next(Q);
}
next(Q)= P;

//----------------------------------------
}
Expand All @@ -71,7 +79,11 @@ address findElm(List L, infotype x) {
address P;
//-------------your code here-------------
cout<<"your code here"<<endl;
P = first(L);

while ((P != NULL ) && (info(P)!= x)){
P = next(P);
}

//----------------------------------------
return P;
Expand All @@ -84,7 +96,11 @@ void deleteFirst(List &L, address &P) {
*/
//-------------your code here-------------
cout<<"your code here"<<endl;

if (first(L) != NULL){
P = first(L);
first(L) = next(P);
next(P) = NULL;
}


//----------------------------------------
Expand All @@ -97,6 +113,17 @@ void deleteLast(List &L, address &P) {
*/
//-------------your code here-------------
cout<<"your code here"<<endl;
address Q;
Q = first(L);
if (next(Q) == NULL){
deleteFirst(L,P);
} else {
while (next(next(Q))!= NULL ){
Q = next(Q);
}
P = next(Q);
next(Q) = NULL;
}



Expand All @@ -110,7 +137,13 @@ void printInfo(List L) {
*/
//-------------your code here-------------
cout<<"your code here"<<endl;
address P;
P = first(L);
while(P != NULL){
cout<<info(P)<<", ";
P = next(P);

}

//----------------------------------------
cout<<endl;
Expand All @@ -125,6 +158,9 @@ void insertAfter(List &L, address Prec, address P) {
*/
//-------------your code here-------------
cout<<"your code here"<<endl;
next(P) = next(Prec);
next(Prec) = P;


//----------------------------------------

Expand All @@ -137,6 +173,9 @@ void deleteAfter(List &L, address Prec, address &P) {
*/
//-------------your code here-------------
cout<<"your code here"<<endl;
P = next(Prec);
next(Prec)=next(P);
next(P)=NULL;


//----------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions list.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ typedef struct elmlist *address;

struct elmlist{
//------------- your code here -----------
infotype info;
address next;



//----------------------------------------
};

struct List{
//------------- your code here -----------
address first;

//----------------------------------------
};
Expand Down
17 changes: 17 additions & 0 deletions operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ void insert_sorted(List &L, infotype x) {

//-------------your code here-------------
cout<<"your code here"<<endl;
address Q,P;
if ((first(L) == NULL) || (x < info(first(L)))) {
P = allocate(x);
insertFirst(L,P);
} else if (findElm(L,x)==NULL){
Q = first(L);
while ((next(Q)!= NULL) && (x > info(next(Q)))){
Q = next(Q);
if (next(Q) == NULL){
P = allocate(x);
insertLast(L,P);
}else if (info(P)!=info(Q)){
P = allocate(x);
insertAfter(L,Q,P);
}
}
}


//----------------------------------------
Expand Down