-
#include <stdio.h>
-
#include <stdlib.h>
-
-
typedef struct node
-
{
-
int key;
-
struct node *next;
-
}list;
-
-
void print(list *root)
-
{
-
list *i;
-
for(i=root; i!=NULL; i=i->next)
-
printf(" %d",i->key);
-
printf("\n");
-
}
-
//create a singly linked list
-
list *addFirst(list *root, int val)
-
{
-
list *p;
-
p=(list*) malloc(sizeof(list));
-
p->key=val;
-
p->next=root;
-
root=p;
-
return root;
-
}
-
list *addLast(list *root, int val)
-
{
-
list *p;
-
p=(list*)malloc(sizeof(list));
-
p->next=NULL;
-
p->key=val;
-
if(root==NULL) root=p;
-
else {
-
list *i;
-
for(i=root; i->next!=NULL;i=i->next);
-
i->next=p;
-
}
-
return root;
-
}
-
list *create(list *root)
-
{
-
root=NULL;
-
int val;
-
while(1==scanf("%d",&val))
-
{
-
if(val>0) root=addFirst(root,val);
-
else root=addLast(root,val);
-
print(root);
-
}
-
addLast(root,-1);
-
return root;
-
}
-
-
list *findKey(list *root, int key)
-
{
-
list *i;
-
for(i=root; i!=NULL; i=i->next)
-
if(i->key==key) return i;
-
return NULL;
-
}
-
int deleteList(list* root)
-
{
-
list *p;
-
while(root!=NULL)
-
{
-
p=root;
-
root=root->next;
-
free(p);
-
}
-
}
-
-
int main()
-
{
-
list *root;
-
root=create(root);
-
print(root);
-
if(findKey(root,0)!=NULL) printf("key 0 has been found");
-
deleteList(root);
-
system("pause");
-
return 0;
-
}
Operations on lists
Posted by marian
on June 29, 2009
undefined reference to `__gxx_personality_v0′
Posted by marian
on June 29, 2009
This happens when you try to compile and link a *.cpp program with gcc.
-
gcc -o hello.o hello.cpp
The solution is to change the extention (if you can) or compile it with g++
-
gcc -o hello.o hello.c