C two-way list deleting items - c

I have a problem. The code below creates a two-way looped list. When I want to delete an item of specyfic key value, everything goes right. The problem comes when I want to add an item after deleting another one. It just doesn't appear when I display the list. Do you have any ideas why?
#include <iostream>
#include <time.h>
#include <vector>
#include <stdlib.h>
struct List
{
int key;
double val1, val2, val3;
struct List *next, *prev;
}*element, *temp, *head;
bool ifrepeat = false,ifdelete;
void AddOne(struct List *&head1, int x)
{
List *tmp = head1;
if (head1 != NULL)
{
do
{
if (head1->key == x)
{
ifrepeat = true;
}
head1 = head1->next;
} while (head1 != tmp);
}
if (ifrepeat)
{
printf("Podana wartosc klucza juz istnieje!\n");
ifrepeat = false;
}
else
{
head1 = tmp;
if (head1 == NULL)
{
head1 = (struct List*)malloc(sizeof(struct List));
head1->key = x;
head1->val1 = 1 + (rand() % 1000);
head1->val2 = 1 + rand() % 1000;
head1->val3 = 1 + rand() % 1000;
head1->next = head1;
head1->prev = head1;
}
else
{
if (element == NULL)
{
element = (struct List*)malloc(sizeof(struct List));
element->key = x;
head->next = element;
element->prev = head1;
element->next = head1;
head1->prev = element;
temp = element;
}
else
{
element = (struct List*)malloc(sizeof(struct List));
element->key = x;
temp->next = element;
element->prev = temp;
element->next = head1;
head1->prev = element;
temp = element;
}
}
}
}
void ShowBegin(struct List *head)
{
if (head == NULL)
printf("Lista jest pusta!");
else
{
struct List *temphead;
temphead = head;
printf("%d ", head->key);
head = head->next;
while (head != temphead)
{
printf("%d ", head->key);
head = head->next;
}
printf("\n");
}
}
void AddMany(struct List *&head1, int x)
{
AddOne(head, 10+(rand()%1000)*(rand()%1000));
int tmpkey;
List *tmp = head1;
for (int i = 0; i < x; i++)
{
tmpkey = 10 + (rand() % 100)*(rand() % 1000);
while (head1 != tmp)
{
while (head->key == tmpkey)
{
tmpkey = 10 + (rand() % 100)*(rand() % 1000);
head1 = tmp;
}
head1 = head1->next;
}
AddOne(head, tmpkey);
head1 = tmp;
head1 = head1->next;
}
}
void Sort(struct List *&head1)
{
int max;
List *maxPos,*tmp;
maxPos = head1;
max = head1->key;
while (true)
{
head1 = head1->next;
if (head1->key > max)
{
tmp = head1;
head1 = maxPos;
maxPos = tmp;
max = maxPos->key;
head1 = head;
}
}
}
void CreateList()
{
head = NULL;
element = NULL;
temp = NULL;
}
void Delete(struct List *&head1, int x)
{
struct List *temphead;
temphead = head1;
if (head == NULL)
{
printf("Lista jest pusta!\n");
return;
}
else
{
head1 = head1->next;
if (head1 == temphead)
{
if (head1->key == x)
{
free(head1);
head1 = NULL;
ifdelete = true;
}
else
ifdelete = false;
}
else
{
while (head1 != temphead)
{
if (head1->key == x)
{
List *temp;
temp = head1;
head1->prev->next = head1->next;
head1->next->prev = head1->prev;
head1 = head1->next;
while (head1 != temphead)
{
}
free(temp);
ifdelete = true;
break;
}
else
ifdelete = false;
head1 = head1->next;
}
}
if (!ifdelete)
{
printf("Element o podanym kluczu nie istnieje!\n");
}
}
}
void ShowEnd(struct List *&head)
{
if (head == NULL)
printf("Lista jest pusta!");
else
{
struct List *temphead;
temphead = head;
head = head->prev;
while (head != temphead)
{
printf("%d ", head->key);
head = head->prev;
}
printf("%d", temphead->key);
printf("\n");
}
}
void DeleteAll(struct List *&head)
{
free(head);
head = NULL;
}
int main()
{
int X=1000, k1=5, k2=2, k3=10, k4=-1;
/*FILE* plik = fopen("inlab02.txt", "r");
if (plik == NULL)
return -257;
fscanf(plik, "%d %d %d %d %d", &X, &k1, &k2, &k3, &k4);
fclose(plik);*/
srand(time(NULL));
clock_t begin, end;
double time_spent;
begin = clock();
CreateList();
Delete(head, k1);
ShowBegin(head);
AddMany(head, 10);
AddOne(head, k2);
Delete(head, k2);
AddOne(head, k4);
//Delete(head, k4);
ShowBegin(head);
//ShowEnd(head);
//DeleteAll(head);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Program wykonal obliczenia w %f", time_spent);
getchar();
return 0;
}

