//Linked list basic functions
#include <stdio.h>
#define NHASH 100
struct node
{
int data;
struct node *next;
};
struct treenode
{
int data;
struct treenode* left;
struct treenode* right;
};
struct node* bin[NHASH];
struct node* readlistfromarray(int array[], int n);
struct node * readlistfromfile(FILE *fp);
struct node* readlistfromstdin();
struct node* readfrombst(struct treenode*);
struct node** makehashtable(struct node* head);
void print(struct node *head);
// then sth to read the list from input or from a file?
struct node * insertattail(struct node *head,int data);
struct node * insertathead(struct node *head,int data);
struct node * insertnexttohead(struct node *head,int data);
struct node * deltail(struct node *head);
struct node * delhead(struct node *head);
struct node* reverse(struct node *head);
struct node* sort(struct node *head);
struct node* ispallindrome(struct node *head);
struct node* mergesort(struct node* first,struct node *second);
struct node* insertattail(struct node *head,int data)
{
//First construct the node in newp
struct node *newp;
struct node *p;
newp
=malloc(sizeof(struct node
));newp->data=data;
newp->next=NULL;
//Standard name for node pointers only used for traversal? p? q? tmp? if more than 1 kind of nodes?
// Then what about temp pointers for swapping,adjusting etc but not exactly for traversal?
// Make your own naming conventions for commonly needed kind of variables.
if(head == NULL) // is there more elegant way of dealing with this? any idiom?
{
head=newp;
return head;
}
for(p=head;p->next!=NULL;p=p->next)
;
p->next=newp;
return head;
}
struct node* insertathead(struct node *head,int data)
{
//First construct a new node;
struct node *new;
new
=malloc(sizeof(struct node
));new->data=data;
new->next=NULL;
new->next=head;
return new;
}
struct node* insertnexttohead(struct node *head,int data)
{
//if head exists insert next to it else put it in head.
//First construct a new node;
struct node *tmp; //should be onetimetmp? singleadjust? adjustonce? adjust? what does Brian name it?
struct node *new;
new->data=data;
new->next=NULL;
if(head == NULL)
{
new->next=head;
return head;
}
tmp=head->next;
head->next=new;
new->next=tmp;
return head;
}
struct node * readlistfromarray(int array[], int n)
{
// Make use of already written insert functions.
int i;
struct node *head=NULL;
for(i=0;i<n;i++)
{
head=insertattail(head,array[i]);
//head=insertathead(head,array[i]);
}
return head;
}
void print(struct node *head)
{
struct node *p;
for(p=head;p!=NULL;p=p->next)
}
main()
{
struct node *head;
int array[5]={3,1,2,4,2};
head=readlistfromarray(array,5);
print(head);
}