Saturday, 15 September 2012

simplified Singly Linked List Program in C


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


struct node
{
int data;//to hold information
struct node * next;//to hold address of sequentially next node
};

typedef struct node node;

node * create()//create a linked list
{
node *h, *p, *n;
char ch;

h = NULL;//no address

do
{
//allocate memory for a node

n = (node*) malloc(sizeof(node));

//initialize the node
printf("\n enter data for node ");
scanf("%d", &n->data);
n->next = NULL;

//connect
if(h == NULL)
{//true for 1st execution
h = n;
p = n;
}
else
{//true for rest of the executions
p->next = n;
p = n;
}

printf("\n continue ? ");
fflush(stdin);
scanf("%c", &ch);

}while(ch == 'y');

return h;
}

void disp(node *h)
{
node *p;

printf("\n");

p = h;
while(p != NULL)
{
printf("  %d", p->data);
p = p->next;
}//while
}

int count(node *h)
{
int c = 0;
node *p;
p = h;

while(p != NULL)
{
c++;
p = p->next;
}

return c;
}

//one imp feature of a linked list
//is dynamic expansion and shrink

node * addNodeAtHead(node *h)
{
node *n;

//1 create a node
n = (node*) malloc(sizeof(node));

//init the node
printf("\n enter data for node ");
scanf("%d", &n->data);
//n->next to initialize while connection

//2 connect node as head
n->next = h;

//3 mark n as new head node
h = n;

//update head in main
return h;

}

node * delNodeAtHead(node *h)
{
node *p;
if(h != NULL)
{
//catch the existing head node
p = h;
//set next node as head
h = h->next;
//deallocate the old head node
free(p);
}
else
{
printf("\n List not created");
}
return h;
}

void addNodeInBetween(node *h)
{
int tot, pos;
int i;
node *n;
node *p, *q;

tot = count(h);//tot number of nodes in list

printf("\n enter pos of new node ");
scanf("%d", &pos);

if(pos <=1 || pos >tot)
{
printf("\n Invalid intermediate pos");
}
else
{
//we have a valid pos node
//so we can add a node in between

//1 create a node
n = (node*) malloc(sizeof(node));

//init the node
printf("\n enter data for node ");
scanf("%d", &n->data);
n->next = NULL;

//2 connect at pos
p = h;

for(i = 1; i< pos; i++)
{
q = p ;
p = p->next;
}

//now p is at node : pos
//and q is at node : pos -1

q->next = n;
n->next = p;


}//else
}

void delNodeInBetween(node *h)
{
int tot, pos;
int i;
node *p, *q, *r;

tot = count(h);//tot number of nodes in list

printf("\n enter pos of new node ");
scanf("%d", &pos);

if(pos <=1 || pos >=tot)
{
printf("\n Invalid intermediate pos");
}
else
{
//we have a valid pos node
//so we can del a node

//catch the node

p = h;
for(i = 1; i< pos; i++)
{
q = p ;
p = p->next;
}

//now p is at node : pos
//and q is at node : pos -1

r = p->next;

//deallocate the node
free(p);
q->next = r;

}//else
}

void addNodeAtTail(node *h)
{
node *n, *p;

if(h != NULL)
{
//1. create a node
//memory allocation
n = (node*) malloc( sizeof(node));
//init the node

printf("\n enter data for node ");
scanf("%d", &n->data);
n->next = NULL;

//2. connect as tail
p = h;
while(p->next != NULL)
{
p = p->next;
}

//now p is at the last node
p->next = n;

}
else
{
printf("\n List Not Created");
}
}


node* delNodeAtTail(node *h)
{
node *p,*q;

if(h != NULL)
{
if(h->next == NULL)
{//single node in list
p = h;
h = NULL;//set h as NULL
free(p);//deallocate p
}
else
{//multiple nodes in list

//1. catch the last node

p = h;
while(p->next != NULL)
{
q = p;
p = p->next;
}

//now p is at last node
//and q is at last -1 node

//2. set new tail node
q->next = NULL;//set q as tail node
free(p);//deallocate node p

}//else

}
else
{
printf("\n list not created ");
}
return h;
}

void dispReverse(node *h)
{
int i, j,tot;
node *p;

tot = count(h); //count no of nodes in list

printf("\n");

for(i = 1; i<=tot; i++)
{//for every node

p = h; //p is at head/first node
//jumps
for(j = 1; j<= tot-i; j++)
{
p = p->next;
}

//disp
printf(" %d", p->data);
}//for(i

}

node* search(node *h, int val)
{
node *p;
p = h;

while(p != NULL)
{
if(p->data == val)
{//found
return p; //address of node where found
}

p = p->next;

}//while

return NULL;//not found
}

reverseList(node *h)
{
node *a, *b, *c;

a = h;
c = NULL;


while(a != NULL)
{
b = a;
a = a->next;
b->next = c;
c = b;
}
return c; //new head node
}

void main()
{
node *head, *p;
int x;
int ch;
head = NULL;

clrscr();

do
{
printf("\n 1. create list ");
printf("\n 2. disp list ");
printf("\n 3. disp list in reverse ");
printf("\n 4. count nodes in list ");
printf("\n 5. search a val in list ");
printf("\n 6. add node at head ");
printf("\n 7. add node in between ");
printf("\n 8. add node at tail ");
printf("\n 9. del node at head ");
printf("\n 10. del node in between ");
printf("\n 11. del node at tail ");
printf("\n 12. reverse linked list");
printf("\n 13. exit ");
printf("\n enter choice ");
scanf("%d", &ch);


switch(ch)
{
case 1:
head = create();
break;
case 2:
disp(head);
break;
case 3:
dispReverse(head);
break;
case 4:
x = count(head);
printf("\n No of nodes : %d ",x);
break;
case 5:
printf("\n enter data to search ");
scanf("%d", &x);
p = search(head, x);
if(p != NULL)
{
printf("\n %d found", x);
}
else
{
printf("\n %d not found",x);
}
break;

case 6:
head = addNodeAtHead(head);
break;
case 7:
addNodeInBetween(head);
break;
case 8:
addNodeAtTail(head);
break;
case 9:
head = delNodeAtHead(head);
break;
case 10:
delNodeInBetween(head);
break;
case 11:
head = delNodeAtTail(head);
break;
case 12:
head = reverseList(head);

}//switch
}while(ch != 13);
}//main

No comments:

Post a Comment