Related

doubly linked list problem with removal and push back maybe

In the main function, I tried to push front couple numbers and push back couple numbers and then remove pos = 0 and pso = 1. However, in the output, remove at pos = 0 and remove at pos = 1 prints out number 5 and number 7 which are wrong. It should be 4 and 2. I cannot find which part I am wrong. Here is my code:
#ifndef MYDLL_H
#define MYDLL_H
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
struct node *previous;
} node_t;
typedef struct DLL
{
int count;
node_t *head;
node_t *tail;
} dll_t;
dll_t *create_dll()
{
dll_t *myDLL = (dll_t*)malloc(sizeof(dll_t));
if (myDLL == NULL) {
return NULL;
}
myDLL->count = 0;
myDLL->head = NULL;
myDLL->tail = NULL;
return myDLL;
}
int dll_empty(dll_t *l)
{
if (l == NULL) {
return -1;
}
if (l->count == 0 && l->head == NULL) {
return 1;
} else {
return 0;
}
}
int dll_push_front(dll_t *l, int item)
{
if (l == NULL) {
return -1;
}
node_t* new = (node_t*)malloc(sizeof(node_t));
if (new == NULL) {
return -1;
}
new->data = item;
new->next = l->head;
new->previous = NULL;
if (l->head != NULL) { //set new prev for the previous head before pushing.
l->head->previous = new;
}
l->head = new; // reset the new head for l.
l->count++;
return 1;
}
int dll_push_back(dll_t *l, int item)
{
if (l == NULL) {
return -1;
}
node_t* new = (node_t*)malloc(sizeof(node_t));
if (new == NULL) {
return -1;
}
new->data = item;
new->previous = l->tail;
new->next = NULL;
if(l->tail != NULL) {
l->tail->next = new;
} else {
l->head = new;
}
l->tail = new;
l->count++;
return 1;
}
int dll_pop_front(dll_t *t)
{
if (t == NULL || t->head == NULL) {
return -1;
}
node_t* pop_pointer = t->head;
int pop_item = pop_pointer->data;
t->head = pop_pointer->next; // set new head.
if (t->head != NULL) {
t->head->previous = NULL;
} else { // t->head = NULL cuz only one item and after popping, the t->head is null.
t->tail = NULL; // else if t->head = NULL, then t->tail also should be NULL.
}
if (t->head == NULL) {
t->tail = NULL;
}
t->count--;
free(pop_pointer);
return pop_item;
}
int dll_pop_back(dll_t *t)
{
if (t == NULL || t->head == NULL) {
return -1;
}
node_t* pop_pointer = t->tail;
int pop_item = pop_pointer->data;
t->tail = pop_pointer->previous;
if (t->tail != NULL) {
t->tail->next = NULL;
} else {
t->head = NULL;
}
if (t->head == NULL) {
t->tail = NULL;
}
t->count--;
free(pop_pointer);
return pop_item;
}
int dll_insert(dll_t *l, int pos, int item)
{
if (l == NULL) {
return -1;
}
if (pos >= l->count || pos < 0) {
return 0;
}
node_t* new = (node_t*)malloc(sizeof(node_t));
if (new == NULL) {
return 0;
}
new->data = item;
if(pos == 0) {
return dll_push_front(l, item);
}
node_t* pointer = l->head;
for (int i = 0; i < pos - 1; i++) {
pointer = pointer->next;
}
//ex: pos = 1, no for loop, pointer still head.
new->previous = pointer;
new->next = pointer->next;
pointer->next->previous = new;
pointer->next = new;
l->count++;
return 1;
}
int dll_get(dll_t *l, int pos)
{
if (l == NULL) {
return -1;
}
if (pos < 0 || pos >= l->count) {
return 0;
}
node_t* pointer = l->head;
for (int i = 0; i < pos - 1; i++) {
pointer = pointer->next;
}
return pointer->data;
}
int dll_remove(dll_t *l, int pos)
{
if (l == NULL) {
return -1;
}
if (pos < 0 || pos >= l->count) {
return 0;
}
node_t* pointer = l->head;
node_t* prev = NULL; // first node prev is null.
for (int i = 0; i < pos; i++) {
prev = pointer;
pointer = pointer->next;
}
if (prev == NULL) { // first item.
l->head = pointer->next;
} else {
prev->next = pointer->next;
}
if (pointer->next != NULL) {
pointer->next->previous = prev;
}
// if we are removing the tail node
if (pointer == l->tail) {
l->tail = prev;
}
int removed_value = pointer->data;
free(pointer);
l->count--;
return removed_value;
}
int dll_size(dll_t *t) {
if (t == NULL) {
return -1;
}
return t->count;
}
void free_dll(dll_t *t)
{
if (t == NULL) {
return;
}
if (t == NULL) {
free(t);
return;
}
node_t* pointer = t->head;
while(pointer != NULL) {
node_t* pointer2 = pointer;
pointer = pointer->next;
free(pointer2);
}
free(t);
}
#endif

