You are on page 1of 2

#include <stdio.

h>
struct node {
char * word;
struct node * next;
};

void addFront(struct node * n, struct node ** head);

int main() {
struct node * head = 0; // head p o i n t e r for the list
struct node n1 = { "hello", 0};
struct node n2 = { "cso201", 0};
struct node n3 = { "students", 0} ;

addFront ( &n1, &head);


addFront ( &n2, &head);
addFront ( &n3, &head);

struct node * current = head;


while (current != 0 ) {
printf ("%s ", current->word );
current = current->next;
}
printf("\n");

delete(head, 2);
printNodes(head);

reverse(head);

printNodes(head);

return 0;
}

void addFront(struct node * n, struct node ** head) {


n->next = *head ;
*head = n;
printf ("%s ", (*head)->word );
}

void reverse(struct node **head){


struct node* prev;
struct node* next;
struct node* curr;

while (curr != 0){


curr->next=prev;
prev=curr;
}
}

void printNodes(struct node *current){


while(current != 0){
printf("%s",current->word);
current= current->next;
}
}

void delete(struct node ** head, int i){


int counter;
struct node* n1;
struct node* n2;
n2=head;
for(counter=1; counter<=i ;counter=counter+1){
if(counter== i-1){
n1=n2;
}
n2= n2->next;
}
n1->next=n2;

You might also like