Anda di halaman 1dari 27

/* PROGRAM IMPLEMENTATION OF SINGLE LINKED LIST */

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
//#define NULL 0

/* STRUCTURE CONTANING A DATA PART AND A LINK PART */

struct node
{
int data;
struct node *next;
}*p;

/* P IS A GLOBAL POINTER CONTAINS THE ADRESS OF THE


FIRST NODE IN LIST*/

/*THIS FUNCTION DELETES A NODE */

delnode(int num)
{
struct node *A, *m;
temp=p;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==p)
{
p=temp->next;
free(temp);
return;
}
else
{
m->next=temp->next;
free(temp);
return;
}
}else
{
m=temp;
temp= temp->next;
}
}
printf(" ELEMENT %d NOT FOUND ", num);
}

/*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED LIST */

append( int num )


{
struct node *temp,*r;

/* CREATING A NODE AND ASSIGNING A VALUE TO IT */

temp= (struct node *)malloc(sizeof(struct node));


temp->data=num;
r=(struct node *)p;

if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE */


{
p=temp;
p->next =NULL;
}
else
{
/* GO TO LAST AND ADD*/

while( r->next != NULL)


r=r->next;
r->next =temp;
r=temp;
r->next=NULL;
}
}

/* ADD A NEW NODE AT BEGINNING */

addbeg( int num )


{

/* CREATING A NODE AND INSERTING VALUE TO IT */

struct node *temp;


temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;

/* IF LIST IS NULL ADD AT BEGINNING */


if ( p== NULL)
{
p=temp;
p->next=NULL;
}

else
{
temp->next=p;
p=temp;
}
}

/* ADD A NEW NODE AFTER A SPECIFIED NO OF NODES */

addafter(int num, int loc)


{
int i;
struct node *temp,*t,*r;
r=p; /* here r stores the first location */
if(loc > count()+1 || loc <= 0)
{
printf(" insertion is not possible :");
return;
}
if (loc == 1)/* if list is null then add at beginning */
{
addbeg(num);
return;
}
else
{
for(i=1;i<loc;i++)
{
t=r; /* t will be holding previous value */
r=r->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
t->next=temp;
t=temp;
t->next=r;
return;
}
}
/* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED LIST */
display(struct node *r)
{
r=p;
if(r==NULL)
{
printf("NO ELEMENT IN THE LIST :");
return;
}
/* traverse the entire linked list */
while(r!=NULL)
{
printf(" -> %d ",r->data);
r=r->next;
}
printf(" ");
}

//THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE LIST


count()
{
struct node *n;
int c=0;
n=p;
while(n!=NULL)
{
n=n->next;
c++;
}
return(c);
}

//THIS FUNCTION REVERSES A LINKED LIST


reverse(struct node *q)
{
struct node *m, *n,*l,*s;
m=q;
n=NULL;
while(m!=NULL)
{
s=n;
n=m;
m=m->next;
n->next=s;
}
p=n;
}

/* THIS IS THE MAIN PROGRAM */

void main()
{
int i;
p=NULL;
while(1) /* this is an indefinite loop */
{
printf(" 1.INSERT A NUMBER AT BEGINNING;");
printf(" 2.INSERT A NUMBER AT LAST:");
printf(" 3.INSERT A NUMBER AT A PARTICULAR LOCATION INlIST:");
printf(" 4.PRINT THE ELEMENTS IN THE LIST :");
printf(" 5.PRINT THE NUMBER OF ELEMENTS IN THE LIST ");
printf(" 6.DELETE A NODE IN THE LINKED LIST:");
printf(" 7.REVERSE A LINKED LIST :");
printf(" 8.GET OUT OF LINKED LIST (BYEE BYEE):");
printf(" PLEASE, ENTER THE NUMBER:");
scanf("%d",&i); /* ENTER A VALUE FOR SWITCH */

switch(i)
{
case 1:
{
int num;
printf(" PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
addbeg(num);
break;
}
case 2:
{
int num;
printf(" PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
append(num);
break;
}

case 3:
{
int num, loc,k;
printf(" PLEASE ENTER THE NUMBER :-");
scanf("%d",&num);
printf("PLEASE ENTER THE LOCATION NUMBER :-");
scanf("%d",&loc);
addafter(num,loc);
break;
} case 4:
{
struct node *n;
printf("THE ELEMENTS IN THE LIST ARE : ");
display(n);
break;
}

case 5:
{
struct node *n;
display(n);
printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE %d",count());
break;
} case 6:
{
int num;
printf(" PLEASE ENTER A NUMBER FROM THE LIST :");
scanf("%d",&num);
delnode(num);
break;
}
case 7:
{
reverse(p);
display(p);
break;
}
case 8:
{
exit();
}
}/* end if switch */
}/* end of while */
}/* end of main */
OUTPUT

MENU

1.INSERT A NUMBER AT BEGINNING


2.INSERT A NUMBER AT LAST
3.INSERT A NUMBER AT A PARTICULAR LOCATION IN lIST
4.PRINT THE ELEMENTS IN THE LIST
5.PRINT THE NUMBER OF ELEMENTS IN THE LIST
6.DELETE A NODE IN THE LINKED LIST
7.REVERSE A LINKED LIST
8.GET OUT OF LINKED LIST

/*PLEASE, ENTER THE NUMBER ENTER A VALUE FOR SWITCH */

1. PLEASE ENTER THE NUMBER:- 25


2. PLEASE ENTER THE NUMBER:- 50
3. PLEASE ENTER THE LOCATION NUMBER:-1
PLEASE ENTER THE NUMBER:-45
4. THE ELEMENTS IN THE LIST ARE:25 45 50
5. TOTAL NO OF ELEMENTS IN THE LSIT ARE:-3
6. PLEASE ENTER A NUMBER FROM THE LIST:45
7. REVERSED LINKED LIST :50 25
STACK AS A LINKED LIST

#include<stdio.h>
#include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
} ;
struct node *top;

void main()
{
void push(int);
void display();
int wish, num,will,a;
wish = 1;
top = NULL;
clrscr();
printf("Program for Stack as Linked List demo..");
while(wish == 1)
{
printf("Main Menu 1.Enter data in stack 2.Delete from stack");
scanf("%d",&will);
switch(will)
{
case 1:
printf("Enter the data");
scanf("%d",&num);
push(num);
display();
break;
case 2:
a=pop();
printf("Value returned from top of the stack is %d",a);
break;
default:
printf("Invalid choice");
}
printf("Do you want to continue, press 1");
scanf("%d",&wish);
}
}
void push(int y)
{
struct node *x;
x=malloc(sizeof(struct node));
printf(" Address of newly created node x is %d",x);
x->data = y;
x->link = top;
top = x;
}
void display()
{
int i =0;
struct node * temp;
temp = top;

while(temp!=NULL)
{
printf("Item No. %d : Data %d Link %d ",i++,temp->data,temp->link);
temp=temp->link;
}
}

/// THIS FUNCTION REMOVES TOP NODE FROM THE STACK AND
RETURNS ITS VALUE///

int pop()
{
int a;
if(top==NULL)
{
printf("STACK EMPTY...");
return 0;
}
else
{
a=top->data;
printf("The value returned is %d ",a);
free(top);
top=top->link;
return (a);

}
}
OUTPUT

Program for Stack as Linked List demo..


Main Menu
1.Enter data in stack
2.Delete from stack

1.Enter the data


25 10 15
2.Value returned from top of the stack is
25
Do you want to continue, press 1
N
The value returned is: 10 15
QUEUE AS A LINKED LIST

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct A
{
int data;
struct node *link;
} ;
struct node *front, *rear;
void main()
{

int wish,will,a,num;
void add(int);
wish=1;
clrscr();
front=rear=NULL;
printf("Program for Queue as Linked List demo..");
while(wish == 1)
{
printf("Main Menu 1.Enter data in queue 2.Delete from queue");
scanf("%d",&will);
switch(will)
{
case 1:
printf("Enter the data");
scanf("%d",&num);
add(num);
//display();
break;
case 2:
a=del();
printf("Value returned from front of the queue is %d",a);
break;
default:
printf("Invalid choice");
}
printf("Do you want to continue, press 1");
scanf("%d",&wish);
}
getch();
}

void add(int y)
{
struct node *ptr;
ptr=malloc(sizeof(struct node));
ptr->data=y;
ptr->link=NULL;
if(front ==NULL)
{
front = rear= ptr;
}
else
{
rear->link=ptr;
rear=ptr;
}
}

int del()
{
int num;
if(front==NULL)
{
printf("QUEUE EMPTY");
return(0);
}
else
{
num=front->data;
front = front->link;
printf(" Value returned by delete function is %d ",num);
return(num);
}
}
OUTPUT

Program for Queue as Linked List demo


Main Menu
1.Enter data in queue
2.Delete from queue
1.
Enter the data
30 45 60 70
2.
Value returned from front of the queue is:
30 45 60 70
Do you want to continue, press 1: N
Value returned by delete function is:
45 60 70
PROGRAM TO ADD TWO POLYNOMIALS

#include<stdio.h>
#include<conio.h>
struct barbie
{
int coff;
int pow;
struct barbie *link;
}*ptr,*start1,*A,*start2,*start3,*ptr1,*ptr2;
typedef struct barbie bar;
int temp1,temp2;

void main()
{

void create(void);
void prnt(void);
void suml(void);
void sort(void);
clrscr();

printf("Enter the elements of the first poly :");


node = (bar *) malloc(sizeof (bar));
start1=node;
if (start1==NULL)
{
printf(" Unable to create memory.");
getch();
exit();
}
create();
printf("Enter the elements of the second poly :");
node = (bar *) malloc(sizeof (bar));
start2=node;
if (start2==NULL)
{
printf(" Unable to create memory.");
getch();
exit();
}
create();
clrscr();
//printing the elements of the lists
printf("The elements of the poly first are :");
ptr=start1;
prnt();

printf("The elements of the poly second are :");


ptr=start2;
prnt();

printf("The first sorted list is :");


ptr=start1;
sort();
ptr=start1;
prnt();

printf("The second sorted list is :");


ptr=start2;
sort();
ptr=start2;
prnt();

printf("The sum of the two lists are :");


suml();
ptr=start3;
prnt();

getch();

/*-----------------------------------------------------------------------------*/
void create()
{
char ch;
while(1)
{
printf(" Enter the coff and pow :");
scanf("%d%d",&node->coff,&node->pow);
if (node->pow==0 )
{
ptr=node;
node=(bar *)malloc(sizeof(bar));
node=NULL;
ptr->link=node;
break;
}

printf("Do u want enter more coff ?(y/n)");


fflush(stdin);
scanf("%c",&ch);
if (ch=='n' )
{
ptr=node;
node=(bar *)malloc(sizeof(bar));
node=NULL;
ptr->link=node;
break;
}
ptr=node;
node=(bar *)malloc(sizeof(bar));
ptr->link=node;
}
}
/*-------------------------------------------------------------------------*/
void prnt()
{ int i=1;

while(ptr!=NULL )
{
if(i!=1)
printf("+ ");
printf(" %dx %d ",ptr->coff,ptr->pow);
ptr=ptr->link;
i++;
}
//printf(" %d %d",ptr->coff,ptr->pow);
}
/*---------------------------------------------------------------------------*/
void sort()
{
for(;ptr->coff!=NULL;ptr=ptr->link)
for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link)
{
if(ptr->pow>ptr2->pow)
{
temp1=ptr->coff;
temp2=ptr->pow;
ptr->coff=ptr2->coff;
ptr->pow=ptr2->pow;
ptr2->coff=temp1;
ptr2->pow=temp2;

}
}
}
/*---------------------------------------------------------------------------*/
void suml()
{
node=(bar *)malloc (sizeof(bar));
start3=node;

ptr1=start1;
ptr2=start2;

while(ptr1!=NULL && ptr2!=NULL)


{
ptr=node;
if (ptr1->pow > ptr2->pow )
{
node->coff=ptr2->coff;
node->pow=ptr2->pow;
ptr2=ptr2->link; //update ptr list B
}
else if ( ptr1->pow < ptr2->pow )
{
node->coff=ptr1->coff;
node->pow=ptr1->pow;
ptr1=ptr1->link; //update ptr list A
}
else
{
node->coff=ptr2->coff+ptr1->coff;
node->pow=ptr2->pow;
ptr1=ptr1->link; //update ptr list A
ptr2=ptr2->link; //update ptr list B

node=(bar *)malloc (sizeof(bar));


ptr->link=node; //update ptr list C
}//end of while

if (ptr1==NULL) //end of list A


{
while(ptr2!=NULL)
{
node->coff=ptr2->coff;
node->pow=ptr2->pow;
ptr2=ptr2->link; //update ptr list B
ptr=node;
node=(bar *)malloc (sizeof(bar));
ptr->link=node; //update ptr list C
}
}

else if (ptr2==NULL) //end of list B


{
while(ptr1!=NULL)
{
node->coff=ptr1->coff;
node->pow=ptr1->pow;
ptr1=ptr1->link; //update ptr list B
ptr=node;
node=(bar *)malloc (sizeof(bar));
ptr->link=node; //update ptr list C
}
}
node=NULL;
ptr->link=node;
}

OUTPUT

Enter the elements of the first poly: 1.4x5:1.5x4:1.7x2:1.8x1:1.9x0


Unable to create memory.
Enter the elements of the second poly 1.5x6:2.5x5:-3.5x4:4.5x3:6.5x1
Unable to create memory.
The elements of the poly first are: 1.4x5:1.5x4:1.7x2:1.8x1:1.9x0
The elements of the poly first are: 1.5x6:2.5x5:-3.5x4:4.5x3:6.5x1
The sum of sorted list are: 3.9x5 : -2.0x4:4.5x3:1.7x2:8.3x1:1.9x0
DOUBLY LINKED LIST

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define N 100

struct dlinklist
{
    struct dlinklist *prev;  /** Stores address of previous node **/
    int roll_no;             /** stores roll number **/
    char name[N];            /** stores Name **/
    float marks;             /** stores Marks **/
    struct dlinklist *next;  /** stores address of next node **/
};

/** Redefining dlinklist as node **/


typedef struct dlin klist node;

void init(node*);     /** Input function **/


void ins_aft(node*);  /** Function inserting before **/
node* ins_bef(node*); /** Function inserting after **/
node* del(node*);     /** Function deleting a node **/
void search(node*);   /** Function for searching node **/
void disp(node*);     /** Function for displaying node **/
void rollsrch(node*); /** Function for searching node by roll number **/
void namesrch(node*); /** Function for searching node by name **/
void marksrch(node*); /** Function for searching node by marks **/

void main()
{
    node *head;
    char ch;                         /* Choice inputing varible */
    int opt;                         /* Option inputing variable*/
    static int flag=0;               /* Unchanged after iniialization */
    clrscr();
    head=(node*)malloc(sizeof(node));
    head->next=NULL;
    head->prev=NULL;
    do
  {
again:
    printf("\nEnter your option\n");
    printf("\n1. Initialize the node\n");
    printf("\n2. Insert before a specified node\n");
    printf("\n3. Insert after a specified node\n");
    printf("\n4. Delete a particular node\n");
    printf("\n5. Search the nodes\n");
    printf("\n6. Display all the nodes\n");
    scanf("%d",&opt);   

if(flag==0 && opt!=1)


  {
        printf("\nNo. You must first initialize at least one node\n");
        goto again;
  }
    if(flag==1 && opt==1)
  {
        printf("\nInitialisation can occur only once.\n");
        printf("\nNow you can insert a node\n");
        goto again;
  }
    if(opt==4 && head->next==NULL)
  {
        printf("\nYou cannot delete the one and only the single node\n");
        goto again;
  }
    if(flag==0 && opt==1)
        flag=1;
    switch(opt)
  {
    case 1:
        init(head);
        break;
    case 2:
        head=ins_bef(head);
        break;
    case 3:
        ins_aft(head);
        break;
    case 4:
        head=del(head);
        break;
    case 5:
        search(head);
        break;
    case 6:
        disp(head);
        break;
  }
    printf("\nDo you wish to continue[y/n]\n");
    ch=getche();
    }while(ch=='Y' || ch=='y');
    printf("\nDone by \"SATENDRA"\n");
    printf("\nPress any key to exit\n");
    getch();
}

void init(node *current)


{
    current->prev=NULL;
    printf("\nEnter Roll number\n");
    scanf("%d",&current->roll_no);
    printf("\nEnter the name\n");
    fflush(stdin);
    gets(current->name);
printf("\nEnter the marks\n");
    scanf("%f",&current->marks);
    current->next=NULL;
}

void ins_aft(node *current)


{
    int rno;             /* Roll number for inserting a node*/
    int flag=0;
    node *newnode;
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the roll number after which you want to insert a node\n");
    scanf("%d",&rno);
    init(newnode);
    while(current->next!=NULL)
  {
        /***  Insertion checking for all nodes except last  ***/
        if(current->roll_no==rno)
    {
            newnode->next=current->next;
            current->next->prev=newnode;
            current->next=newnode;
            newnode->prev=current;
            flag=1;
    }
        current=current->next;
  }
    if(flag==0 && current->next==NULL && current->roll_no==rno)
  {
        /***  Insertion checking for last nodes  ***/
        newnode->next=current->next;
        current->next=newnode;
        flag=1;
  }
    else if(flag==0 && current->next==NULL)
        printf("\nNo match found\n");
}

node* ins_bef(node *current)


{
    int rno;            /* Roll number for inserting a node*/
    node *newnode,*temp;
    newnode=(node*)malloc(sizeof(node));
    printf("\nEnter the roll number before which you want to insert a node\n");
    scanf("%d",&rno);
    init(newnode);
    if(current->roll_no==rno)
  {
        /*** Insertion checking for first node ***/
        newnode->next=current;
        current->prev=newnode;
        current=newnode;
        return(current);
  }

temp=current;
    while(temp->next!=NULL)
    {                                       
        /*** Insertion checking for all node except first ***/
        if(temp->next->roll_no==rno)
    {
            newnode->next=temp->next;
            temp->next->prev=newnode;
            temp->next=newnode;
            newnode->prev=temp;
            return(current);
    }
        temp=temp->next;
  }
    /*
    If the function does not return from any return statement.
    There is no match to insert before the input  roll number.
    */
    printf("\nMatch not found\n");
    return(current);
}
node* del(node *current)
{
    int rno;               /* Roll number for deleting a node*/
    node *newnode,*temp;
    printf("\nEnter the roll number whose node you want to delete\n");
    scanf("%d",&rno);
    newnode=current;
    if(current->roll_no==rno)
  {
        /***  Checking condition for deletion of first node  ***/
        newnode=current; /*  Unnecessary step  */
        current=current->next;
        current->prev=NULL;
        free(newnode);
        return(current);
  }
    else
  {
        while(newnode->next->next!=NULL)
    {
            /***  Checking condition for deletion of   ***/
            /*** all nodes except first and last node  ***/
            if(current->next->roll_no==rno)
      {
                newnode=current;
                temp=current->next;
                newnode->next=newnode->next->next;
                newnode->next->prev=current;
                free(temp);
                return(current);
      }
            newnode=newnode->next;
    }
        if(newnode->next->next==NULL && newnode->next->roll_no==rno)

{
            /***  Checking condition for deletion of last node  ***/
            temp=newnode->next;
            free(temp);
            newnode->next=NULL;
            return(current);
    }
  }
    printf("\nMatch not found\n");
    return(current);
}

void search(node *current)


{
    int ch;                  /* Choice inputing variable */
    printf("\nEnter the criteria for search\n");
    printf("\n1. Roll number\n");
    printf("\n2. Name\n");
    printf("\n3. Marks\n");
    scanf("%d",&ch);
    switch(ch)
  {
    case 1:
        rollsrch(current);
        break;
    case 2:
        namesrch(current);
        break;
    case 3:
        marksrch(current);
        break;
    default:
        rollsrch(current);
  }
}

void rollsrch(node *current)


{
    int rno;
    printf("\nEnter the roll number to search\n");
    scanf("%d",&rno);
    while(current->next!=NULL)
  {
        if(current->roll_no==rno)
            printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
        current=current->next;
  }
    if(current->next==NULL && current->roll_no==rno)
        printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
}

void namesrch(node *current)


{
    char arr[20];
  printf("\nEnter the name to search\n");
    fflush(stdin);
    gets(arr);
    while(current->next!=NULL)
  {
        if(strcmp(current->name,arr)==NULL)
            printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
        current=current->next;
  }
    if(current->next==NULL && strcmp(current->name,arr)==NULL)
        printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
}

void marksrch(node *current)


{
    float marks;
    printf("\nEnter the marks to search\n");
    scanf("%f",&marks);
    while(current->next!=NULL)
  {
        if(current->marks==marks)
            printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
        current=current->next;
  }
    if(current->next==NULL && current->marks==marks)
        printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
}

void disp(node *current)


{
    while(current!=NULL)
  {
        printf("\n%d\t%s\t%f",current->roll_no,current->name,current->marks);
        current=current->next;
  }
}

OUTPUT

MENU
1. Initialize the node
2. Insert before a specified node
3. Insert after a specified node
4. Delete a particular node
5. Search the nodes
6. Display all the nodes
Now you can insert a node
Enter your option : 1
Enter Roll number
60,61,79,81.
Enter the name
ALISHA,VIBHA,SATENDRA,SUNNY
Enter the marks
92,93,93,95

Enter the roll number after which you want to insert a node 
61
Enter Roll number
60,61,68,79,81.
Enter the name
ALISHA,VIBHA,SACHIN,SATENDRA,SUNNY
Enter the marks
92,93,90,93,95
Enter the roll number before which you want to insert a node
68
Enter Roll number
60,61,64,68,79,81.
Enter the name
ALISHA,VIBHA,DEEPIKA,SACHIN,SATENDRA,SUNNY
Enter the marks
92,93,92,90,93,95

Enter the criteria for search


1. Roll number
2. Name
3. Marks

Enter the roll number to search


79
Enter the roll number whose node you want to delete
64
Enter the name to search
SACHIN
Enter the marks to search   
93

Do you wish to continue[y/n]


Press any key to exit

Anda mungkin juga menyukai