linked list
implementation in C
//insert at
beginning,delete at beginning
//display,
reverse
#include<stdio.h>
#include<malloc.h>
void insert(int d);
void display();
void deletee();
void reverse();
struct node
{
int data;
struct node *next;
};
struct node *start;
int main()
{
printf("\n\n");
start=NULL;//starting
with empty list
insert(10);
insert(10);
insert(15);
insert(10);
insert(11);
insert(15);
insert(99);
printf("\ndisplay()
called:\n");
display();
printf("\ndeletee()
called:\n");
deletee();
display();
printf("\nreverse()
called:\n");
reverse();
display();
printf("\n\n");
return 0;
}
void insert(int d)
{
struct node
*newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=start;
start=newnode;
}
void display()
{
struct node
*ptr=start;
while(ptr!=NULL)
{
printf("%d
",ptr->data);
ptr=ptr->next;
}
}
void deletee()
{
struct node
*temp=(struct node*)malloc(sizeof(struct node));
temp=start;
start=start->next;
if(temp!=NULL)
{
free(temp);
}
}
void reverse()
{
struct node
*next=(struct node*)malloc(sizeof(struct node));
struct node
*result=(struct node*)malloc(sizeof(struct node));
struct node
*current=(struct node*)malloc(sizeof(struct node));
next=NULL;
result=NULL;
current=start;
while(current!=NULL)
{
next=current->next;
current->next=result;
result=current;
current=next;
}
start=result;
}
/*
display() called:
99 15 11 10 15 10 10
deletee() called:
15 11 10 15 10 10
reverse() called:
10 10 15 10 11 15
*/
No comments:
Post a Comment