Вы находитесь на странице: 1из 47

REVERSE A STACK USING RECURSION :

#include <stdio.h>
#include <stdlib.h>

struct node
{
int a;
struct node *next;
};

void generate(struct node **);


void display(struct node *);
void stack_reverse(struct node **, struct node **);
void delete(struct node **);

int main()
{
struct node *head = NULL;

generate(&head);
printf("\nThe sequence of contents in stack\n");
display(head);
printf("\nInversing the contents of the stack\n");
if (head != NULL)
{
stack_reverse(&head, &(head->next));
}
printf("\nThe contents in stack after reversal\n");
display(head);
delete(&head);

return 0;
}

void stack_reverse(struct node **head, struct node **head_next)

{
struct node *temp;

if (*head_next != NULL)
{
temp = (*head_next)->next;
(*head_next)->next = (*head);
*head = *head_next;
*head_next = temp;
stack_reverse(head, head_next);
}
}

void display(struct node *head)


{
if (head != NULL)
{
printf("%d ", head->a);
display(head->next);
}
}

void generate(struct node **head)


{
int num, i;
struct node *temp;

printf("Enter length of list: ");


scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{

*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}

void delete(struct node **head)


{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}

REVERSE STACK WITHOUT RECURSION :


#include <stdio.h>
#include <stdlib.h>

struct node
{
int a;
struct node *next;
};

void generate(struct node **);


void display(struct node *);
void stack_reverse(struct node **);

void delete(struct node **);

int main()
{
struct node *head = NULL;

generate(&head);
printf("\nThe sequence of contents in stack\n");
display(head);
printf("\nInversing the contents of the stack\n");
stack_reverse(&head);
printf("\nThe contents in stack after reversal\n");
display(head);
delete(&head);
return 0;
}

void stack_reverse(struct node **head)


{
struct node *temp, *prev;

if (*head == NULL)
{
printf("Stack does not exist\n");
}
else if ((*head)->next == NULL)
{
printf("Single node stack reversal brings no difference\n");
}
else if ((*head)->next->next == NULL)
{
(*head)->next->next = *head;
*head = (*head)->next;
(*head)->next->next = NULL;
}

else
{
prev = *head;
temp = (*head)->next;
*head = (*head)->next->next;
prev->next = NULL;
while ((*head)->next != NULL)
{
temp->next = prev;
prev = temp;
temp = *head;
*head = (*head)->next;
}
temp->next = prev;
(*head)->next = temp;
}
}

void display(struct node *head)


{
if (head != NULL)
{
printf("%d ", head->a);
display(head->next);
}
}

void generate(struct node **head)


{
int num, i;
struct node *temp;

printf("Enter length of list: ");


scanf("%d", &num);
for (i = num; i > 0; i--)

{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}

void delete(struct node **head)


{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}

IMPLEMENT PRIORITY QUEUE TO ADD OR DELETE ELEMENTS :


#include <stdio.h>
#include <stdlib.h>

#define MAX 5

void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();

int pri_que[MAX];
int front, rear;

void main()
{
int n, ch;

printf("\n1 - Insert an element into queue");


printf("\n2 - Delete an element from queue");
printf("\n3 - Display queue elements");
printf("\n4 - Exit");

create();

while (1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("\nEnter value to be inserted : ");
scanf("%d",&n);
insert_by_priority(n);
break;
case 2:
printf("\nEnter value to delete : ");
scanf("%d",&n);

delete_by_priority(n);
break;
case 3:
display_pqueue();
break;
case 4:
exit(0);
default:
printf("\nChoice is incorrect, Enter a correct choice");
}
}
}

/* Function to create an empty priority queue */


void create()
{
front = rear = -1;
}

/* Function to insert value into priority queue */


void insert_by_priority(int data)
{
if (rear >= MAX - 1)
{
printf("\nQueue overflow no more elements can be inserted");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pri_que[rear] = data;
return;
}
else

check(data);
rear++;
}

/* Function to check priority and place element */


void check(int data)
{
int i,j;

for (i = 0; i <= rear; i++)


{
if (data >= pri_que[i])
{
for (j = rear + 1; j > i; j--)
{
pri_que[j] = pri_que[j - 1];
}
pri_que[i] = data;
return;
}
}
pri_que[i] = data;
}

/* Function to delete an element from queue */


void delete_by_priority(int data)
{
int i;

if ((front==-1) && (rear==-1))


{
printf("\nQueue is empty no elements to delete");
return;
}

for (i = 0; i <= rear; i++)


{
if (data == pri_que[i])
{
for (; i < rear; i++)
{
pri_que[i] = pri_que[i + 1];
}

pri_que[i] = -99;
rear--;

if (rear == -1)
front = -1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}

/* Function to display queue elements */


void display_pqueue()
{
if ((front == -1) && (rear == -1))
{
printf("\nQueue is empty");
return;
}

for (; front <= rear; front++)


{
printf(" %d ", pri_que[front]);
}

front = 0;

}
CHECK STRING IS PALINDROME USING STACK :
#include <stdio.h>
#include <string.h>

void push(char);
char pop();

char stack[100];
int top = -1;

void main()
{
char str[100];
int i, count = 0, len;

printf("Enter string to check it is palindrome or not : ");


scanf("%s", str);

len = strlen(str);

for (i = 0; i < len; i++)


{
push(str[i]);
}

for (i = 0; i < len; i++)


{
if (str[i] == pop())
count++;
}

if (count == len)
printf("%s is a Palindrome string\n", str);
else

printf("%s is not a palindrome string\n", str);


}

/* Function to push character into stack */


void push(char c)
{
stack[++top] = c;
}

/* Function to pop the top character from stack */


char pop()
{
return(stack[top--]);
}

PARENTHESIS CHECK :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top = -1;


char stack[100];

// function prototypes
void push(char);
void pop();
void find_top();

void main()
{
int i;
char a[100];
printf("enter expression\n");
scanf("%s", &a);
for (i = 0; a[i] != '\0';i++)
{
if (a[i] == '(')
{
push(a[i]);
}
else if (a[i] == ')')
{
pop();
}
}
find_top();
}

// to push elements in stack


void push(char a)
{
stack[top] = a;
top++;
}

// to pop elements from stack


void pop()
{
if (top == -1)
{
printf("expression is invalid\n");
exit(0);

}
else
{
top--;
}
}

// to find top element of stack


void find_top()
{
if (top == -1)
printf("\nexpression is valid\n");
else
printf("\nexpression is invalid\n");
}

IMPLEMENT QUEUE OPERATIONS USING DYNAMIC MEMORY ALLOCATION :


#include <stdio.h>
#include <stdlib.h>
#define MAX 10

struct node
{
int data;
struct node *link;
}*front, *rear;

// function protypes
void insert();
void delete();

void queue_size();
void check();
void first_element();

void main()
{
int choice, value;

while(1)
{
printf("enter the choice \n");
printf("1 : create an empty queue \n2 : Insert element\n");
printf("3 : Dequeue an element \n4 : Check if empty\n");
printf("5. Get the first element of the queue\n");
printf("6. Get the number of entries in the queue\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice)

// menu driven program

{
case 1:
printf("Empty queue is created with a capacity of %d\n", MAX);
break;
case 2:
insert();
break;
case 3:
delete();
break;
case 4:
check();
break;
case 5:
first_element();
break;
case 6:

queue_size();
break;
case 7:
exit(0);
default:
printf("wrong choice\n");
break;
}
}
}

// to insert elements in queue


void insert()
{
struct node *temp;

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


printf("Enter value to be inserted \n");
scanf("%d", &temp->data);
temp->link = NULL;
if (rear == NULL)
{
front = rear = temp;
}
else
{
rear->link = temp;
rear = temp;
}
}

// delete elements from queue


void delete()
{
struct node *temp;

temp = front;
if (front == NULL)
{
printf("queue is empty \n");
front = rear = NULL;
}
else
{
printf("deleted element is %d\n", front->data);
front = front->link;
free(temp);
}
}

// check if queue is empty or not


void check()
{
if (front == NULL)
printf("\nQueue is empty\n");
else
printf("*************** Elements are present in the queue **************\n");
}

// returns first element of queue


void first_element()
{
if (front == NULL)
{
printf("**************** The queue is empty ****************\n");
}
else
printf("**************** The front element is %d ***********\n", front->data);
}

// returns number of entries and displays the elements in queue


void queue_size()
{
struct node *temp;

temp = front;
int cnt = 0;
if (front == NULL)
{
printf(" queue empty \n");
}
while (temp)
{
printf("%d ", temp->data);
temp = temp->link;
cnt++;
}
printf("********* size of queue is %d ******** \n", cnt);
}

IMPLEMENT STACK OPERATIONS USING DYNAMIC MEMORY ALLOCATION :


#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *link;
}*top = NULL;

#define MAX 5

// function prototypes
void push();
void pop();

void empty();
void stack_full();
void stack_count();
void destroy();
void print_top();

void main()
{
int choice;

while (1)
{
printf("1. push an element \n");
printf("2. pop an element \n");
printf("3. check if stack is empty \n");
printf("4. check if stack is full \n");
printf("5. count/display elements present in stack \n");
printf("6. empty and destroy stack \n");
printf("7. Print top of the stack \n");
printf("8. exit \n");
printf("Enter your choice \n");
scanf("%d",&choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
empty();
break;
case 4:
stack_full();

break;
case 5:
stack_count();
break;
case 6:
destroy();
break;
case 7:
print_top();
break;
case 8:
exit(0);
default:
printf("wrong choice\n");
}
}
}

// to insert elements in stack


void push()
{
int val,count;
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));

count = st_count();
if (count <= MAX - 1)
{
printf("\nEnter value which you want to push into the stack :\n");
scanf("%d",&val);
temp->data = val;
temp->link = top;
top = temp;
}
else

printf("WARNING: STACK FULL\n");


}

// to delete elements from stack


void pop()
{
struct node *temp;
if (top = = NULL)
printf("**Stack is empty**\n");
else
{
temp = top;
printf("Value popped out is %d \n",temp->data);
top = top->link;
free(temp);
}
}

// to check if stack is empty


void empty()
{
if (top = = NULL)
printf("STACK IS EMPTY\n");
else
printf("elements are present, stack is not empty \n");
}

// to check if stack is full


void stack_full()
{
int count;

count = st_count();
if (count = = MAX)
{

printf("stack is full\n");
}
else
printf("stack is not full \n");
}

// to count the number of elements


void stack_count()
{
int count = 0;
struct node *temp;

temp = top;
while (temp! = NULL)
{
printf(" %d\n",temp->data);
temp = temp->link;
count++;
}
printf("size of stack is %d \n",count);
}

int st_count()
{
int count = 0;
struct node *temp;
temp = top;
while (temp! = NULL)
{
temp = temp->link;
count++;
}
return count;
}

// to empty and destroy the stack


void destroy()
{
struct node *temp;
temp = top;
while (temp! = NULL)
{
pop();
temp = temp->link;
}
printf("stack destroyed\n");
}

// to print top element of stack


void print_top()
{
if (top == NULL)
printf("\n**Top is not available for an EMPTY stack**\n");
else
printf("\nTop of the stack is %d \n",top->data);
}

IMPLEMENT QUEUE USING STACK


#include <stdio.h>
#include <stdlib.h>

void push1(int);
void push2(int);
int pop1();
int pop2();
void enqueue();
void dequeue();
void display();
void create();

int st1[100], st2[100];


int top1 = -1, top2 = -1;
int count = 0;

void main()
{
int ch;

printf("\n1 - Enqueue element into queue");


printf("\n2 - Dequeu element from queue");
printf("\n3 - Display from queue");
printf("\n4 - Exit");
create();
while (1)
{
printf("\nEnter choice");
scanf("%d", &ch);
switch (ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice");
}
}
}

/*Function to create a queue*/


void create()
{
top1 = top2 = -1;
}

/*Function to push the element on to the stack*/


void push1(int data)
{
st1[++top1] = data;
}

/*Function to pop the element from the stack*/


int pop1()
{
return(st1[top1--]);
}

/*Function to push an element on to stack*/


void push2(int data)
{
st2[++top2] = data;
}

/*Function to pop an element from th stack*/

int pop2()
{
return(st2[top2--]);
}

/*Function to add an element into the queue using stack*/


void enqueue()
{

int data, i;

printf("Enter data into queue");


scanf("%d", &data);
push1(data);
count++;
}

/*Function to delete an element from the queue using stack*/

void dequeue()
{
int i;

for (i = 0;i <= count;i++)


{
push2(pop1());
}
pop2();
count--;
for (i = 0;i <= count;i++)
{
push1(pop2());
}
}

/*Function to display the elements in the stack*/

void display()
{
int i;

for (i = 0;i <= top1;i++)


{
printf(" %d ", st1[i]);

}
}

IMPLEMENT OUEUE USING TWO STACKS :


#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void push(struct node** top, int data);
int pop(struct node** top);
struct queue
{
struct node *stack1;
struct node *stack2;
};
void enqueue(struct queue *q, int x)
{
push(&q->stack1, x);
}
void dequeue(struct queue *q)
{
int x;
if (q->stack1 == NULL && q->stack2 == NULL) {
printf("queue is empty");
return;
}
if (q->stack2 == NULL) {

while (q->stack1 != NULL) {


x = pop(&q->stack1);
push(&q->stack2, x);
}
}
x = pop(&q->stack2);
printf("%d\n", x);
}
void push(struct node** top, int data)
{
struct node* newnode = (struct node*) malloc(sizeof(struct node));
if (newnode == NULL) {
printf("Stack overflow \n");
return;
}
newnode->data = data;
newnode->next = (*top);
(*top) = newnode;
}
int pop(struct node** top)
{
int buff;
struct node *t;
if (*top == NULL) {
printf("Stack underflow \n");
return;
}
else {
t = *top;
buff = t->data;
*top = t->next;
free(t);
return buff;
}
}

void display(struct node *top1,struct node *top2)


{
while (top1 != NULL) {
printf("%d\n", top1->data);
top1 = top1->next;
}
while (top2 != NULL) {
printf("%d\n", top2->data);
top2 = top2->next;
}
}
int main()
{
struct queue *q = (struct queue*)malloc(sizeof(struct queue));
int f = 0, a;
char ch = 'y';
q->stack1 = NULL;
q->stack2 = NULL;
while (ch == 'y'||ch == 'Y') {
printf("enter ur choice\n1.add to queue\n2.remove
from queue\n3.display\n4.exit\n");
scanf("%d", &f);
switch(f) {
case 1 : printf("enter the element to be added to queue\n");
scanf("%d", &a);
enqueue(q, a);
break;
case 2 : dequeue(q);
break;
case 3 : display(q->stack1, q->stack2);
break;
case 4 : exit(1);
break;
default : printf("invalid\n");
break;

}
}
}

C PROGRAM TO PRINT ALTERNATE NODE IN A LINKED LIST USING RECURSION :


#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void push(struct node** top, int data);
int pop(struct node** top);
struct queue
{
struct node *stack1;
struct node *stack2;
};
void enqueue(struct queue *q, int x)
{
push(&q->stack1, x);
}
void dequeue(struct queue *q)
{
int x;
if (q->stack1 == NULL && q->stack2 == NULL) {
printf("queue is empty");
return;
}
if (q->stack2 == NULL) {
while (q->stack1 != NULL) {
x = pop(&q->stack1);
push(&q->stack2, x);

}
}
x = pop(&q->stack2);
printf("%d\n", x);
}
void push(struct node** top, int data)
{
struct node* newnode = (struct node*) malloc(sizeof(struct node));
if (newnode == NULL) {
printf("Stack overflow \n");
return;
}
newnode->data = data;
newnode->next = (*top);
(*top) = newnode;
}
int pop(struct node** top)
{
int buff;
struct node *t;
if (*top == NULL) {
printf("Stack underflow \n");
return;
}
else {
t = *top;
buff = t->data;
*top = t->next;
free(t);
return buff;
}
}
void display(struct node *top1,struct node *top2)
{
while (top1 != NULL) {

printf("%d\n", top1->data);
top1 = top1->next;
}
while (top2 != NULL) {
printf("%d\n", top2->data);
top2 = top2->next;
}
}
int main()
{
struct queue *q = (struct queue*)malloc(sizeof(struct queue));
int f = 0, a;
char ch = 'y';
q->stack1 = NULL;
q->stack2 = NULL;
while (ch == 'y'||ch == 'Y') {
printf("enter ur choice\n1.add to queue\n2.remove
from queue\n3.display\n4.exit\n");
scanf("%d", &f);
switch(f) {
case 1 : printf("enter the element to be added to queue\n");
scanf("%d", &a);
enqueue(q, a);
break;
case 2 : dequeue(q);
break;
case 3 : display(q->stack1, q->stack2);
break;
case 4 : exit(1);
break;
default : printf("invalid\n");
break;
}
}
}

C PROGRAM TO PRINT ALTERNATE NODE IN A LINKED LIST WITHOUT USING RECURSION :


#include <stdio.h>
#include <stdlib.h>

struct node
{
int a;
struct node *next;
};

void generate(struct node **);


void display(struct node *);
void delete(struct node **);

int main()
{
struct node *head = NULL;

generate(&head);
printf("\nDisplaying the alternate nodes\n");
display(head);
delete(&head);

return 0;
}

void display(struct node *head)


{
int flag = 0;

while(head != NULL)
{
if (!(flag % 2))

{
printf("%d ", head->a);
}
flag++;
head = head->next;
}
}

void generate(struct node **head)


{
int num, i;
struct node *temp;

printf("Enter length of list: ");


scanf("%d", &num);
for (i = num; i > 0; i--)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}

void delete(struct node **head)


{
struct node *temp;

while (*head != NULL)


{
temp = *head;
*head = (*head)->next;
free(temp);
}
}}

Check WHETHER 2 LINKED LIST ARE SAME :


#include <stdio.h>
#include <stdlib.h>

struct node
{
int num;
struct node *next;
};

void feedmember(struct node **);


int compare (struct node *, struct node *);
void release(struct node **);

int main()
{
struct node *p = NULL;
struct node *q = NULL;
int result;

printf("Enter data into first list\n");


feedmember(&p);
printf("Enter data into second list\n");
feedmember(&q);
result = compare(p, q);
if (result == 1)
{

printf("The 2 list are equal.\n");


}
else
{
printf("The 2 lists are unequal.\n");
}
release (&p);
release (&q);

return 0;
}

int compare (struct node *p, struct node *q)


{
while (p != NULL && q != NULL)
{
if (p->num != q-> num)
{
return 0;
}
else
{
p = p->next;
q = q->next;
}
}
if (p != NULL || q != NULL)
{
return 0;
}
else
{
return 1;
}
}

void feedmember (struct node **head)


{
int c, ch;
struct node *temp;

do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = *head;
*head = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
}while (ch != 0);
printf("\n");
}

void release (struct node **head)


{
struct node *temp = *head;

while ((*head) != NULL)


{
(*head) = (*head)->next;
free(temp);
temp = *head;
}
}

PROGRAM TO FIND FIRST COMMON ELEMENT IN TWO LINKED LIST :


#include <stdio.h>
#include <stdlib.h>

struct node
{
int num;
struct node *next;
};

void create(struct node **);


int find(struct node *, struct node *);
void release(struct node **);
void display(struct node *);

int main()
{
struct node *p = NULL, *q = NULL;
int result;

printf("Enter data into the list\n");


create(&p);
printf("Enter data into the list\n");
create(&q);
printf("Displaying list1:\n");
display(p);
printf("Displaying list2:\n");
display(q);
result = find(p, q);
if (result)
{
printf("The first matched element is %d.\n", result);
}
else
{
printf("No matching element found.\n");
}
release (&p);

return 0;
}

int find(struct node *p, struct node *q)


{
struct node *temp;

while (p != NULL)
{
temp = q;
while (temp != NULL)
{
if (temp->num == p->num)
{
return p->num;
}
temp = temp->next;
}
p = p->next;
}

/*Assuming 0 is not used in the list*/


return 0;
}

void create(struct node **head)


{
int c, ch;
struct node *temp, *rear;

do
{
printf("Enter number: ");
scanf("%d", &c);

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


temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}

void display(struct node *head)


{
while (head != NULL)
{
printf("%d\t", head->num);
head = head->next;
}
printf("\n");
}

void release(struct node **head)


{
struct node *temp;
while ((*head) != NULL)
{
temp = *head;
(*head) = (*head)->next;

free(temp);
}
}

REVERSE FIRST N ELEMENTS LINKED LIST :


#include <stdio.h>
#include <stdlib.h>

struct node
{
int num;
struct node *next;
};

void create(struct node **);


void reverse(struct node **, int);
void release(struct node **);
void display(struct node *);

int main()
{
struct node *p = NULL;
int n;

printf("Enter data into the list\n");


create(&p);
printf("Displaying the nodes in the list:\n");
display(p);
printf("Enter the number N to reverse first N node: ");
scanf("%d", &n);
printf("Reversing the list...\n");
if (n > 1)
{
reverse(&p, n - 2);
}

printf("Displaying the reversed list:\n");


display(p);
release(&p);

return 0;
}

void reverse(struct node **head, int n)


{
struct node *p, *q, *r, *rear;

p = q = r = *head;
if (n == 0)
{
q = q->next;
p->next = q->next;
q->next = p;
*head = q;
}
else
{
p = p->next->next;
q = q->next;
r->next = NULL;
rear = r;
q->next = r;

while (n > 0 && p != NULL)


{
r = q;
q = p;
p = p->next;
q->next = r;
n--;
}

*head = q;
rear->next = p;
}
}

void create(struct node **head)


{
int c, ch;
struct node *temp, *rear;

do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}

void display(struct node *p)


{
while (p != NULL)

{
printf("%d\t", p->num);
p = p->next;
}
printf("\n");
}

void release(struct node **head)


{
struct node *temp = *head;
*head = (*head)->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}

REMOVE DUPLICATES IN LINKED LIST:


#include <stdio.h>
#include <stdlib.h>

struct node
{
int num;
struct node *next;
};

void create(struct node **);


void dup_delete(struct node **);
void release(struct node **);
void display(struct node *);

int main()

{
struct node *p = NULL;
struct node_occur *head = NULL;
int n;

printf("Enter data into the list\n");


create(&p);
printf("Displaying the nodes in the list:\n");
display(p);
printf("Deleting duplicate elements in the list...\n");
dup_delete(&p);
printf("Displaying non-deleted nodes in the list:\n");
display(p);
release(&p);

return 0;
}

void dup_delete(struct node **head)


{
struct node *p, *q, *prev, *temp;

p = q = prev = *head;
q = q->next;
while (p != NULL)
{
while (q != NULL && q->num != p->num)
{
prev = q;
q = q->next;
}
if (q == NULL)
{
p = p->next;
if (p != NULL)

{
q = p->next;
}
}
else if (q->num == p->num)
{
prev->next = q->next;
temp = q;
q = q->next;
free(temp);
}
}
}

void create(struct node **head)


{
int c, ch;
struct node *temp, *rear;

do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;

printf("Do you wish to continue [1/0]: ");


scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}

void display(struct node *p)


{
while (p != NULL)
{
printf("%d\t", p->num);
p = p->next;
}
printf("\n");
}

void release(struct node **head)


{
struct node *temp = *head;
*head = (*head)->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}

Вам также может понравиться