void SLList::Insert(int contents) {
	if(head_ == NULL) {
		InsertHead(contents);
	}
	else if(contents < GetHead()) {
		InsertHead(contents);
	}
	else if (head_->next_node() == NULL && head_ != NULL) {
		InsertTail(contents);
	}
	else if(contents > GetTail()) {
		InsertTail(contents);
	}
	else {
		SLNode* node = new SLNode(contents);
		SLNode* i = head_;
		SLNode* j = NULL;
		while(i->contents() <= contents && i->next_node() != NULL) {
			j = i;
			i = i->next_node();
		}
		j->set_next_node(node);
		node->set_next_node(i);
		size_++;
	}
}
Example #2
0
void SLList::Insert(int contents)
{
    //Checks if head is null or contents is < head_->contents();
    if (head_ == NULL || head_->contents() > contents)
    {
        InsertHead(contents);
    } else if ( tail_->contents() < contents)
    {
      //  cout << " got it: " << contents << endl;
     //   cout << ToString();
        InsertTail(contents);
    } else 
    {
        //creates iterator and newnode
        SLNode* iterator;
        iterator = head_;
        while (iterator->next_node() != NULL && contents > iterator->next_node()->contents())
        {
        iterator = iterator->next_node();
        }
        if (iterator != tail_)
        {
            SLNode *newNode = new SLNode(contents);
            newNode->set_next_node(iterator->next_node());
            iterator->set_next_node(newNode);
            size_ +=1;
        } else
        {
            InsertTail(contents);
        }
    }
}
Example #3
0
void *addItemToList(tList *pList, void *pItem, int(*fcmp)(void *pItList, void *pItNew))
{
    if(pItem)
    {
        if(pList->phead)
        {
            pList->pcurr=pList->phead;
            while(pList->pcurr)
            {
                if(fcmp(pList->pcurr->pdata, pItem) > 0)
                {
                    InsertBefore(pList, pItem);
                    break;
                } 
                else if(pList->pcurr == pList->ptail)
                {
                    InsertTail(pList, pItem);
                    break;
                }
                GetNext(pList);
            }
        }
        else InsertHead(pList, pItem); 
    }
    return 0;

}
Example #4
0
void main()
{
  LinkList list;

  Initialization(&list);
  int i;
  for (i=0; i<10; i++)
  {
    InsertTail(list, i);
  }
  PrintList(list);
  printf("List length: %d\n", ListLength(list));

  InsertTail(list, 5);
  FindDup(list);
}
Example #5
0
void SLList::Insert(int data)
{  
  
  SLNode* insert = new SLNode(data); //assigns data to new node 
  SLNode* current = head_; //assigns head to current
  SLNode* prev = current;
  if(!head_ || data <= current->contents()) //if there is a list or data is less than or equal to head_                                                          
  {
    InsertHead(data); 
  }
  else if(data > tail_->contents()) //if data is greater than tail
  {
    InsertTail(data);
  }
  else
  {
  while(current->contents()  < data) //while current is less than data
  {
    prev = current; //we assign prev to currents position
    current = current->next_node(); //then assign current to next position
  }
  insert->set_next_node(current); //set node at currents position
  prev->set_next_node(insert); //set data at proper position
  size_++;
  }
}
void DLList::Insert(int new_node)
{
    DLNode *temp = new DLNode(new_node);
    //creates temp node and makes it point at the new_node information
    DLNode *iterator = head_;
    //creates an iterator and makes it point to head_
    DLNode *iterator2 = tail_;
    //second iterator to trail behind the first iterator
    unsigned int original_size = size_;
    
    if(iterator == NULL)
    //checks if the list is empty
    {
        InsertHead(new_node);
    }
    
    while(iterator != NULL && original_size == size_)
    {
        if((iterator -> contents()) >= (temp -> contents()))
        //checks if the iterator is larger than or equal to the new node
        {
            if(iterator2 == NULL)
            {
                InsertHead(new_node);
            }
            
            else
            {
                iterator2 -> set_next_node(temp);
                //makes the previous node point at the new node
                temp -> set_next_node(iterator);
                //makes the new node point at the next node
                iterator -> set_previous_node(temp);
                //makes the next node point at the new node
                temp -> set_previous_node(iterator2);
                //makes the new node point the previous node
                size_++;
            }
        }
        
        else if((iterator -> contents()) < (temp -> contents()))
        //checks if the iterator is smaller than the new node
        {
            if((iterator -> next_node()) == NULL)
            {
                InsertTail(new_node);
            }
            
            else
            {
                iterator2 = iterator;
                iterator = iterator -> next_node();
            }
        }
    }
}
Example #7
0
int main(){
  int i;
  node *root = (node*) malloc( sizeof(node) );
  root->value = 2;
  root->next = NULL;  

  DisplayList(root);
  for(i = 3; i < 8; i++)
    InsertTail(&root, i);
	
  for(i = 1; i >= 0; i--)
	InsertHead(&root, i);
  	
  DisplayList(root);

  ClearList(&root);  
  
  DisplayList(root);
  
  return 0;
}
Example #8
0
/* Generiert ein neues Telefonlistenmodell fuer
 * eine Telefonliste.
 * Dazu holt sie sich die Telefonliste mit dem 
 * entsprechendem Namen, prueft diese auf Existenz,
 * generiert ein neues Modell (GtkListStore) und 
 * Modell-Listen-Element (tPhoneListStore), setzt 
 * den Namen und das Modell und ruft die Fkt. zum
 * fuellen des Modells auf.
 * Args:
 *   name .. Name der Liste
 * Ret: 
 *   Nichts
 * */
