Advertisement

Beat This

Started by July 14, 2005 12:36 AM
20 comments, last by GameDev.net 19 years, 6 months ago
I want to start a small compo here something for fun since i am kind of bored. So since i am writting the post i will be generous and post my method first. So before i post anything let's me say what the compo is about. What i want to see is that in how much line can someone implement a linked list program that can Add, Delete the whole list,and Print. Ok here is mine without the Comment it is 48 Lines of Codes. Now let's see what other are capable of doing #include<iostream> using namespace std; struct node { int item; node* next; }; void AddToList(node*& l,int item){ if(l){ AddToList(l->next,item); return; } l=new node; l->item=item; l->next=NULL; } void FreeList(node*& l) { if(!l) return; if(l->next) FreeList(l->next); /*Deleted Backwards*/ delete l; l=NULL; } void PrintList(node*& l) { if(!l) return; else if(l){ cout<<l->item<<endl; PrintList(l->next); return; } } void main(){ node *a=0; AddToList(a,12); AddToList(a,32); AddToList(a,45); AddToList(a,56); AddToList(a,58); PrintList(a); FreeList(a); }
#include <list>#include <iostream>#include <algorithm>int main(){  std::list<int> blah;  blah.add(4);  blah.add(5);  std::copy(blah.begin(), blah.end(), std::ostream_iterator(cout, '\n'));  blah.clear();}


do I win? =)
Advertisement
Behold my clever trickery:

#include <list>#include <string>#include <iostream>template<typename T> class slist {public:  slist() : list_(std::list<T>()) {}  slist(const std::string& str) : list_(std::list<T>()) { add_from_str(str); }  ~slist() { delete_all(); }  void delete_all() {    list_.clear();  }  void add(T item) { list_.push_back(item); }  void add_from_str(const std::string& str) {    for (int c = 0; c < str.length(); ++c)      add(static_cast<T>(str[c]));   }  void print() {    for (std::list<T>::iterator it = list_.begin(); it != list_.end(); ++it)      std::cout << *it;  }private:  std::list<T> list_;};void main() {  slist<int> list = slist<int>("1234567890");  list.delete_all();}


27 lines!

EDIT: Danget! Someone stole my idea!
I forgot to mention that you cannot used any of the STL Library. you have to code everything yourself. Also you cannot used any base classes you already had i want to see the whole thing build from the ground up. look at my example and you will see what i mean. I want it to be like i can copy it here and paste it and compile it and it works.
Quote:
Original post by BornToCode
I forgot to mention that you cannot used any of the STL Library. you have to code everything yourself. Also you cannot used any base classes you already had i want to see the whole thing build from the ground up. look at my example and you will see what i mean


Yeah, I figured as much. I was just messing around. [smile]
Quote:
Original post by BornToCode
I forgot to mention that you cannot used any of the STL Library. you have to code everything yourself. Also you cannot used any base classes you already had i want to see the whole thing build from the ground up. look at my example and you will see what i mean. I want it to be like i can copy it here and paste it and compile it and it works.


Well what are you using cout for? That's part of the library!
Advertisement
Ok, here's my new entry. 2 lines. Beat this!
#include<iostream>using namespace std;struct node{int item;node* next;};void AddToList(node*& l,int item){if(l){AddToList(l->next,item);return;}l=new node;l->item=item;l->next=NULL;}void FreeList(node*& l){if(!l)return;if(l->next)FreeList(l->next); /*Deleted Backwards*/delete l;l=NULL;}void PrintList(node*& l){if(!l)return;else if(l){cout<<l->item<<endl;PrintList(l->next);return;}}void main(){node *a=0;AddToList(a,12);AddToList(a,32);AddToList(a,45);AddToList(a,56);AddToList(a,58);PrintList(a);FreeList(a);}
no guarantees on compiling (or working):

public class ListObject{	public Object item;	public Object next;		public Object(item)	{		this.item = item;	}	}public class List{	ListObject tail;	ListObject head;	public List()	{		head = tail = new ListObject(null);	}	public void add(Object obj)	{		tail = tail.next = new ListObject(obj);			}	public void removeAll()	{		head = tail = new ListObject(null); //Garbage Collector will take care of the rest		}	public void print()	{				for(ListObject li = head; != null; li = li.next;)		{			Console.printline(head.item.toString()); //I cant remember the actual console printline function		}	}	public static void testMe()	{		List list = new List();		list.add(new Integer(45));		list.print();		list.removeAll();	}}
BTW, what does this have to do with AI?
I dunno, I found it by going to recent threads.

This topic is closed to new replies.

Advertisement