Hello,

I'm still a novice with this C programming and I got some problems with my program. I got 3 errors and 3 warnings with my Linked List. I attached an image for you to see. Someone can help me how to solve this problem?

Here's my code:

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

void push(struct list *sList, int data);
void pop(struct list *sList);
void print(struct list *sList);

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

struct list
{
 struct node *start;	
};


int main()
{
 struct list *sList;
 int i,cho=0;
 do
 {
  printf("\n\n\n [1] Add Items\n");
  printf(" [2] Display Items\n");
  printf(" [3] Delete Items\n");
  printf(" [4] Exit\n\n");
  printf(" Enter your choice: ");
  scanf("%d",&cho);
  i = getchar();
  if(cho==1)
  {
   push(sList,data);
  }
  else if(cho==2)
  {
   print(slist);
  }
  else if(cho==3)
  {
   pop(sList);
  }
 }
 while(cho != 4);
 {
  clrscr();
  printf("\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t    ___________________\n\n\t\t\t    Press Enter to Exit");
  printf("\n\t\t\t    ___________________");
  i = getchar();
  ++i;
 }
 clrscr();
 return 0;
}

void push(struct list *sList, int data);
{
 sList->start = NULL;
 struct node *p;
 int n;
 clrscr();
 printf("\n\n Enter integer for total numbers to be allocated: ");
 scanf("%d",&n);
 printf("\n\n");
 p = (struct node *)malloc(sizeof(struct node));
 p->data = n;
 p->next = sList->start;
 sList->start = p;
}

void print(struct list *sList)
{
 struct node *p = sList->start;
 while(p != NULL)
 {
  printf("%d",p->data);
  p=p->next;
 }
}

void pop(struct list *sList)
{
 if(sList->start != NULL)
 {
  struct node *p = sList->start;
  sList->start = sList->start->next;
  free(p);
 }
}

Recommended Answers

All 4 Replies

I'm confused.

Linked lists can be done as a queue (a regular list or line), or as a stack (like a stack of plates).

push and pop are used for stacks, and data is entered one at a time, at the top of the stack, only.

lists use a FIFO first in, first out, type of data flow, and the new data is simply added to the tail end of the line - again, one at a time.

I don't know which you're trying to make here, or why you have an option to make more than one node at a time, or why you have nested structs, etc.

Maybe you can tell us what the data struct is being used for, then we can understand it more.

Hello Adak,

Actually I just copied those codes from HERE.

Here's the code:

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

// Prototypes
void InitList(struct list *sList);
void push(struct list *sList, int data);
void pop(struct list *sList);
void print(struct list *sList);

/* Node Structure */
struct node {
	int data;
	struct node *next;
};

/* List Structure */
struct list {
	struct node *start;	
};

int main(int argc, char** argv)
{
	int x;
	
	struct list MyList;
	InitList(&MyList);
	
	for(x = 0; x < 100; x++) push(&MyList, x);
	print(&MyList);
	printf("\n");
	for(x = 0; x < 25; x++) pop(&MyList);
	print(&MyList);
	printf("\n");
	for(x = 0; x < 80; x++) pop(&MyList);
	print(&MyList);
	printf("\n");
	
	return 0;
}

/* Initializes the list structure */
void InitList(struct list *sList)
{
	sList->start = NULL;
}

/* Adds a value to the front of the list */
void push(struct list *sList, int data)
{
	struct node *p;
	p = (struct node *)malloc(sizeof(struct node));
	p->data = data;
	p->next = sList->start;
	sList->start = p;
}

/* Prints the list */
void print(struct list *sList)
{
	struct node *p = sList->start;
	while(p != NULL) {
		printf("%d ", p->data);
		p = p->next;
	}
}

/* Removes the first value of the list */
void pop(struct list *sList)
{
	if(sList->start != NULL) {
		struct node *p = sList->start;
		sList->start = sList->start->next;
		free(p);
	}
}

Can you help me solve my problem?

You've copied code and said "it doesn't work". That's it. :(

Copying code is NOT programming, and you don't seem to be studying programming. You seem to be mastering the art of finding code, and copying it.

I'm not interested in fixing somebody's code that I don't know, from some other forum, so you can get through an assignment, without studying.

It's fine to study other people's code, but you need to study up, and write your own, also. Copy and pasting won't give you good answers, reliably.

If you are interested in fixing your code I would start of by getting rid of the list data structure ... It just adds complexity

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.