Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct listNode {
- int number;
- struct listNode *nextPtr;
- };
- typedef struct listNode LISTNODE;
- typedef LISTNODE *LISTNODEPTR;
- // insert new number into ordered list
- void insert(LISTNODEPTR *, int);
- // remove number(s) from list, returns 1 if match is found, 0 otherwise
- int delete(LISTNODEPTR *, int);
- // print all numbers in list in a loop
- void printList(LISTNODE *);
- // returns 1 if list is empty, 0 otherwise
- int isEmpty(LISTNODE *);
- int main ()
- {
- //LISTNODE b = {24,NULL};
- LISTNODE a = {1,NULL};
- LISTNODEPTR ptr = &a;
- printList(&a);
- insert(&ptr,23);
- insert(&ptr,10);
- insert(&ptr,12);
- insert(&ptr,15);
- printList(&a);
- return 0;
- }
- void printList(LISTNODE *listPtr){
- printf("\nprinting list:\n");
- while(1){
- printf("%d", (*listPtr).number);
- if((*listPtr).nextPtr != NULL){
- listPtr = (*listPtr).nextPtr;
- printf("-");
- }else{
- break;
- }
- }
- }
- void insert(LISTNODEPTR *list, int number){
- if((**list).nextPtr == NULL){
- printf("nextp == null %d\n",number);
- LISTNODE *newNode = malloc(sizeof(LISTNODE));
- (*newNode).number = number;
- (*newNode).nextPtr = NULL;
- (**list).nextPtr = newNode;
- }else{
- printf("other %d\n",number);
- LISTNODE *listPtr = *list;
- LISTNODE *listPtr1 = (*listPtr).nextPtr;
- while((*listPtr1).number < number && (*listPtr).nextPtr != NULL ){
- printf("%d > %d\n",(*listPtr).number , number);
- listPtr = (*listPtr).nextPtr;
- listPtr1 = (*listPtr).nextPtr;
- }
- LISTNODE *newNode = malloc(sizeof(LISTNODE));
- (*newNode).number = number;
- if((*listPtr).nextPtr != NULL){
- (*newNode).nextPtr = listPtr1;
- }else{
- (*newNode).nextPtr = NULL;
- }
- (*listPtr).nextPtr = newNode;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement