so cool. Thanks, Salem !!! I started to do it that way, but wasn't sure.
now lastly, how to delete a node with a given value:
instructor said this is a bit tricky to do. but he didn't give any hints about it.
Code:
/*********************/
/* stack.c */
/*********************/
#include <stdio.h>
#include <stdlib.h>
struct node {
int data; /* 1st item of node */
struct node *next; /* 2nd item of node */
};
struct node *first = NULL; /* global variable */
struct node *new_node; /* need pointers for
dynamic mem alloc */
void push(int insert_value);
void pop();
void print_list();
void choice_list(void);
void count_nodes();
struct node *search_list(struct node *list, int n);
struct node *delete_from_list(struct node *list, int n);
void count_nodes()
{
int count;
for (new_node = first; new_node != NULL;
new_node = new_node->next) {
count++;
}
printf("The number of nodes in your list is %d\n\n",
count);
}
void push(int insert_value)
{
new_node=malloc(sizeof(struct node));
new_node->data = insert_value;
new_node->next = first;
first = new_node;
}
void pop()
{
if (first == NULL) {
return ;
}
else {
printf("You want to delete %d.\n",first->data);
first = first->next;
printf("So here is your list now :\n");
print_list();
return ;
}
}
void print_list()
{
if (new_node == NULL) {
return ;
}
else {
while (new_node != NULL) {
printf("%d -> ",new_node->data);
new_node = new_node->next;
}
printf("NULL\n\n");
}
}
void choice_list(void)
{
printf("Choose one of the following.\n"
"0 To end program\n"
"1 To add a value to list\n"
"2 To remove a value from list\n"
"3 To count number of nodes in list\n"
"4 To delete a specific value in list\n");
}
main()
{
int number,choice;
int x;
for( ; ; ) {
choice_list();
printf("Enter your choice :");
scanf("%d",&choice);
printf("\n");
switch(choice) {
case 0:
printf("You have ended the program.\n"
"Good-bye !!\n\n");
exit(0);
case 1:
printf("Enter a number integer : ");
scanf("%d",&number);
push(number);
print_list();
break;
case 2:
pop();
break;
case 3:
count_nodes();
break;
*/ not sure about this at all */
case 4:
printf("Which value in list do you want to delete?"\n);
scanf("%d",first->data);
search_list(????,first->data);
delete_from_list(?????,first->);
break;
default:
printf("That is an invalid choice\n\n");
break;
}
printf("\n");
}
printf("End of run.\n");
return 0;
}
so here's my first stab at the functions search_list and
delete_from_list which look like they are both used to find the value to delete and then implement the deletion itself.
Please critique as you wish. Be kind, though. I am a newbie trying to make sense of this difficult language.
Code:
/* search_list : finds a value in your list */
struct node *search_list(struct node *list, int n)
{
for (; list != NULL; list = list->next)
if (list->data == n)
return list;
return NULL;
}
/* delete_from_list : deletes value/node from list when given a value */
struct node *delete_from_list(struct node *list, int in)
{
for (cur = list, prev = NULL;
cur != NULL && cur -> data !=n;
prev = cur, cur = cur->next)
;
if (cur == NULL)
return list; /*n was not found */
if (prev == NULL)
list = list->next; /* n is in the first node */
else
prev->next = cur->next; /* n is in some other node */
free (cur);
return list;
}
my biggest questions
Where do you declare CUR and PREV to be the current and previous nodes ?
What the heck is list for exactly (first and new_node are already pointers used do I need another pointer?) and how do you call the functions where list is an argument?