DISQUS

DISQUS Hello!  The comments on this profile are unclaimed and thus are unverified.

Do they belong to you? Claim these comments.

reverse a linked list's picture

Unregistered

Feeds

aliases

  • reverse a linked list

reverse a linked list

10 months ago

in Bad Apples (Cont’d) on iRant
#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct list
{
int data;
struct list *next;
};
typedef struct list node;

void init(node* record)
{
record->next=NULL;
}
void addnode(node* record,int d)
{
node* fresh;
fresh=(node *)malloc(sizeof(node));
fresh->data=d;
fresh->next=record->next;
record->next=fresh;

}
void print(node *record)
{
node* temp;
temp=(node *)malloc(sizeof(node));
for(temp=record->next;temp;temp=temp->next)
printf(" %d",temp->data);

}
void reverse(node* record)
{
node* temp;node* temp1;node* temp2;
temp=(node *)malloc(sizeof(node));
temp1=(node *)malloc(sizeof(node));
temp2=(node *)malloc(sizeof(node));
temp=record;temp1=temp->next;temp2=temp1->next;
temp->next->next=NULL;
while(temp2!=NULL)
{
temp=temp1;
temp1=temp2;
temp2=temp1->next;
temp1->next=temp;

}
record->next=temp1;

}
int main(void)
{
node* start;
node* start1;
start=(node *) malloc(sizeof(node));
init(start);
int i=0;
for(i=10;i>=0;i--)
addnode(start,i);
getch();
print(start);
getch();
reverse(start);
getch();
print(start);
getch();
return 0;
}
Returning? Login