Skip to content

Instantly share code, notes, and snippets.

Created October 17, 2013 20:41
Show Gist options
  • Select an option

  • Save anonymous/7031843 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/7031843 to your computer and use it in GitHub Desktop.
I begin my initializing a head, then I call add_head (which replaces the head), and then I call add_tail (which adds a tail). No error here. Then I call add_id, which passes an id number to the function which creates an img with that id and then adds it to the list in the appropriate order. It works except for when the id is greatest and (conseq…
#include <iostream>
using namespace std;
int const IMGSIZE = 512;
struct img{
int id;
char pxls[IMGSIZE][IMGSIZE];
img *next;
};
int add_tail(img *head);
int add_head(img* &head);
void clear_pxls(char pxls[IMGSIZE][IMGSIZE]);
int add_id(img* &head, int ident);
int delete_id(img* &head, int id);
int main(){
img *head;
head = new img;
if (head == NULL)
cout << "Error in initializing head" << endl ;
head->id = 1;
head->next = NULL;
int error_code1 = add_tail(head);
cout << error_code1 << endl;
int error_code2 = add_head(head);
cout << error_code2 << endl << endl;
cout << head->id << endl;
cout << head->next->id << endl;
cout << head->next->next->id << endl << endl;
cout << "Call add_id function with ID = 12:" << endl << endl;
int id = 12;
int error_code3 = add_id(head, id);
cout << "Error code: " << error_code3 << endl;
img *tempy;
tempy = head;
cout << "Print ID's: " << endl;
cout << tempy->id << " " << tempy->next->id << " " ;
cout << tempy->next->next-> id << endl << endl;
while(tempy != NULL){
cout << tempy->id << " " ;
tempy = tempy->next;
}
return 0;
}
int add_tail(img *head){
img *temp;
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = new img;
temp->next->id = temp->id + 1;
clear_pxls(temp->next->pxls);
temp->next->next = NULL;
if (temp->next != NULL)
return 0;
else
return head->id;
}
int add_head(img* &head){
img *temp;
temp = new img;
if (temp == NULL)
return 1;
clear_pxls(temp->pxls);
temp->next = head;
head = temp;
int count = 1;
while(temp != NULL){
temp->id = count;
temp = temp->next;
++count;
}
return head->id;
}
int add_id(img* &head, int id){
img *temp1 = new img;
if (temp1 == NULL)
return 1;
temp1->id = id;
clear_pxls(temp1->pxls);
img *temp2 = head;
if(id <= temp2->id){
temp1->next = temp2;
head = temp1;
return 0;
}
while (id > temp2->next->id && temp2 != NULL)
temp2 = temp2->next;
if(temp2->next != NULL){
temp1->next = temp2->next;
temp2->next = temp1;
}
else{
temp2->next = temp1;
temp1->next = NULL;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment