0% found this document useful (0 votes)
13 views11 pages

Ds

Uploaded by

shivank kunwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Ds

Uploaded by

shivank kunwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

DATA STRUCTURE LAB

ASSIGNMENT 2

NAME : SHIVANK KUNWAR

SECTION : SE

ROLL NO: 60

ASSIGNMENT ON LINKED LISTS

//QUESTION 1

#include <stdio.h>

#include <malloc.h>

typedef struct node

int data;

struct node *next;

} snode;

snode *enqueue(snode *tail, int n)

snode *p;

p=(snode*)malloc(sizeof(snode));

if (p != NULL)

p->data=n;

p->next = NULL;

if (tail == NULL)

return p;

tail->next = p;

tail = p;

else

printf("Memory not Allocated ! \n ");

return tail;

}
void display(snode *head, snode *tail)

if (head == NULL)

return;

display(head->next, tail);

printf("%d", head->data);

int main()

snode *head = NULL, *tail = NULL;

int ch, n;

do

printf("1.Insert Element\n2.Display in reverse \n3.Exit\nEnter your choice: ");

scanf("%d", &ch);

if (ch == 1)

printf(" Enter value to insert : ");

scanf("%d", &n);

if (head == NULL)

head =enqueue(head, n);

tail = head;

else tail = enqueue(tail, n);

printf("Element Inserted\n");

else if (ch == 2)

if (head != NULL)

printf("Queue Status:"), display(head, tail), printf("\n");


else

printf("Queue Underflow\n");

} while (ch != 3);

return 0;

//OUTPUT

1.Insert Element

2.Display in reverse

3.Exit

Enter your choice: 1

Enter value to insert : 3

Element Inserted

1.Insert Element

2.Display in reverse

3.Exit

Enter your choice: 1

Enter value to insert : 3

Element Inserted

1.Insert Element

2.Display in reverse

3.Exit

Enter your choice: 1

Enter value to insert : 3

Element Inserted

1.Insert Element

2.Display in reverse

3.Exit

Enter your choice: 1

Enter value to insert : 4

Element Inserted
1.Insert Element

2.Display in reverse

3.Exit

Enter your choice:

//QUESTION 2

#include <malloc.h>

#include <stdio.h>

typedef struct node

int data;

struct node *next;

} snode;

snode *push(snode *top, int n)

snode *p= NULL;

p= (snode *)malloc(sizeof(snode));

if (p != NULL)

p ->data = n;

p->next = top;

top =p;

else

printf("Memory not Allocated! \n");

return top;

void disp(snode *top)

{
if (top == NULL)

return;

disp(top->next);

printf("%d ", top->data);

int main()

snode *top = NULL;

int ch, n;

do

printf("1. Push\n2. Display in reverse\n3. Exit\nEnter your choice: ");

scanf("%d", &ch);

if (ch == 1)

printf("Enter value to push: ");

scanf("%d", &n);

top = push(top, n);

printf("Element pushed ! \n");

else if (ch == 2)

if (top != NULL)

printf("Stack status: "), disp(top), printf("\n");

else

printf("stack underflow\n");

} while (ch != 3);

}
//OUTPUT

1. Push

2. Display in reverse

3. Exit

Enter your choice: 1

Enter value to push: 4

Element pushed !

1. Push

2. Display in reverse

3. Exit

Enter your choice: 1

Enter value to push: 5

Element pushed !

1. Push

2. Display in reverse

3. Exit

Enter your choice: 1

Enter value to push: 3

Element pushed !

1. Push

2. Display in reverse

3. Exit

Enter your choice: 2

Stack status: 4 5 3
1. Push

2. Display in reverse

3. Exit

Enter your choice:

//QUESTION 3

#include<stdio.h>

#include<malloc.h>

typedef struct node

struct node *prev;

struct node *next;

int data;

} snode;

void insrt(snode** hd, snode** tl, int n)

snode *p = NULL;

p=(snode*)malloc(sizeof(snode));

if (p != NULL)

p->data = n;

p->next = NULL;

if (*hd == NULL)

*hd = *tl = p;

else

(*tl)->next = p;
*tl = p;

printf("Element Inserted! \n");

else

printf("Memory Not Allocated ! \n");

void del(snode** hd, snode** tl, int k)

int loc = 1;

snode *temp = *hd, *prev;

if (temp != NULL && temp -> data == k)

*hd = temp->next;

free(temp) ;

return;

while (temp !=NULL && temp->data != k)

prev = temp;

temp = temp->next;

loc++;

if (temp == NULL)

return;

printf("%d is found at position %d", k, loc);

prev->next = temp->next;

free(temp);

printf(" Node Deleted ! \n");

void disp(snode* hd, snode* tl)


{

printf("Linked List Status: ");

while (hd != tl)

printf("%d", hd->data);

hd = hd->next;

printf("%d\n", hd->data);

int main()

snode *head = NULL, *tail = NULL;

int ch, n;

do

printf("\n\tMENU\n1. Insert\n2. Delete a Node with Key\n3. Display\n4. Exit\nEnter your


choice: ");

scanf("%d", &ch);

if(ch == 1){

printf("Enter Value to insert: ");

scanf("%d ",&n) ;

insrt(&head, &tail, n);}

else if(ch == 2){

if (head == NULL)

printf("Linked List Underflow\n");

continue;

printf("Enter Key: ");

scanf("%d", &n);

del(&head, &tail, n);


}

else if (ch == 3)

if (head = NULL)

printf("Linked List Underflow\n");

continue;

disp(head, tail);

else if (ch == 4)

break;

else

printf("Invalid Choice!");

}while (ch!= 4);

//output

MENU

1. Insert

2. Delete a Node with Key

3. Display

4. Exit

Enter your choice: 1

Enter Value to insert: 2

Element Inserted!

MENU

1. Insert
2. Delete a Node with Key

3. Display

4. Exit

Enter your choice: Enter Value to insert: 2

You might also like