Singly Linked List head of 0

I got problem with Singly Linked List problem.
When i inserted something in front of head. head is always have 0 of data.
I think init_list() function is something wrong. I think head of 0 is from randomly initialized data.
anything is fine without head 0 problem.
I'm sure that initializing method is wrong. But I don't know how to solve it..
Here is my I/0 and Desired Output
Input
2
insert 0 1
size
Output I got
1->0->NULL
2
1->0->NULL
Desired Output
1->NULL
1
1->NULL
Here is My Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Element;
typedef struct LinkedNode {
Element data;
struct LinkedNode* link;
} Node;
Node* head;
void init_list() {
head = (Node*)malloc(sizeof(Node));
head->link = NULL;
}
int is_empty() {
if (head == NULL) return 1;
else return 0;
}
Node* get_entry(int pos)
{
Node* p = head;
int i;
for (i = 0; i < pos; i++, p=p->link){
if (p == NULL) return NULL;
}
return p;
}
int size()
{
Node* p;
int count = 0;
for (p = head; p != NULL; p = p->link)
count++;
return count;
}
void replace(int pos, Element val)
{
Node* node = get_entry(pos);
if (node != NULL)
node->data = val; // replace
}
Node* search_list(Element val)
{
Node* p;
for (p = head; p != NULL; p = p->link)
if (p->data == val) return p;
return NULL;
}
void insert_next(Node * before, Node * node)
{
if (node != NULL) {
node->link = before->link;
before->link = node;
}
}
void insert(int pos, Element val)
{
Node* new_node, * prev;
new_node = (Node*)malloc(sizeof(Node));
new_node->data = val;
if (pos == 0) {
new_node->link = head;
head = new_node;
}
else {
prev = get_entry(pos-1);
if (prev != NULL)
insert_next(prev, new_node);
else free(new_node);
}
}
Node * remove_next(Node * prev)
{
Node* removed = prev->link;
if (removed != NULL) {
prev->link = removed->link;
}
return removed;
}
void delete(int pos)
{
Node* prev, * removed;
if (pos == 0 && is_empty() == 0) {
removed = get_entry(pos);
head = head->link;
free(removed);
}
else {
prev = get_entry(pos-1);
if (prev != NULL) {
remove_next(prev);
free(removed);
}
}
}
void clear_list()
{
while (is_empty() == 0)
delete(0);
}
void print_list()
{
Node* p;
for (p = head; p != NULL; p = p->link)
printf("%d->", p->data);
printf("NULL\n");
}
Node * concat_list(Node * new_node)
{
if(is_empty()) return new_node;
else if(new_node == NULL) return new_node;
else{
Node* p;
p = head;
while (p->link != NULL) {
p = p->link;
}
p->link = new_node;
return head;
}
}
int main(void)
{
Element num;
int pos;
int n, i, j, len;
char c[15];
Node* tmp_head= NULL;
Node* new_head= NULL;
init_list();
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%s", c);
if (strcmp(c, "insert") == 0) { scanf("%d %d\n",&pos, &num); insert(pos,num); print_list();}
else if (strcmp(c, "delete") == 0) { scanf("%d\n", &pos); delete(pos); print_list();}
else if (strcmp(c, "size") == 0) {printf("%d\n", size()); print_list();}
else if (strcmp(c, "empty") == 0) {printf("%d\n", is_empty()); print_list();}
else if (strcmp(c, "getEntry") == 0) { scanf("%d\n", &pos); printf("%d\n", get_entry(pos)->data); print_list();}
else if (strcmp(c, "search_list") == 0) { scanf("%d\n", &num); printf("%d\n", search_list(num)->data); print_list();}
else if (strcmp(c, "replace") == 0) { scanf("%d %d\n", &pos, &num); replace(pos,num); print_list();}
else if (strcmp(c, "concat_list") == 0) {
tmp_head = head;
init_list();
scanf("%d", &len);
for (j = 0; j < len; j++)
{
scanf("%d %d\n",&pos, &num); insert(pos,num);
}
printf("new_node: ");
print_list();
new_head = head;
head = tmp_head;
head = concat_list(new_head);
print_list();
}
else printf("error\n");
}
return 0;
}
The basic problem is that within init_list, the code only initializes link but not data. I'd suggest instead that you initialize head to NULL and simply use insert to create nodes.

