First off let me say this is not Hw im trying to remember how linked lists work etc for my upcoming fall class: However what im trying to do is traverse a linear linked list thats sorted and when i find the node im looking for to delete it. Its been a while since ive messed with data structures so im trying to refresh myself. Please see the code i have below and tell me why only 6,5,4,3,2,1 prints out. If you could correct it in code I would greatly appreciate it.
Code:
#include<stdlib.h>
#include<stdio.h>
struct list_el {
int val;
struct list_el * next;
};
typedef struct list_el item;
int main() {
item * curr, * head, * temp;
int i;
head = NULL;
for(i=1;i<=10;i++) {
curr = malloc(sizeof(item));
curr->val = i;
curr->next = head;
head = curr;
}
curr = head;
int zz,number=10;
for(zz=1;zz<=10;zz++)
{
if(curr->val==5)
{
temp->next = curr->next;
//printf("You have found the number 5 so im gonna delete it\n");
}
else if(curr->val!=5)
{
printf("current possition = %d\n", number);
number--;
temp = curr;
curr = curr->next;
}
}
curr=temp;
while(curr) {
printf("%d\n", curr->val);
curr = curr->next ;
}
system("PAUSE");
return 0;
}