void addPhoneModelList(char *name){
  tList *list = getPhoneList(name);
  GtkListStore *store;
  char *listName;
  tPhoneListStore * newList;
  
  if (phoneModelLists == NULL){
    phoneModelLists = CreateList();
  }
  store = gtk_list_store_new(PHONE_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
  
  listName = malloc(sizeof(char)*strlen(name)+2);
  strcpy(listName, name);

  newList = malloc(sizeof(tPhoneListStore));
  newList->store = store;
  newList->name = listName;

  InsertTail(phoneModelLists, newList);
  fillPhoneModel(store, list);
}
Example #9
0
void main(void) {
    int choose, i, insertdata, num;
    ListNode* p;
    FILE* fin;

    if ((fin = fopen("List.in", "r")) == NULL) {
        printf("File can not be opened, program terminate.");
        exit(1);
    }

    listA = (ListNode*)malloc(sizeof(ListNode));
    listA->next = NULL;
    fscanf(fin, "%d", &insertdata);

    while (!feof(fin)) {
        InsertTail(listA, insertdata);
        fscanf(fin, "%d", &insertdata);
    }

    fclose(fin);

    while (1) {
        printf("\n串列內容( Content of list )=>");
        PrintList(listA);
        printf("\n(1)附加節點(Append new node)\n(2)插入節點(Insert new node)\n(3)刪除節點(Delete node)\n(0)結束(exit)=>");
        scanf("%d", &choose);

        switch (choose) {
        case 0:
            FreeAllNode(listA);     /*釋放所有節點*/
            exit(0);            /*結束程式*/

        case 1:
            printf("請輸入欲附加之資料(Input new data )=>");
            scanf("%d", &insertdata);
            InsertTail(listA, insertdata);
            break;

        case 2:
            printf("請輸入欲插入之資料(Input new data)=>");
            scanf("%d", &insertdata);
            printf("請輸入欲插入之位置(New Position)=>");
            scanf("%d", &num);

            for (i = 1, p = listA; i != num && p != NULL; p = p->next, i++);

            if (p == NULL) {
                printf("插入失敗 ( Insert Failed )");
            } else if (InsertAfter(p, insertdata) == 0) {
                printf("插入失敗 ( Insert Failed )");
            }

            break;

        case 3:
            printf("請輸入欲刪除之資料 ( data to be deleted )=>");
            scanf("%d", &num);

            for (i = 0, p = listA; p != NULL && p->data != num; p = p->next, i++);

            if (p == NULL) {
                printf("此資料不在串列中( the data is not in list )");
            } else if (DeleteNode(listA, p) == 0) {
                printf("刪除失敗( Delete Failed)");
            }

            break;

        default:
            printf("選項錯誤 ( Wrong option ) !!");
        }
    }
}