Skip to main content

Example of Reversed Link List Operation in C


#include<stdio.h>
#include<malloc.h>
typedef struct list
{
int data;
struct list *next;
}node;


node *START=NULL;


insert()
{
int i;
    node *tmp,*q;
    tmp=(node*)malloc(sizeof(node));
printf("\nEnter Element :: ");
scanf("%d",&i);
    tmp->next=NULL;
    tmp->data=i;
    if(START==NULL)
    {
        START=tmp;
    }else
        {
            q=START;
            while(q->next!=NULL)
            {
                q=q->next;
            }
            q->next=tmp;
        }
}


display()
{
node *q;
if(START==NULL)
{
printf("\nList Is Empty");
}else{
q=START;
while(q!=NULL)
{
printf("%d -> ",q->data);
q=q->next;
}
}



reverse()
{
node *p1,*p2,*p3;
int d;
if(START==NULL)
{
printf("\nThis Opetation Is Not Possible Due to Lack of Elements");
}
else
{
p1=START;
p2=p1->next;
p3=p2->next;

p1->next=NULL;
p2->next=p1;

while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->next;
p2->next=p1;
}

START=p2;
}
}




int main()
{
    int ch,f=0;
    //clrscr();//Optional
    while(f<=1)
    {
        printf("\n1. INSERT VALUE\n2. DISPLAY \n3. REVERSE");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
                insert();
                //getch(); //Optional
                break;
            case 2:
                display();
                //getch();//Optional
                break;
            case 3:
            reverse();
            printf("\nOperation Done...");
            break;
            default:
                printf("\nWrong Input");
        }
    }
}

Comments

Popular posts from this blog

Sample : String Reverse In Java

import java.io.*; class test {     public static void main(String args[])     {         DataInputStream in=new DataInputStream(System.in);         try         {             String text;             System.out.println("\nEnter The Text");             text=in.readLine();             System.out.println("\nIn Reverse Order::\n");             for(int i=text.length()-1;i>=0;i--)             {                 System.out.print(text.charAt(i));             }          ...

Getting Browser Information in javaScript

<html> <head> <script> function getBrowserName() { tag_link.innerHTML="You are surfing internet through  "+'<b>'+navigator.appName+'</b>'+" browser family." } </script> </head> <body onLoad="getBrowserName()"> <center> <p id="tag_link" style="font-size:30"></p> </center> </body> </html>