So I have yet to take the data structures course at my university but I was asked for help by someone who is in said class so I decided to take a look and see what I could come up with code wise.

The original assignment was supposed to be in Java with a GUI for pushing and popping as well as to display the current stack. I decided to try my had at doing it in C with stdio first since that is what I am more familiar with and this is what I came up with.
Code:

#include <stdio.h>
#include <stdlib.h>

struct Item {
   int val;
   struct Item *next;
   struct Item *last;
}Item;

static struct Item *head =NULL;
static struct Item *tail =NULL;

static void push(int value) {
   struct Item *tmp;
   tmp = malloc(sizeof(Item));
   tmp->val = value;
   if (head==NULL) {
      tail = head = tmp;
      tmp->next=NULL;
      tmp->last=NULL;
   }
   tail->next = tmp;
   tmp->last = tail;
   tail = tmp;
}

static int pop() {
   int ret;
   if (head == NULL) {
      return 0;
   }
   struct Item *tmp;
   ret = tail->val;
   tmp = tail;
   if (tail->last != tail){
      tail = tail->last;
   }else {
      tail = head =NULL;
   }
   free(tmp);
      
   return ret;
}

static void printList() {
   struct Item *tmp =head;
   printf("Stack = [");
   while (tmp != tail) {
      printf(" %i,", tmp->val);
      tmp = tmp->next;
   }
   printf(" %i]\n",tmp->val);
}

static int peekHead() {
   if (head != NULL) {
      return head->val;
   }
   return 0;
}

static int peekTail() {
   if (tail != NULL) {
      return tail->val;
   }
   return 0;
}

int main(void) {
   int i,j,k;
   
   scanf("%i", &i);
   printf("Will Push %i values.\n",i);
   for (j=0;j<i;j++) {
      scanf("%i", &k);
      push(k);
      printf("Pushed %i\n",tail->val);
   }
   printf("head value = %i\n",peekHead());
   printList();
   printf("tail value = %i\n",peekTail());
   for (j=0;j<(i+1);j++) {
      k=pop(k);
      printf("Popped %i\n",k);
   }   
   return 0;
}

This is entirely my own code I came up with after trying to help my friend started with his java. Other than checking if malloc is actually returning a pointer and not null, is there anything else you would suggest or a better way to handle things?

I also have a similar program for a doubly linked list implementing a queue but the code is so similar its not really worth posting. So again, thoughts suggestions, things I missed?
The implementation looks quite good to me, and indeed very close to what I would probably have come up with had I been given the same assignment. My biggest gripes are just style issues, which says a lot for your code. Capitalizing a struct type is a very C++ thing to do, and not usually done in C, but it makes sense why you did it. The other thing is that "next/last" is ambiguous; it would be better to use the accepted "next/prev" terminology.
Thanks Kerm, that's what I was hoping to hear. As to the style issues, what sort of styling do you mean? My guess would be my handling { and } with regards to functions but I'm sure there are some other things.

I do agree the the next/last thing could be better and I've already done a find replace on my source to that extent.
No, I like the way you do braces, I was referring to the specific two issues that I mentioned. Now that I'm reviewing the code again, you have some inconsistencies about whitespace around equal signs and at the end of your struct definition, but those are miniscule nit-picks.
TheStorm wrote:
Thanks Kerm, that's what I was hoping to hear. As to the style issues, what sort of styling do you mean?


Your spacing is nonsensical, inconsistent, and retarded.

Proper:

Code:

int a, b, c;
if (a == b)
    a = NULL;
typedef struct item {
} item;
for (int i = 0; i < 100; i++) {
    printf("i = %d\n", i);
}


Oh, and you are missing a typedef for your struct (see my snippet above) OR you should get rid of the second "Item" in your struct declaration.
Kllrnohj wrote:

Your spacing is nonsensical, inconsistent, and retarded.

There's the whimsical fair of helpfulness we all know and love Wink
Of course Jonimus gets the intense style flaming from Kllrnohj. Razz I didn't think he had a huge amount of inconsistent and nonsensical formatting.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement