We are storing a list of student info, (id number, First name and Last name) usi
ID: 3569883 • Letter: W
Question
We are storing a list of student info, (id number, First name and Last name) using a link list. The ID is the key field. Your program should implement a linked list using arrays. Your program should process the following operations. 1) Add an item to the linked list?ex. 1 254 John Smith 2) Print the linked list in logical order?ex. 2 3) Print the physical order of the linked list including list, avail and next.?ex. 3 4) Print a count of how many items are in the linked list.?ex. 4 Bonus (you must get 1-4 working before attempting 5)?5) Delete an item from the linked list?
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
typedef int stack_data; //stack data type - set for integers, modifiable
typedef struct stack Node; //short name for the stack type
struct stack //stack structure format
{
stack_data data;
Node *next;
};
int stack_len(Node *node_head); //stack length
void stack_push(Node **node_head, stack_data d); //pushes a value d onto the stack
stack_data stack_pop(Node **node_head); //removes the head from the stack & returns its value
void stack_print(Node **node_head); //prints all the stack data
void stack_clear(Node **node_head); //clears the stack of all elements
void stack_snoc(Node **node_head, stack_data d); //appends a node
int stack_elem(Node **node_head, stack_data d); //checks for an element
int main(void)
{
Node *node_head = NULL; //pointer to stack head
//example usage:
stack_push(&node_head, 7); //push value 7 onto stack
printf("%d ", node_head -> data); //show stack head value
stack_push(&node_head, 21); //push value 21 onto stack
stack_print(&node_head); //print the stack
if(stack_elem(&node_head, 7)) puts("found 7"); //does 7 belong to the stack?
stack_snoc(&node_head, 0); //append 0 to the end of the stack
stack_print(&node_head); //print the stack
printf("%d ", stack_pop(&node_head)); //pop the stack's head
stack_print(&node_head); //print the stack
stack_clear(&node_head); //clear the stack
stack_print(&node_head); //print the stack
getchar();
return 0;
}
int stack_len(Node *node_head)
{
Node *curr = node_head;
int len = 0;
while(curr)
{
++len;
curr = curr -> next;
}
return len;
}
void stack_push(Node **node_head, stack_data d)
{
Node *node_new = malloc(sizeof(Node));
node_new -> data = d;
node_new -> next = *node_head;
*node_head = node_new;
}
stack_data stack_pop(Node **node_head)
{
Node *node_togo = *node_head;
stack_data d;
if(node_head)
{
d = node_togo -> data;
*node_head = node_togo -> next;
free(node_togo);
}
return d;
}
void stack_print(Node **node_head)
{
Node *node_curr = *node_head;
if(!node_curr)
puts("the stack is empty");
else
{
while(node_curr)
{
printf("%d ", node_curr -> data); //set for integers, modifiable
node_curr = node_curr -> next;
}
putchar(' ');
}
}
void stack_clear(Node **node_head)
{
while(*node_head)
stack_pop(node_head);
}
void stack_snoc(Node **node_head, stack_data d)
{
Node *node_curr = *node_head;
if(!node_curr)
stack_push(node_head, d);
else
{
//find the last node
while(node_curr -> next)
node_curr = node_curr -> next;
//build the node after it
stack_push(&(node_curr -> next), d);
}
}
int stack_elem(Node **node_head, stack_data d)
{
Node *node_curr = *node_head;
while(node_curr)
{
if(node_curr -> data == d) //set for numbers, modifiable
return 1;
else
node_curr = node_curr -> next;
}
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.