This is a simple demonstration of a singly linked list, from my class work:
#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <stddef.h>
#include <wchar.h>
#include <locale.h>
#include <setjmp.h>
struct node
{
int info;
struct node *link;
};
typedef struct node* NODE;
NODE main()
{
NODE first = NULL;
int item, choice;
for(;;)
{
printf("1.INSERT AT FRONT\n2.DISPLAY\n3.QUIT\n\nPLEASE ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("\nEnter the item to be inserted:");
scanf("%d", &item);
first = insert_front(item,first);
break;
case 2:
display(first);
break;
case 3:
exit(0);
}
}
}
NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if(x == NULL)
{
printf("Out of memory");
exit(0);
}
return x;
}
int insert_front(int item, NODE first)
{
NODE temp;
temp = getnode();
temp->info = item;
temp->link = first;
return temp;
}
void display(NODE first)
{
NODE temp;
if(first == NULL)
{
printf("Empty");
return;
}
printf("\nThe contents are: ");
temp = first;
while(temp != NULL)
{
printf("%d ", temp->info);
temp = temp->link;
}
printf("\n");
}
As you can see, this code demonstrates the working of a linked list. How can this program more smaller, better, more professional and much more good? If you were given a chance to modify this program, how would you do it? I wanna know how to think and how to train myself to modify programs like these. And as you see, I have learned most of C. I referred to and learned C from K.N Kings The C programming language: A modern approach and Computer science: A structured approach using C and data structures from Data structures using C by Tenenbaum.
Now I have stretched my legs in learning C what can I do to create application that might be useful to me or to others. Say I want to create a small game like "snake" or create a small application that my father might use to do his business.