diff --git a/6,1.cpp b/6,1.cpp new file mode 100644 index 0000000..c6650c3 --- /dev/null +++ b/6,1.cpp @@ -0,0 +1,82 @@ +#include +#include + +typedef struct Node +{ + int data; + struct Node *prev; + struct Node *next; +} Node; + +Node *head = NULL; + +Node *createNode(int data) +{ + Node *newNode = (Node *)malloc(sizeof(Node)); + newNode->data = data; + newNode->prev = NULL; + newNode->next = NULL; + return newNode; +} + +void insertNode(int data, int position) +{ + Node *newNode = createNode(data); + + if (head == NULL || position == 1) + { + newNode->next = head; + head = newNode; + if (head->next != NULL) + { + head->next->prev = newNode; + } + } + else + { + Node *temp = head; + int i = 1; + while (i < position - 1 && temp != NULL) + { + temp = temp->next; + i++; + } + + if (temp == NULL) + { + printf("Invalid position\n"); + return; + } + + newNode->next = temp->next; + newNode->prev = temp; + temp->next = newNode; + if (newNode->next != NULL) + { + newNode->next->prev = newNode; + } + } +} + +void displayList() +{ + Node *temp = head; + while (temp != NULL) + { + printf("%d <-> ", temp->data); + temp = temp->next; + } + printf("NULL\n"); +} + +int main() +{ + insertNode(10, 1); + insertNode(20, 2); + insertNode(30, 3); + insertNode(40, 4); + + displayList(); + +//    return 0; +} diff --git a/6,1.exe b/6,1.exe new file mode 100644 index 0000000..78053ea Binary files /dev/null and b/6,1.exe differ diff --git a/CW(7,1).cpp b/CW(7,1).cpp new file mode 100644 index 0000000..71919a2 --- /dev/null +++ b/CW(7,1).cpp @@ -0,0 +1,85 @@ +#include +#include + +struct p +{ + int data; + struct p *add; +}; + +struct p *start = NULL, *temp, *new1, *next, *prev; +void creat(); +void display(); +void insert_frist(); +void insert_last(); +void main() +{ + int n; + printf("----------------linked list-------------\n"); + do + { + printf("1.craet\n2.dispaly\n3.exit\n"); + scanf("%d", &n); + switch (n) + { + case 1: + creat(); + break; + case 2: + display(); + break; + } + } while (n != 3); +} + +void creat() +{ + char ch; + int n; + printf("enter a element="); + scanf("%d", &n); + start = (struct p *)(malloc(sizeof(struct p))); + start->data = n; + start->add = NULL; + temp = start; + + printf("want to cuntinue (yes or no)\n"); + scanf(" %c", &ch); + while (ch == 'Y' || ch == 'y') + { + printf("entre a next elment ="); + scanf("%d", &n); + new1 = (struct p *)(malloc(sizeof(struct p))); + new1->data = n; + new1->add = NULL; + temp->add = new1; + temp = temp->add; + printf("want to cuntinue 1 (yes or no)\n"); + scanf("%c", &ch); + scanf(" %c", &ch); + ch=getche(); + printf("\n"); + } +} + +void display() +{ + int count = 0; + if (start == NULL) + { + printf("list ist not found"); + } + else + { + temp = start; + while (temp != NULL) + { + if (temp->data != 0) { + count++;} + temp = temp->add; + } + + printf("The number of non-zero elements in the linked list is: %d\n", count); + +    } +} diff --git a/DS(1,2).exe b/DS(1,2).exe new file mode 100644 index 0000000..4d1fba5 Binary files /dev/null and b/DS(1,2).exe differ diff --git a/DS(2,4).cpp b/DS(2,4).cpp new file mode 100644 index 0000000..21a2cb1 --- /dev/null +++ b/DS(2,4).cpp @@ -0,0 +1,50 @@ +#include + +char grade(int marks) { + if (marks > 90) + return 'O'; + else if (marks > 80) + return 'E'; + else if (marks > 70) + return 'A'; + else if (marks > 60) + return 'B'; + else + return 'F'; +} + +int main() { + int n; + printf("Enter number of classes:"); + scanf("%d", &n); + + for (int i = 0; i < n; i++) { + + int sub; + printf("Enter the total number of subjects in class %d:", i + 1); + scanf("%d", &sub); + int students; + printf("Enter number of students in class %d:", i + 1); + scanf("%d", &students); + + int marks[students][sub]; + for (int j = 0; j < students; j++) { + printf("Enter marks of student %d:\n", j + 1); + for (int k = 0; k < sub; k++) { + printf("Enter marks in subject %d:", k + 1); + scanf("%d", &marks[j][k]); + } + } + printf("\n"); + + printf("Class number %d:\n", i + 1); + for (int j = 0; j < students; j++) { + printf("Marks of student number %d:\n", j + 1); + for (int k = 0; k < sub; k++) { + printf("Grade of student in sub %d: %c\n", k + 1, grade(marks[j][k])); + } + } + } + return 0; +} + diff --git a/DS(2,4).exe b/DS(2,4).exe new file mode 100644 index 0000000..d530595 Binary files /dev/null and b/DS(2,4).exe differ diff --git a/DS(3,1).exe b/DS(3,1).exe index 4e932fd..c49adf7 100644 Binary files a/DS(3,1).exe and b/DS(3,1).exe differ diff --git a/DS(3,4).exe b/DS(3,4).exe index ae120d1..cdb1e07 100644 Binary files a/DS(3,4).exe and b/DS(3,4).exe differ diff --git a/DS(4,1).cpp b/DS(4,1).cpp new file mode 100644 index 0000000..9194c96 --- /dev/null +++ b/DS(4,1).cpp @@ -0,0 +1,121 @@ +//#include +//#define n 10 +//int queue[n]; +//int front = -1; +//int rear = -1; +// +//void insert(int x) +//{ +// if (rear==n-1) +// { +// printf("queue is full \n"); +// } +// else if(front==-1 && rear==-1) +// { +// front=rear=0; +// queue[rear]=x; +// } +//} +// +//void delet() +//{ +// if(front==-1 && rear==-1) +// { +// printf("queue is empty \n"); +// } +// else if(front==rear) +// { +// // printf("%d",queue[front]); +// front=rear=-1; +// } +// else +// { +// front++; +// } +//} +// +//void display() +//{ +// int i; +// if(front==-1 && rear==-1) +// { +// printf("queue is empty \n"); +// } +// else +// { +// for(i=front;i +# define max 10 +int queue[max]; +int front = -1; +int rear = -1; +int insert(int ele){ + if (rear == max - 1) + { + printf("Queue is full\n"); + } + if (front == -1) + front = 0; + rear++; + queue[rear] = ele; + printf("The inserted element is %d\n",ele); + +} +int delt() +{ + int item; + if (front == -1 || front > rear) + { + printf("Queue is empty\n"); + return -1; + } + item = queue[front]; + front++; + return item; +} +void display() +{ + int i; + if (front == -1 && front > rear) + { + printf("Queue is empty\n"); + return; + } + printf("The queue elements are:\n"); + for (i = front; i <= rear; i++) + printf("%d\n", queue[i]); + +} +int main(){ + printf("insert numbers: \n"); + insert(1); + insert(6); + insert(9); + delt(); + insert(5); + insert(7); + insert(8); + delt(); + display(); +} + diff --git a/DS(4,1).exe b/DS(4,1).exe new file mode 100644 index 0000000..98ab70e Binary files /dev/null and b/DS(4,1).exe differ diff --git a/DS(4,2).cpp b/DS(4,2).cpp new file mode 100644 index 0000000..920f8c5 --- /dev/null +++ b/DS(4,2).cpp @@ -0,0 +1,54 @@ +#include +# define max 10 +int queue[max]; +int front = -1; +int rear = -1; +int cqinsert(int ele){ + if (rear == max - 1) + { + printf("Queue is full\n"); + } + if (front == -1) + front = 0; + rear++; + queue[rear % max] = ele; + printf("The inserted element is %d\n",ele); + +} +int cqdelt() +{ + int item; + if (front == -1 || front > rear) + { + printf("Queue is empty\n"); + return -1; + } + item = queue[front]; + front=(front+1)%max; + return item; +} +void display() +{ + int i; + if (front == -1 && front > rear) + { + printf("Queue is empty\n"); + return; + } + printf("The queue elements are:\n"); + for (i = front; i <= rear; i++) + printf("%d\n", queue[i]); + +} +int main(){ + printf("insert numbers: \n"); + cqinsert(1); + cqinsert(6); + cqinsert(9); + cqdelt(); + cqinsert(5); + cqinsert(7); + cqinsert(8); + cqdelt(); + display(); +} diff --git a/DS(4,2).exe b/DS(4,2).exe new file mode 100644 index 0000000..ec8b2b6 Binary files /dev/null and b/DS(4,2).exe differ diff --git a/DS(4,3).cpp b/DS(4,3).cpp new file mode 100644 index 0000000..5da2bb1 --- /dev/null +++ b/DS(4,3).cpp @@ -0,0 +1,141 @@ +#include +#include +# define N 10 +int stack1[N], stack2[N]; +int top_stack1 = -1; +int top_stack2 = -1; +int count = 0; +void push_stack1 (int data) +{ + if (top_stack1 == N - 1) + { + printf ("Stack1 is overflow"); + return; + } + + else + { + top_stack1++; + stack1[top_stack1] = data; + } + + return; +} + +void push_stack2 (int data) +{ + if (top_stack2 == N - 1) + { + printf ("Stack2 is overflow"); + return; + } + + else + { + top_stack2++; + stack2[top_stack2] = data; + } + + return; + +} + +int pop_stack1 () +{ + if (top_stack1 == -1) + { + printf ("Stack1 is underflow\n"); + return -1; + } + + return stack1[top_stack1--]; +} + +int pop_stack2 () +{ + + if (top_stack2 == -1) + { + printf ("Stack2 is underflow\n"); + return -1; + } + + return stack2[top_stack2--]; + +} + +void enqueue (int data) +{ + push_stack1 (data); + count++; + +} + +void dequeue () +{ + if (top_stack1 == -1 && top_stack2 == -1) + printf ("Queue is empty\n"); + + else + { + for (int i = 0; i < count; i++) + { + + int temp = pop_stack1 (); + push_stack2 (temp); + } + + int x = pop_stack2 (); + + printf ("Dequeued element is: %d", x); + printf("\n"); + count--; + + for (int i = 0; i < count; i++) + { + + int temp = pop_stack2 (); + push_stack1 (temp); + + } + } +} + +void display () +{ + if (top_stack1 == -1) + { + printf ("Queue is empty \n"); + return; + } + + for (int i = 0; i < top_stack1; i++) + printf ("%d ", stack1[i]); + + printf ("\n"); + +} + +void top () +{ + printf ("Top element of queue is %d \n", stack1[0]); +} + +int main () +{ + + enqueue (3); + enqueue (4); + enqueue (1); + + display (); + + dequeue (); + enqueue (5); + enqueue (2); + enqueue (3); + display (); + + return 0; + +} diff --git a/DS(4,3).exe b/DS(4,3).exe new file mode 100644 index 0000000..41a1e10 Binary files /dev/null and b/DS(4,3).exe differ diff --git a/DS(5,1).cpp b/DS(5,1).cpp new file mode 100644 index 0000000..7526b4d --- /dev/null +++ b/DS(5,1).cpp @@ -0,0 +1,72 @@ +/*write a c program to implement a singly linked list and perform an insertion operation to insert a new node after a specific element in the list.*/ +#include +#include +struct node{ + int data; + struct node*next; +}; +struct node*traversal(struct node*ptr) + { + while (ptr != NULL) + { + printf("Element: %d\n", ptr->data); + ptr = ptr->next; + } + return 0; +} + +int insertAfter(struct node* ptr, int ele, int newData) { + if (ptr == NULL) { + printf("The ptr cannot be NULL.\n"); + } + + struct node* newNode = (struct node*)malloc(sizeof(struct node)); + newNode->data = newData; + + while (ptr != NULL) { + if(ptr->data == ele) + { + newNode->next = ptr->next; + ptr->next = newNode; + break; + } + ptr = ptr->next; + } + return 0; +} + +int main(){ + + struct node*head = (struct node*)malloc(sizeof(struct node)); + + struct node*one = (struct node*)malloc(sizeof(struct node)); + one->data = 10; + + struct node*second=(struct node*)malloc(sizeof(struct node)); + second->data = 30; + + struct node*third=(struct node*)malloc(sizeof(struct node)); + third->data = 20; + + struct node*fourth=(struct node*)malloc(sizeof(struct node)); + fourth->data = 40; + + struct node*fifth=(struct node*)malloc(sizeof(struct node)); + fifth->data = 50; + + head = one; + one->next = second; + second->next = third; + third->next = fourth; + fourth->next = fifth; + fifth->next = NULL; + + printf("Original List: "); + traversal(head); + + insertAfter(head, 20, 25); + + printf("\n New list will be:\n"); + traversal(head); + return 0; +} diff --git a/DS(5,1).exe b/DS(5,1).exe new file mode 100644 index 0000000..b40e1c5 Binary files /dev/null and b/DS(5,1).exe differ diff --git a/DS(5,2).cpp b/DS(5,2).cpp new file mode 100644 index 0000000..d463165 --- /dev/null +++ b/DS(5,2).cpp @@ -0,0 +1,70 @@ +//2 +#include +#include + +struct Node { + int data; + struct Node* next; +}; +void traverse(struct Node* head) { + struct Node* ptr = head; + + while (ptr != NULL) { + printf("%d ", ptr->data); + ptr = ptr->next; + } + printf("\n"); +} + + +struct Node* search(struct Node* head, int ele) { + struct Node* ptr = head; + + while (ptr != NULL) { + if (ptr->data == ele) { + return ptr; + } + ptr = ptr->next; + } + + return NULL; +} + +int main() { + struct Node* head = NULL; + + head = (struct Node*)malloc(sizeof(struct Node)); + head->data = 10; + + struct Node* second = (struct Node*)malloc(sizeof(struct Node)); + second->data = 30; + + struct Node* third = (struct Node*)malloc(sizeof(struct Node)); + third->data = 20; + + struct Node* fourth = (struct Node*)malloc(sizeof(struct Node)); + fourth->data = 40; + + struct Node* fifth = (struct Node*)malloc(sizeof(struct Node)); + fifth->data = 50; + + head->next = second; + second->next = third; + third->next = fourth; + fourth->next = fifth; + fifth->next = NULL; + + printf("Original List: "); + traverse(head); + + struct Node* targetele = search(head, 30); + + if (targetele != NULL) { + printf("The target node with the value 30 is found at address %d\n", targetele); + } else { + printf("The target node with the value 30 is not found in the list.\n"); + } + + return 0; +} + diff --git a/DS(5,2).exe b/DS(5,2).exe new file mode 100644 index 0000000..8052b16 Binary files /dev/null and b/DS(5,2).exe differ diff --git a/DS(5,3).cpp b/DS(5,3).cpp new file mode 100644 index 0000000..229d1ac --- /dev/null +++ b/DS(5,3).cpp @@ -0,0 +1,56 @@ +#include +#include +struct node{ + int data; + struct node*next; +}; +struct node*traversal(struct node*ptr) + { + while (ptr != NULL) + { + printf("Element: %d\n", ptr->data); + ptr = ptr->next; + } + return 0; +} + +int main(){ + + struct node*head = (struct node*)malloc(sizeof(struct node)); + + struct node*one = (struct node*)malloc(sizeof(struct node)); + one->data = 10; + + struct node*second=(struct node*)malloc(sizeof(struct node)); + second->data = 30; + + struct node*third=(struct node*)malloc(sizeof(struct node)); + third->data = 20; + + struct node*fourth=(struct node*)malloc(sizeof(struct node)); + fourth->data = 40; + + struct node*fifth=(struct node*)malloc(sizeof(struct node)); + fifth->data = 50; + + head = one; + one->next = second; + second->next = third; + third->next = fourth; + fourth->next = fifth; + fifth->next = NULL; + + printf("Original List: "); + traversal(head); + + int sum=0; + struct node* ptr = head; + + while (ptr != NULL) { + sum += ptr->data; + ptr = ptr->next; + } + + printf("sum of all elements in the list is:%d",sum); + return 0; +} diff --git a/DS(5,3).exe b/DS(5,3).exe new file mode 100644 index 0000000..926ad8a Binary files /dev/null and b/DS(5,3).exe differ diff --git a/Untitled1.cpp b/Untitled1.cpp new file mode 100644 index 0000000..1c54ae4 --- /dev/null +++ b/Untitled1.cpp @@ -0,0 +1,7 @@ +#include +#include +struct node{ + int data; + struct node*next; +}; +struct trasver