/*
Name: Singly Linked List
Author:
Description: Includes insertion deletion modules
*/
#include<stdio.h>
#include<conio.h>
struct linklist
{
int data;
struct linklist *next;
};
typedef struct linklist node;
node *create_node()
{
node *p;
p=(node*)malloc(sizeof(node));
return p;
}
node *create_list_e(int val,node *head)//Adding contents at the end
{
node *t=create_node();
node *t_head=head;
if(t!=NULL)
{
t->data=val;
t->next=NULL;
if(t_head==NULL)
head=t;
else
{
while(t_head->next!=NULL)
t_head=t_head->next;
t_head->next=t;
}
}
else
printf("Sorry no more memory space available\n");
return head;
}
node *create_list_b(int val,node *head)//Adding contents at the beginning
{
node *t=create_node();
if(t!=NULL)
{
t->data=val;
t->next=head;
head=t;
}
else
printf("Sorry no more memory space available\n");
return head;
}
void print(node *head)/*Prints the contents of the linked list*/
{
node *t_head=head;
printf("\nContents\n");
while(t_head!=NULL)
{
printf("%d\n",t_head->data);
t_head=t_head->next;
}
}
node *del_end(node *head)/*Deleting from end*/
{
node *prev=NULL,*p=head;
if(head==NULL)
printf("Underflow\n");
else
{
while(p->next!=NULL)
{
prev=p;
p=p->next;
}
printf("%d\n",p->data);
if(p==head)
head=NULL;
else
prev->next=NULL;
free(p);
}
return head;
}
node *del_beg(node *head)/*Deleting from beginning*/
{
node *p;
if(head==NULL)
printf("Underflow\n");
else
{
p=head;
printf("%d\n",head->data);
head=head->next;
free(p);
}
return head;
}
int main()
{
node *head=NULL;
int i=0;
while(i<=10)
{
head=create_list_b(i,head);
i++;
}
head=create_list_e(i,head);
print(head);
printf("\n");
head=del_end(head);
head=del_end(head);
head=del_end(head);
head=del_beg(head);
head=del_beg(head);
printf("\n");
print(head);
getch();
}
0 comments:
Post a Comment