Selection sort of singly Linked Lists by swapping nodes(iterative approach)?

I am trying to perform selection sort on singly linked list by swapping the node itself but after all the inputs it looks like my sort() function in not working properly.
What am I missing or did wrong please someone help me with that.
NOTE: Name of all the functions and pointers pretty much tells their task so I thought not to add comments But if anyone wants comment then please let me know.
NOTE: In swap_node(int i, int j),i and j are the positions of the two nodes which needs to be swapped.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
node *head = NULL, *prev = NULL, *next = NULL;
static int k = 0;//Global variable K which will store the no of nodes created so far.
node *make_node()
{
k++;
return ((node *)malloc(sizeof(node)));
}
void push()
{
if (k == 0)
{
next = make_node();
printf("Enter Data:");
scanf("%d", &next->data);
next->next = NULL;
head = next;
prev = next;
}
else
{
next = make_node();
printf("Enter Data:");
scanf("%d", &next->data);
next->next = NULL;
prev->next = next;
prev = next;
}
}
node *x = NULL, *y = NULL;
void swap_node(int i, int j)
{
node *prevX = NULL, *currX = head, *prevY = NULL, *currY = head;
if (i == 1)
{
for (int l = 1; l < j; l++)
{
prevY = currY;
currY = currY->next;
}
head = currY;
if (j - i == 1)
{
currX->next = currY->next;
currY->next = currX;
}
else
{
node *temp = currX->next;
currX->next = currY->next;
currY->next = temp;
prevY->next = currX;
}
}
else
{
for (int l = 1; l < i; l++)
{
prevX = currX;
currX = currX->next;
}
for (int l = 1; l < j; l++)
{
prevY = currY;
currY = currY->next;
}
if (j - i == 1)
{
prevX->next = currY;
currX->next = currY->next;
currY->next = currX;
}
else
{
node *temp = currX->next;
prevX->next = currY;
prevY->next = currX;
currX->next = currY->next;
currY->next = temp;
}
}
x = currX;
y = currY;
}
void sort()
{
x = head;
int i = 0, j = 0;
for (i = 1; i < k; i++)
{
y = x->next;
for (j = i + 1; j <= k; j++)
{
if (x->data > y->data)
{
swap_node(i, j);
}
y = y->next;
}
x = x->next;
}
}
void print_node()
{
printf("------------Printing Node--------------\n");
node *temp = head;
do
{
printf("%d\n", temp->data);
temp = temp->next;
} while (temp != NULL);
}
void main(void)
{
int choice;
printf("MENU\n1-PUSH\n2-Print node\n");
do
{
printf("Enter Your Choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
sort();
print_node();
break;
default:
printf("Wrong Choice!");
}
} while (choice == 1);
}
why not just swap the content :
void swap_content (node *first, node *second) {
int med = first->data;
first->data = second->data;
second->data = med;
}

delete element from a list

