I have this assignment where I need to implement string hashcoding with chaining in c++ I,ve already tried it but with int data type only could anyone provide some guidance on how to do it? Should I use another hashcode function ??
I’ve already done the display insert function I still have to convert it to string and add three functions of hashcoding and apply it each time to a file and tht each line contain a random word of length from 3 to 30 characters and I have 10000 line which means 10000 words
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10 // prime number for size of array
#define h(x) (x % m) // h(x) = x mod m
// Node for List
typedef struct Node {
int val;
struct Node *next;
} Node;
//function's declaration
Node *fill_table(Node *table, int table_range, int number);
Node *insert(Node *table, int hash_index, int val);
void print_table(Node *table, int table_range);
int main() {
Node *table; // hashtable
int n, i, j, choice, search;
int hash_num, val;
// allocate table
table = (Node*) malloc(N * sizeof(Node));
// make table "heads" NULL
for (i = 0; i < N; i++) {
table[i].next = NULL;
}
printf("--h(x) = xmod%d--\n", N);
printf("\n\n");
while (1) {
printf("1.Insert random numbers\n");
printf("2.Show Hash Table\n");
printf("0.Exit Programm\n");
printf("\n--------\n");
printf("Choice: ");
scanf("%d",&choice);
switch (choice) {
case 0: break;
case 1:
// insert random numbers
printf("Lot to insert: ");
scanf("%d", &n);
table = fill_table(table, N, n);
printf("Filled HashTable with %d random values\n", n);
printf("\n--------\n");
break;
case 2:
// print hashtable
printf("-HASHTABLE-\n\n");
print_table(table, N);
printf("\n--------\n");
break;
}
}
return 0;
}
// FUNCTIONS
Node *fill_table(Node *table, int table_range, int number) {
int i;
int num;
for (i = 0; i < number; i++) {
num = rand() % 10000; // random number from 0 to 9999
table = insert(table, num % table_range, num);
}
return table;
}
void print_table(Node *table, int table_range) {
int i;
Node* cur;
for (i = 0; i < table_range; i++) { // for each list
if (table[i].next == NULL) { // if empty
printf("Table[%d] is empty!\n", i);
continue;
}
cur = table[i].next;
printf("Table[%d]", i);
while (cur != NULL) { // else print all the values
printf("->%d", cur->val);
cur = cur->next; //cur=cur+1;
}
printf("\n");
}
}
Node *insert(Node *table, int hash_index, int val) {
Node *nn, *cur;
nn = (Node*)malloc(sizeof(Node));
nn->val = val;
nn->next = NULL;
if (table[hash_index].next == NULL) {
table[hash_index].next = nn;
return table;
}
cur = table[hash_index].next;
while (cur->next != NULL) {
cur = cur->next; //cur=cur+1;
}
cur->next = nn;
return table;
}
hash
Related
I am trying to creat a linked list in c language,
the code compiles with no error but when I run it , it won't run.
the data structure is a task which contains the label it's number, duration , number of linked tasks which will be put in an array and a pointer to the next task.
here's my code and the test code
#include <stdio.h>
#include <stdlib.h>
typedef struct tache { //linked listes structure
char label;
int num; //task number
int dure;
int *inter;
int n; //nombre d'antécedents
struct tache * suivant;
}strTache,*pTask;
pTask creerVide(){ //créer une tache vide
return NULL;
}
pTask firstTask(int d){
//créer la première tache qui prend la durée en argument
pTask first = (strTache*)(malloc(sizeof(strTache)));
first->num = 1;
first->suivant = NULL;
first->label = 'A';
first->dure = d;
first->n = 0;
return first;
}
Bool vide(pTask t){return t==NULL;} //check if a liste of tasks is empty this is predefined in a header included in the program
pTask ajouteTask(pTask t, int d){
pTask new = (strTache*)(malloc(sizeof(strTache)));
if(vide(t)){
new = firstTask(d);
t->suivant = new;
}else
{
pTask parc = t;
while(parc->suivant != NULL){parc=parc->suivant;}
new->num = parc->num+1;
new->label = parc->label+1;
new->suivant = parc->suivant;
parc->suivant = new;
new->dure=d;
}
return new;
}
void ajouteInter(pTask p,int numero, int taille, int tab[]){
int i,j;
pTask move = p;
while(move->suivant!=NULL){
if(move->num != numero){
move=move->suivant;
}//fin if
else
{
move->inter = (int*) malloc(taille*sizeof(int));
move->n = taille;
}//fin else
}//fin while
for(i=0;i<taille;i++){
move->inter[i] = tab[i];
}//fin for
}//fin function
void affichmat2(pTask p){
int i = 0;
pTask parc = p;
while(parc!= NULL){
printf("la tache %c a %d antécédents : ",parc->label,parc->n);
for(int j=0;j<parc->n;j++){
printf("%d ",parc->inter[j]);
}
printf("\n");
parc=parc->suivant;
i++;
}
free(parc);
}
int main()
{
int b[1] = {1};
int c[2] = {1,2};
//pTask t1 = creerVide();
pTask t1 = firstTask(2);
pTask t2,t3;
t2 = ajouteTask(t1,3);
t3 = ajouteTask(t2,4);
ajouteInter(t2,2,1,b);
ajouteInter(t3,3,2,c);
affichmat2(t1);
return 0;
}
I have made some changes thanks to your advice:
the function ajoutetask which called now addtask:
,,,
pTask addTask(pTask t, int d){
pTask new;
if(vide(t)){
new = firstTask(d);
new->suivant = t;
}else
{
new = (strTache*)(malloc(sizeof(strTache)));
pTask parc = t;
while(parc->suivant != NULL){parc=parc->suivant;}
new->num = parc->num+1;
new->label = parc->label+1;
new->suivant = parc->suivant;
parc->suivant = new;
new->dure=d;
}
return new;
}
,,,
the function ajouteinter which called now add antecedent:
void addAntecedent(pTask p,int num, int size, int b[]){
pTask search = p;
while(search->num !=num){
search = search->suivant;
}
search->inter = (int*)malloc(sizeof(int)*size);
search->n = size;
for(int i = 0;i<size;i++){
search->inter[i] = b[i];
}
}
the changes to the test file :
int main (){
int b[1] = {1};
int c[2] = {1,2};
pTask t1 = firstTask(2);
pTask t2,t3;
t2 = addTask(t1,3);
t3 = addTask(t1,4);
addAntecedent(t1,2,1,b);
addAntecedent(t1,3,2,c);
affichmat2(t1);
return 0;
}
the code works properly now
the result :
la tache A a 0 antécédents :
la tache B a 1 antécédents : 1
la tache C a 2 antécédents : 1 2
For starters use English words for identifiers.
Your code does not make a sense.
Consider for example the function ajouteTask
pTask ajouteTask(pTask t, int d){
pTask new = (strTache*)(malloc(sizeof(strTache)));
if(vide(t)){
new = firstTask(d);
t->suivant = new;
//...
If the pointer t is equal to NULL (that is when the function vide returns 1) then 1) the function invokes undefined behavior due to this statement
t->suivant = new;
and 2) it produces a memory leak because at first a memory was allocated and its address was assigned to the pointer new
pTask new = (strTache*)(malloc(sizeof(strTache)));
and then the pointer was reassigned
new = firstTask(d);
Or within the function ajouteInter you have an infinite while loop if move->num is equal to numero
void ajouteInter(pTask p,int numero, int taille, int tab[]){
int i,j;
pTask move = p;
while(move->suivant!=NULL){
if(move->num != numero){
move=move->suivant;
}//fin if
else
{
move->inter = (int*) malloc(taille*sizeof(int));
move->n = taille;
}//fin else
}//fin while
//...
So what you need is to rework your code and if there will be problems ask a new question.
Click here
https://uniblogsp.blogspot.com/2022/05/link-list-implementation-in-c-insertion.html
#include <stdio.h>
#include <stdlib.h>
typedef struct intnode
{
int data;
struct intnode *next;
} intnode;
intnode *create(int n)
{
intnode *head, *q, *newnode;
head = (intnode *)malloc(sizeof(intnode));
scanf("%d", &head->data);
head->next = NULL;
q = head;
for (int i = 0; i < n - 1; i++)
{
newnode = (intnode *)malloc(sizeof(intnode));
scanf("%d", &newnode->data);
newnode->next = NULL;
q->next = newnode;
q = newnode;
}
return head;
}
void printList(intnode *head)
{
intnode *temp;
temp = head;
while (head != NULL)
{
printf("%d", head->data);
head = head->next;
printf("\n");
}
head = temp;
}
void insertList(intnode *head, int i, intnode *t)
{
intnode *r, *x;
if (i == 0)
{
t->next = head;
head = t;
return;
}
r = head;
for (int j = 1; (j < i - 1) && (r != NULL); j++)
{
r = r->next;
}
if ((r == NULL) && (i > 0))
{
return;
}
x = r;
t->next = x->next;
x->next = t;
return;
}
void deleteList(intnode *head, int i)
{
intnode *y, *x;
y = head;
if (i == 0)
{
head = head->next;
free(y);
return;
}
x = y->next;
for (int j = 1; (j < i) && (x != NULL); i++)
{
y = x;
x = x->next;
}
if ((x == NULL) && (i > 0))
{
return;
}
y->next = x->next;
x->next = NULL;
free(x);
return;
}
int main()
{
int n, l;
printf("Enter the no of nodes: \n");
scanf("%d", &n);
printf("Enter the elements of nodes: \n");
intnode *p = create(n);
printf("Nodes are: \n");
printList(p);
printf("If you want to insert node then press 1 and in case of delete node press 0 : \n");
int ans;
scanf("%d", &ans);
if (ans == 1)
{
printf("In what positiomn you want to insert the node ??\n");
scanf("%d", &l);
intnode *a;
a = (intnode *)malloc(sizeof(intnode));
printf("Enter the node data :\n");
scanf("%d", &a->data);
a->next = NULL;
insertList(p, l, a);
printf("After insertion the list is : \n");
printList(p);
}
else
{
printf("In what positiomn you want to delete the node ??\n");
scanf("%d", &l);
deleteList(p, l);
printf("After deletion the list is : \n");
printList(p);
}
return 0;
}
I am trying to write numbers from type double outside of the stack but I can't quite work it out how to do it. I have tried doing it with while loop and if statements, sometimes I get only zeros but most of the time I get nothing displayed. Thanks for helping in advance.
#include <stdio.h>
struct element
{
double data;
element * next;
};
element *top = NULL;
void Get(double &x)
{
element *p;
if(top == NULL)
{
printf("\nThe stack is empty!");
return;
}
x = top -> data;
p = top;
top = top -> next;
delete(p);
}
void Put(double x)
{
element *p;
p = new(element);
if(p == NULL)
{
printf("\nNOT enough memory!");
return;
}
p -> data = x;
p -> next = top;
top = p;
}
int main()
{
printf("Enter integers in stack. Enter 0 to end!\n");
double val;
double m;
do
{
printf("Enter number: ");
scanf("%lf", &val);
if(val >= 3697 && val <= 3698)
{
Put(val);
}
}
while(val != 0);
printf("\nStack contains: ");
while(top != NULL)
{
Get(val);
printf("%lf ", val);
}
return 0;
}
I'm trying to create a sparse matrix in C using linked lists. My professor gave the file.h structs, a list of the functions and what they should do and told us to "work with it".
To resume everything, my code does not work, it stops working when I try to insert an element (calling ins_elem()) and returning weird number when calling soma_elem_coluna() and soma_elem_linha().
I've put the whole code below but the issue is really within the functions specified above (and soma_const() as I can't test it without inserting an element).
The logic I used is to create a matrix as in the image below (with numbers instead of characters)
[
Don't mind the weird typing, some words are in Portuguese.
(C = column and L = row)
this is my file.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINES 4
#define COLUM 4
typedef struct Node{
int Val, L, C;
struct Node *down, *right;
}Node;
typedef struct{
Node *L[LINES], *C[COLUM];
}matriz;
void cria_matriz(matriz* m); // creates matrix
void ins_elem(matriz* m, int e, int l, int c); // insert element e into lxc
void soma_const(matriz* m, int e, int l, int c); // add value e to existing lxc
int soma_elem_linha(matriz *m, int l); // sums all the values in row l
int soma_elem_coluna(matriz *m, int c); // sums all the values in column c
void imp(matriz *m); // prints the matrix
my file.c
#include "file.h"
void cria_matriz(matriz* m)
{
int i;
for(i=0;i<LINES;i++) //initializes headrows
{
m->L[i]->down = NULL;
m->L[i]->right = NULL;
m->L[i]->C = -1;
m->L[i]->L = -1;
}
for(i=0;i<COLUM;i++) //initializes headcolumns
{
m->C[i]->down = NULL;
m->C[i]->right = NULL;
m->C[i]->C = -1;
m->C[i]->L = -1;
}
}
void ins_elem(matriz *m, int e, int l, int c)
{
Node* auxLine; // move through row
Node* auxCol; // move through column
auxLine = &m->L[l];
auxCol = &m->C[c];
while(auxLine->right != NULL && auxLine->right->C < c) //find correct pos in row
{
auxLine = auxLine->right;
}
while(auxCol->down != NULL && auxCol->down->L < c) //find correct pos in column (error happens here)
{
auxCol = auxCol->down;
}
Node* novo = malloc(sizeof(Node));
novo->Val = e;
novo->C = c;
novo->L = l;
novo->down = auxCol->down;
auxCol->down = novo;
novo->right = auxLine->right;
auxLine->right = novo;
}
void soma_const(matriz *m, int e, int l, int c)
{
Node* aux = &m->C[c];
while(aux->down != NULL && aux->L < l)
aux = aux->down;
aux->Val+=e;
}
int soma_elem_coluna(matriz *m, int c)
{
int sum = 0;
Node* aux = &m->C[c];
if(aux->down == NULL)
return 0;
while(aux->down != NULL)
{
aux = aux->down;
sum += aux->Val;
}
return sum;
}
int soma_elem_linha(matriz *m, int l)
{
int sum = 0;
Node* aux = &m->L[l];
if(aux->right == NULL)
return 0;
while(aux->right != NULL)
{
aux = aux->right;
sum += aux->Val;
}
return sum;
}
void imp(matriz *m)
{
Node* aux;
int i, j;
for(i=0;i<LINES;i++)
{
aux = &m->L[i];
for(j=0;j<COLUM;j++)
{
if(aux->C == j)
{
printf("%d ",aux->Val);
aux = aux->right;
}
else
{
printf("0 ");
}
}
printf("\n");
}
}
my main.c:
#include "file.h"
int main()
{
struct matriz* M = (matriz*)malloc(sizeof(matriz));
int i,e,c,l,result;
char str[20];
for(i=0;i<5;i++) // this is temporary and another issue, how I make a while that
{ // only stops when user (a bot actually) presses enter?
scanf(" %s",str);
if(!strcmp(str,"cria_matriz"))
{
cria_matriz(M);
}
else if(!strcmp(str,"ins_elem"))
{
scanf("%d %d %d",&e,&l,&c);
ins_elem(M,e,l,c);
}
else if(!strcmp(str,"soma_const"))
{
scanf("%d %d %d",&e,&l,&c);
soma_const(M,e,l,c);
}
else if(!strcmp(str,"soma_elem_linha"))
{
scanf("%d",&l);
result = soma_elem_linha(M,l);
printf("%d\n",result);
}
else if(!strcmp(str,"soma_elem_coluna"))
{
scanf("%d",&c);
result = soma_elem_coluna(M,c);
printf("%d\n",result);
}
else if(!strcmp(str,"imp"))
{
imp(M);
}
}
return 0;
}
Your files didn't compile without warnings as there were lots of pointer and other problems with the code. I've worked over your code to clean up the warnings but I'm not in a position to test the logic -- you'll have to do that:
file.c
#include "file.h"
void cria_matriz(matriz *m)
{
for (int i = 0; i < LINES; i++) // initializes headrows
{
m->L[i] = malloc(sizeof(Node));
m->L[i]->down = NULL;
m->L[i]->right = NULL;
m->L[i]->C = -1;
m->L[i]->L = -1;
m->L[i]->Val = 0;
}
for (int i = 0; i < COLUM; i++) // initializes headcolumns
{
m->C[i] = malloc(sizeof(Node));
m->C[i]->down = NULL;
m->C[i]->right = NULL;
m->C[i]->C = -1;
m->C[i]->L = -1;
m->C[i]->Val = 0;
}
}
void ins_elem(matriz *m, int e, int l, int c)
{
Node *auxLine = m->L[l]; // move through row
Node *auxCol = m->C[c]; // move through column
while (auxLine->right != NULL && auxLine->right->C < c) // find correct pos in row
{
auxLine = auxLine->right;
}
while (auxCol->down != NULL && auxCol->down->L < c) // find correct pos in column (error happens here)
{
auxCol = auxCol->down;
}
Node *novo = malloc(sizeof(*novo));
novo->Val = e;
novo->C = c;
novo->L = l;
novo->down = auxCol->down;
auxCol->down = novo;
novo->right = auxLine->right;
auxLine->right = novo;
}
void soma_const(matriz *m, int e, int l, int c)
{
Node *aux = m->C[c];
while (aux->down != NULL && aux->L < l)
{
aux = aux->down;
}
aux->Val += e;
}
int soma_elem_coluna(matriz *m, int c)
{
Node *aux = m->C[c];
if (aux->down == NULL) {
return 0;
}
int sum = 0;
while (aux->down != NULL)
{
aux = aux->down;
sum += aux->Val;
}
return sum;
}
int soma_elem_linha(matriz *m, int l)
{
Node *aux = m->L[l];
if (aux->right == NULL) {
return 0;
}
int sum = 0;
while (aux->right != NULL)
{
aux = aux->right;
sum += aux->Val;
}
return sum;
}
void imp(matriz *m)
{
for (int i = 0; i < LINES; i++)
{
Node *aux = m->L[i];
for (int j = 0; j < COLUM; j++)
{
if (aux->C == j)
{
printf("%d ", aux->Val);
aux = aux->right;
}
else
{
printf("0 ");
}
}
printf("\n");
}
}
main.c
#include "file.h"
int main()
{
matriz *M = malloc(sizeof(*M));
int e, c, l;
char str[32];
for (int i = 0; i < 5; i++) // this is temporary and another issue, how I make a while that
{ // only stops when user (a bot actually) presses enter?
(void) scanf(" %s", str);
if (strcmp(str, "cria_matriz") == 0)
{
cria_matriz(M);
}
else if (strcmp(str, "ins_elem") == 0)
{
scanf("%d %d %d", &e, &l, &c);
ins_elem(M, e, l, c);
}
else if (strcmp(str, "soma_const") == 0)
{
(void) scanf("%d %d %d", &e, &l, &c);
soma_const(M, e, l, c);
}
else if (strcmp(str, "soma_elem_linha") == 0)
{
(void) scanf("%d", &l);
int result = soma_elem_linha(M, l);
printf("%d\n", result);
}
else if (strcmp(str, "soma_elem_coluna") == 0)
{
scanf("%d", &c);
int result = soma_elem_coluna(M, c);
printf("%d\n", result);
}
else if (strcmp(str,"imp") == 0)
{
imp(M);
}
}
free(M);
return 0;
}
The biggest issue was that you kept making pointers to things that were already pointers. Also, you initialized data that you hadn't actually allocated. You didn't initialize some data that you subsequently added onto. Finally, strcmp() isn't a boolean, don't use it as one.
I was trying to add some linked list to my recursive prime number codes, I was able to store the values using linked list then when I was to retrieve the prime numbers between the two inputted numbers i got this as a result.
for input 1 and 5: 1, 21, 301, 5
output should be:
2, 3, 5
the code is:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct node
{
//struct node *prev;
int num;
struct node *nxt;
}*head;
void store(int value){
struct node *var, *temp;
var = (struct node *)malloc(sizeof(struct node));
var->num = value;
if(head==NULL){
head = var;
head->nxt = NULL;
}else{
temp = var;
temp->nxt = head;
head=temp;
}
}
void accept(int value, int i){
if(i<2){
printf("Enter value: ");
scanf("%d", &value);
store(value);
i++;
accept(value,i);
}
}
void prime(){
int num,x,y;
struct node *temp,*temp2,*var;
temp = head;
temp2 = temp->nxt;
y = temp->num;
x = temp2->num;
primeloop(x,y);
}
int primeloop(int x,int y){
int num;
if ( x == 1 ) x++;
if(x <= y){
num = isPrime(x,2); // second input parameter added
printf("%d",num);
if(num == 0){
printf("");
}else{
printf("%5d",x);
}
primeloop(x+1,y);
}
}
int isPrime(int n, int i){
if(n%i==0 && n!=2 && n!=i){
return(0);
} else {
if (i < sqrt(n)) {
return( isPrime(n,i+1) );
} else
return(1);
}
}
void main(){
int i,value;
clrscr();
i = 0;
accept(value,i);
prime();
getch();
}
I had to change some lines to make it work with linked list, I could be missing something here since the algo is still the same. Do point out what I did wrong.
I found your problem I think. Your algorithm is correct, but you have two redundant printfs.
your primeloop should be like this
int primeloop(int x,int y){
int num;
if ( x == 1 ) x++;
if(x <= y){
num = isPrime(x,2); // second input parameter added
if(num != 0){
printf("%5d ",x);
}
primeloop(x+1,y);
}
}
Here is my code:
#include<stdio.h>
#include<stdlib.h>
typedef struct list_node
{
int data;
struct list_node *next;
} node;
node *head,*temp;
int counter;
void display_menu();
char get_choice();
void process(char option);
void Insert_first();
void Print_first();
void Delete_first();
void Delete_end(); // Problem
void Insert_end(); // Problem
void Search();
int main()
{
char choice;
display_menu();
do
{
choice=get_choice();
process(choice);
} while(choice!='J');
return 0;
}
void display_menu()
{
printf("What do you want do do?\n");
printf("A. Insert a node at the beginning of the list\n");;
printf("B. Delete the first node of the list\n");
printf("C. Print list\n");
printf("D. Insert end\n");
printf("E. Delete the last node of the list\n");
printf("F. Search\n");
}
char get_choice()
{
char select;
printf("Enter your choice:\t");
scanf("%c",&select);
getchar();
printf("\n");
return select;
}
void process(char option)
{
switch(option)
{
case 'A': Insert_first();break;
case 'B': Delete_first();break;;
case 'C': Print_first();break;
case 'D': Insert_end();break;
case 'E': Delete_end();break;
case 'F': Search(); break;
default:
printf("Invalid input!\n\n");
}
}
void Search()
{
node *a;
node *b;
int i, j;
a=head;
b=head;
for(i = 1; i < counter - 1; ++i){
a = a -> next;
for(j = i + 1; j < counter; ++j){
b = b -> next;
if((a -> data) == (b -> data)){
printf("0\n");
}
else{
printf("1\n");
}
}
}
counter++;
}
void Insert_first()
{
temp=(node *)malloc(sizeof(node));
printf("Enter a number:\t");
scanf("%d",&(temp->data));
getchar();
printf("\n");
temp->next=head;
head=temp;
counter++;
}
void Insert_end()
{
temp=(node *)malloc(sizeof(node));
node *x;
int y;
printf("Enter a number:\t");
scanf("%d",&(temp->data));
getchar();
printf("\n");
x=head;
for(y = 1; y < counter; ++y){
x=x->next;
}
temp->next=x->next;
x->next = temp;
counter++;
}
void Print_first()
{
if(head==NULL)
{
printf("The list is empty\n");
}
else
{
temp=head;
printf("-------------------------------------------------\n");
do
{
printf("\t%d\t", temp->data);
temp=temp->next;
} while(temp!=NULL);
printf("\n");
printf("-------------------------------------------------\n\n");
}
}
void Delete_first()
{
if(head==NULL)
{
printf("\nThe list is empty\n");
}
else
{
temp=head;
head=temp->next;
free(temp);
counter--;
}
}
void Delete_end()
{
node *x, *z;
int y;
if(head == NULL)
printf("The list is empty");
else{
x=head;
for(y = 1; y < counter; ++y){
x=x->next;
}
z=x;
x=x->next;
free(z);
counter--;
}
}
I have two problems to solve:
The Delete_end() function;
The Search() function.
I want the Delete_end() function to act as Delete_first() function, but instead of deleting the FIRST node and deallocates its space, it does these to the END node. I tried to code these functions, but it didnt return what I want it to be.
If we run the codes, and Enter the Following:
As you notice when I delete the first node (Switch Choice: B), the list from
--------------------------------
4 3 2
--------------------------------
becomes
--------------------------------
3 2
--------------------------------
Thus, it deletes the first node and deallocates the space. Now when I delete the end node (Switch Choice: E) and print the list (Switch Choice: C), the list becomes
--------------------------------
3 135704608
--------------------------------
I think the number 135704608 is the location in the memory. So what happen here is it didnt delete the last node and deallocates it but instead returns 135704608. I hope you can help me with that.
Second function is the Search(), this function should return 0 if the number is already in the list, else 1 is returned. Here is the Search() function from the above codes:
void Search()
{
node *a;
node *b;
int i, j;
a=head;
b=head;
for(i = 1; i < counter - 1; ++i){
a = a -> next;
for(j = i + 1; j < counter; ++j){
b = b -> next;
if((a -> data) == (b -> data)){
printf("0\n");
}
else{
printf("1\n");
}
}
}
counter++;
}
Any help is greatly appreciated!
For delete at end, your code finds the next to last element and does this:
z=x; // save next to last as z
x=x->next; //set last as x
free(z); // free next to last
Instead, I think you want to do something more like this:
free(x->next); // free last elem
x->next = NULL; // next to last now points to NULL, meaning x is now the last element
The problem with your search function is that you never check the first element! You should do your if comparison before you do b = b -> next and then do a = a -> next after the inner for loop - otherwise the head will never be compared to anything, and nothing will be compared to the element immediately after it.