#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node *next;
};
struct node *head=NULL;
void push(int d)
{
struct node *tmp = (struct node *)malloc(sizeof(struct node*));
struct node *ptr = head;
if( head == NULL)
{
tmp->data = d;
tmp->next = NULL;
head = tmp;
} else {
tmp->data = d;
tmp->next = NULL;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = tmp;
}
}
void display()
{
struct node *ptr = head;
while(ptr)
{
printf("%d",ptr->data);
ptr = ptr->next;
}
}
void delete(int n)
{
struct node *ptr=head;
struct node *pre;
int i=1;
if(head != NULL && n==1)
{
head = head->next;
free(ptr);
return;
}
while(ptr != NULL)
{
if(i == n)
{
pre->next = ptr->next;
free(ptr);
ptr=pre->next;
break;
}
pre= ptr;
ptr= ptr->next;
i++;
}
}
void reverse()
{
struct node *ptr=head;
struct node *tmp=NULL;
struct node *next=NULL;
while(ptr)
{
next = ptr->next;
ptr->next = tmp;
tmp=ptr;
ptr=next;
}
head = tmp;
}
main()
{
int d=0,i=0;
while(i<5)
{
i++;
scanf("%d",&d);
push(d);
}
display();
scanf("%d",&d);
delete(d);
display();
printf("reverse ");
reverse();
display();
}
No comments:
Post a Comment