#include <stdio.h>
#include <malloc.h>
struct el {
int info;
struct el* next;
};
struct el* create_el(struct el* Li)
{
int num;
printf("\n\nInsert number:\n\n");
scanf("%d", &num);
Li = (struct el*)malloc(sizeof(struct el));
if (Li != NULL) {
Li->info = num;
Li->next = NULL;
}
return (Li);
}
struct el* push(struct el* L, struct el* e)
{ //inserts the elements from the head of the list
if (L == NULL)
return (e);
else {
e->next = L;
L = e;
return (L);
}
}
void visualize(struct el* primo)
{
printf("\n\nList-->");
while (primo->next != NULL) {
printf("%d", primo->info);
printf("-->");
primo = primo->next;
}
if (primo->next == NULL)
printf("%d-->NULL", primo->info);
}
struct el* cancel(struct el** P, int val)
{ //delete element
struct el* prec = NULL;
struct el* curr = (*P);
if (P == NULL) //case empty list
return NULL;
else if (prec == NULL) {
if (curr->info == val) { //case 2 : if the element is the head
(*P)->next = curr->next;
free(curr);
curr = NULL;
}
}
else {
while ((curr != NULL) && (curr->info != val)) {
prec = curr;
curr = curr->next;
}
if (curr->next == NULL && curr->info == val) { // case 3: the elemnt is the last one
prec->next = NULL;
free(curr);
curr = NULL;
return (prec);
}
else {
if (curr->info == val) { //other cases
prec->next = curr->next;
free(curr);
curr = NULL;
return (prec);
}
}
}
}
int main()
{
struct el* head = NULL;
struct el* element;
struct el* list = NULL;
int i, n;
int elem;
printf("Insert the number of elements for the list:\n\n");
scanf("%d", &n);
for (i = 0; i <= n; i++) {
element = create_el(head);
if (element != NULL) {
list = push(list, element);
}
}
visualize(list);
printf("\n\nInsert the element that you want to cancel:");
elem = scanf("%d", &elem);
cancel(&list, elem);
visualize(list);
}
All I've wanted to do was delete an element from a listr, but after all the procediment the list is printed without any modification.
Can anyone see whats wrong in the function cancel(which is meant to delete an element by including any possible position of it)?
In your function cancel, P is definitely not NULL (assuming OS has assigned it an address initially).
prec is NULL the before execution enters if loop.
So, execution executes the line
if(curr->info==val)
Now, if the value, val, you have provided doesn't match curr->info then execution exits the function without deleting any node.

huffman tree in c prints memory address instead of values?

Here is a program to create huffman tree,
written by my junior.
what's error? perhaps it is printing memory addresses.
#include <stdio.h>
#include <stdlib.h>
struct link
{
int data;
struct link *next, *left, *right;
};
typedef struct link node;
node *curr = NULL, *head = NULL, *nnode = NULL;
void createheap();
void main()
{
node* i;
int k1, k2;
int ch;
int x;
printf("create node\nadd nodes\ntraverse node\nmake heap\n");
do
{
printf("enter the choice\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
nnode = (node*)malloc(sizeof(node));
printf("enter number please\n");
scanf("%d", &x);
nnode->data = x;
nnode->next = NULL;
nnode->left = NULL;
nnode->right = NULL;
head = nnode;
curr = nnode;
break;
case 2:
nnode = (node*)malloc(sizeof(node));
printf("enter number please\n");
scanf("%d", &x);
nnode->data = x;
nnode->next = NULL;
nnode->left = NULL;
nnode->right = NULL;
curr->next = nnode;
curr = nnode;
break;
case 3:
printf("traverse\n");
for (i = head; i != NULL; i = i->next)
{
printf("%d\n", i->data);
}
break;
case 4:
createheap();
break;
default:
printf("this is wrong number\n");
}
} while (ch <= 5);
}
void createheap()
{
node* l;
node *t1, *t2, *t;
node *np, *r1;
int k1, k2, k3;
node* i;
// void traverse(node*);
l = head;
while (head->next != NULL)
{
l = head;
t = l;
nnode = (node*)malloc(sizeof(node));
t1 = t->next;
t2 = t->next->next;
k1 = t->data;
k2 = t1->data;
k3 = k1 + k2;
if (t2 == NULL)
{
nnode->left = t;
nnode->right = t1;
nnode->data = k3;
nnode->next = NULL;
curr = nnode;
head = nnode;
}
else if (k3 <= t2->data)
{
nnode->next = t2;
nnode->left = t;
nnode->right = t1;
nnode->data = k3;
head = nnode;
}
else if (k3 > t2->data)
{
nnode->left = t;
nnode->right = t1;
nnode->data = k3;
if (k3 <= curr->data)
{
for (i = t2; i != NULL; i = i->next)
{
if (i->data >= k3)
{
break;
}
else
{
np = i;
}
}
r1 = np->next;
nnode->next = r1;
np->next = nnode;
}
else if (k3 > curr->data)
{
nnode->next = NULL;
curr->next = nnode;
curr = nnode;
}
head = t2;
}
free(t);
free(t1);
printf("l %d\n", nnode->left);
printf("r %d\n", nnode->right);
printf("d %d\n", nnode->data);
}
// traverse(l);
// printf("\n%d",l->left->data);
// printf("\n%d",l->right->left->data);*/
}
void traverse(node* t)
{
if (t != NULL)
{
traverse(t->left);
printf("\n%d\n", t->data);
traverse(t->right);
}
}
"create node" means initialise the list and enter first value.
"add nodes" means add second, third and so on.. values. (please provided sorted values only for now)
"make heap" means create heap out of provided list, AND print left(l), right(r) and root values(d), from bottom of tree (as it gets made).
So, problem is root is printing correct value. but left and right are printing like.. 31298384, 31298323.

Resources